1

My requirement is very similar to the question asked here - With.Parameters.ConstructorArgument with ninject 2.0 This may be a dumb question -- But, isn't the approach of sending parameters to the constructor during the call to Get<IMyType> goes against the philosophy of the de-coupling (which, I believe is the major motivation for DI) itself?
This assumes that the user of IMyType has knowledge of the constructor of the specific implemenation of IMyType.
Since there is no way to declare the constructor arguments within the interface, it is possible that another implementation can be added in the future, which takes different set of parameters in the constructor & you bind to that implementation. Again, going by the philosophy of de-coupling, the code that does Bind<IMyType>.To<ADifferentImpl>() should be able to work with any module that was written earlier, which gets the implementation object from Get<IMyType>.

I have very similar requirement - where I don't know the values of the constructor parameters at the time of binding, but only at the time of accessing. I thought that the best approach to follow in the given situation is to go with Adrian Grigore's answer. Please let me know if there is any other approach.

Community
  • 1
  • 1
Bhanu Gotluru
  • 235
  • 2
  • 7

1 Answers1

0

I will make an example how I did it:

public static class DI
{
    private static IKernel _kernel;
    private static Boolean _kernelLoaded = false;
    public static T Resolve<T>() where T : class
    {
        ...
        var a = _kernel.Get<T>();
        return a;
    }
   ...
}

Here is the class UserRepository which have constructor with parammetrs:

public class UserRepository : RepositoryBase<User>, IUserRepository
{
    public UserRepository(IMyContext context)
        : base(context)
    {
    }

    public void AddList(String name)
    {

    }
}

As you can see my constructor work only with the interface of MyContext, Ninject automaticly give to the default constructor of the IMyContext examplar if it is registred in ninject.

Here is example of bindings in ninject:

    public override void Load()
    {
        Bind(typeof(IUserRepository)).To(typef(UserRepository));
        Bind(typeof(IMyContext)).To(typeof(MyContext));
        ...
    }

So I can get this repository in my code like this:

        var repository = DI.Resolve<IUserRepository>();
        List<User> users = repository.GetAll();

And it is working as well

Maris
  • 4,608
  • 6
  • 39
  • 68
  • But, what if `MyContext` object needs to be initialized with some context specific information, and hence it doesn't have a default constructor. `class MyContext { public MyContext(string category){...} }` and the category is known only at the point in the code where we get the repository? That is, Only at that point in your code where you call `DI.Resolve()` ? How can you send the category information to the constructor? – Bhanu Gotluru Mar 01 '13 at 05:45
  • Then use factory pattern! It's build for situations like this. – Maris Mar 01 '13 at 05:49
  • If you dont know how to use it, ask I will give you advance – Maris Mar 01 '13 at 05:52
  • Did you mean that I should use a factory pattern instead of the DI pattern, or use both of them together? – Bhanu Gotluru Mar 01 '13 at 06:38
  • Use both of them together. You get the factory through the DI like ISomeFactory a = Container.Resolve(); ISomeClass b = a.Get("Someparams"); – Maris Mar 01 '13 at 06:56
  • So if you have one implementation of your interface you use DI pattern, if you have many impementations(you use one or another implementation of interface based on some incomming parammeters) you use factory pattern. – Maris Mar 01 '13 at 07:01