Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 171351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:58:21+00:00 2026-05-11T12:58:21+00:00

Suppose I have an interface for a service: public interface IFooService { void DoSomething();

  • 0

Suppose I have an interface for a service:

public interface IFooService {    void DoSomething(); } 

And a concrete implementation of that service that is a generic:

public class FooService<TRequestingClass> : IFooService {    public virtual void DoSomething() { } } 

And I have some other class that needs an instance of IFooService:

public class Bar {    private IFooService _fooService;    public Bar(IFooService fooService)    {       this._fooService = fooService;    } } 

I need to wire up my IoC container such that when Bar gets created, it gets passed a constructor argument of FooService<Bar>. There are many other classes just like Bar. Each might also need an instance of FooService<TRequestingClass> passed to them where TRequestingClass is the type of the class that needs the instance of IFooService. I don’t need to expose this quirkiness to the consumers of IFooService. All they should care about is that they can call the methods of the IFooService they were passed. They should not need to know that the concrete implementation of IFooService they were passed needed anything special to be constructed.

An acceptable alternatative to FooService<T> would to be a non-generic class that has a string argument in its constructur that contains the name of the class it is being created for. i.e:

public class FooService : IFooService {    public FooService(string requestingClassName) { } } 

How can I wire up my IoC container to construct a dependency this way?

If you are confused why I would want such a wierd structure, consider how log4net works best when you get an ILog that gets created with log4net.LogManager.GetLogger(typeof(SomeClass)). I don’t want to litter my code with references to log4net, so I’d like to write a simple ILogger interface, and implement it with something like this:

public class GenericLogger<T> : ILogger {     private readonly ILog log;      public GenericLogger()     {         this.log = log4net.LogManager.GetLogger(typeof(T));     }      public void Debug(object message)     {         this.log.Debug(message);     }      /* .... etc ....  */ } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:58:22+00:00Added an answer on May 11, 2026 at 12:58 pm

    The easiest way would be to create an ILogger<T> interface:

    public class ILogger<T> : ILogger { } public class GenericLogger<T> : ILogger<T> { ... } 

    Then rely on generic type inference to get the correct type. For example, in Ninject, the following binding is all you’d need:

    Bind(typeof(ILogger<>)).To(typeof(GenericLogger<>)); 

    Then your consuming types would look like this:

    public class FooService : IFooService {   public FooService(ILogger<FooService> logger) { ... } } 

    If you’re adamantly against the ILogger<T> interface, you could do something more creative, like a custom provider, which reads the IContext to determine the parent type.

    public class GenericLogger : ILogger {   public class GenericLogger(Type type) { ... } }  public class LoggerProvider : Provider<ILogger> {   public override ILogger CreateInstance(IContext context) {     return new GenericLogger(context.Target.Member.ReflectedType);   } } 

    Then consuming types would work like this:

    public class FooService : IFooService {   public FooService(ILogger logger) { ... } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One possible solution would be to store those common preferences… May 11, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer No, you'll get a OutOfMemoryException in the object can't be… May 11, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer Create a "criteria" object and use that as a parameter.… May 11, 2026 at 5:45 pm

Related Questions

I have a WSDL file and I am trying to create a web service
I have been assigned a project to develop a set of classes that act
I'm looking to create a rich-featured application using a client/server model, using WCF as
I am trying to implement a decorator chain for my data-access based on IRepository.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.