Ok, so I read in ninject recently, but I had trouble understanding what makes it better, why they link to the "poor person" DI page on the wiki page. The sad thing is that I looked at all their pages on the wiki and still do not understand = (.
I usually port classes of service to a factory template that processes DI like this:
public static class SomeTypeServiceFactory { public static SomeTypeService GetService() { SomeTypeRepository someTypeRepository = new SomeTypeRepository(); return = new SomeTypeService(someTypeRepository); } }
Which seems very similar to modules to me:
public class WarriorModule : NinjectModule { public override void Load() { Bind<IWeapon>().To<Sword>(); Bind<Samurai>().ToSelf().InSingletonScope(); } }
If each class has a module associated with it, and you bind its constructor to a specific implementation. While ninject code is 1 line, I just don't see this advantage. Anytime you add / remove constructors or change the implementation of an interface constructor, will you have to change the module in much the same way as in the factory? Therefore, we do not see the benefits here.
Then I thought that I could come up with a general factory-based conditional contract as follows:
public static TServiceClass GetService<TServiceClass>() where TServiceClass : class { TServiceClass serviceClass = null; string repositoryName = typeof(TServiceClass).ToString().Replace("Service", "Repository"); Type repositoryType = Type.GetType(repositoryName); if (repositoryType != null) { object repository = Activator.CreateInstance(repositoryType); serviceClass = (TServiceClass)Activator.CreateInstance(typeof (TServiceClass), new[]{repository}); } return serviceClass; }
However, this is crappy for two reasons: 1) it is highly dependent on a naming convention; 2) he suggested that there will never be any constructors in the repository (not true), and that the only repo constructor will be this corresponding repo (also not true). I was told: "Hey, this is where you should use the IoC container, it would be great here!" And so, my research has begun ... but I just don't see it, and it's hard for me to understand this ...
Is there any way that ninject can automatically resolve class constructors without a special declaration, so it would be great to use it in my universal factory (I also understand that I could just do it manually using reflection, but that would hit performance and ninject says right on their page that they donβt use reflection).
Read this problem and / or show how it can be used in my general factory!
EDIT: answer
Thus, thanks to the explanation below, I skillfully fully understood the awesomeness of ninject, and my overall factory is as follows:
public static class EntityServiceFactory { public static TServiceClass GetService<TServiceClass>() where TServiceClass : class { IKernel kernel = new StandardKernel(); return kernel.Get<TServiceClass>(); } }
Pretty awesome. Everything is handled automatically, since specific classes have implicit binding.