0

Iam Using Unity as IOC Container and I Use RegistrationByConvension , The Question is My DbContext has a multiple constructors and i want to make a default constructor when i search i found InjectionConstructor But It using With The regular Register Not with RegistrationByConvension , So How To Make It With RegistrationByConvension ? There is My Code In UnityConfig ? Thanks

container.RegisterTypes(AllClasses.FromAssemblies(Assembly.GetAssembly(typeof(MyDB))),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifeTime.Transient);
  • Why do you want to register that single type `DbContext` using convension based registration? A DbContext is a type that should typically be registered using a factory method, instead of using auto-registration. – Steven Jan 16 '18 at 08:05
  • i didn't want to make a special register for dbcontext ? i ask if there is any way to make it with registrationbyconvension or i have to make special register for DbContext ? – ahmed mohamed Jan 16 '18 at 08:08
  • And If I Make a special register for dbcontext i have to reference entity framework in IOC Container Project and That is wrong ? – ahmed mohamed Jan 16 '18 at 09:05
  • The [Composition Root](http://blog.ploeh.dk/2011/07/28/CompositionRoot/) is in the top most assembly (the entry-point) of your application. It will therefore be the most volatile part of your application and [by definition](https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application) depend on _all_ other assemblies in your application. It makes no sense to prevent this. – Steven Jan 16 '18 at 09:07
  • I didn't make a Composition Root , I will Try it – ahmed mohamed Jan 16 '18 at 09:32
  • i made IOC Project and add BLL and DAL as a references to it ? is it my entry point ? – ahmed mohamed Jan 16 '18 at 09:37
  • No, the Composition Root is the unique location in the _start-up assembly_ of your application. Only the Composition Root should depend on the DI Container. Your BLL and DAL should not reference it. – Steven Jan 16 '18 at 09:56
  • No The IOC Project has BLL and DAL Reference – ahmed mohamed Jan 16 '18 at 11:07
  • There' a great book on this subject that I can highly advise: https://manning.com/seemann2. – Steven Jan 16 '18 at 11:18

0 Answers0