Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SimpleInjector in a multi project solution without creating circular references

I’m starting a multi project solution that will have more than one entry point, for example a Windows Service, ASP.NET web sites, WebApi Controllers etc. I’ve settled on SimpleInjector as it’s very fast and I do not need any advanced features.

My understanding is that SimpleInjector should be centrally configured on start-up. Starting with the following basic example set of projects

  • NS.Controllers
  • NS.Core.Data
  • NS.Core.Data.Model
  • NS.Web
  • NS.WindowsService (assume this will not always be running)

With multiple entry points where should the bootstrapping of SimpleInjector go and can/should it be handled centrally (in which case the configuration process would need to reference all projects to be able to set up all solution classes)?

Should I have a global instance (such as NS.Global.Container) that references none of the other projects and each entry point is responsible for adding its own instance requirements on startup (gracefully handling duplicate registrations such as NS.Core.Model)?

Should I be looking to use the ResolveUnregisteredType event to handle registrations on request?

Am I simply lacking some schoolboy knowledge?

UPDATED:

Links provided by Steven in the comments below give thorough answers to this question.

Where to locate Ninject modules in a multi-tier application

How to initialize Ninject in a class project part of an mvc site

like image 497
qujck Avatar asked Nov 27 '12 13:11

qujck


1 Answers

Why not using IPackage interface? You can make packages in each module and then in each entry point just call container.RegisterPackages();. For example, all configuration for NS.Core.Data will be located in NS.Core.Data.dll and so on. Then, it will be useable in both NS.Web and NS.WindowsService. In NS.Web just configure types that are specified to it and then call container.RegisterPackages(); and so on. Your NS.Core.Data package may looks like this:

public class CoreDataPackage : IPackage {
    public void RegisterServices(Container container) {
        container.Register<ISomeService,SomeImplementation>();
        // etc...
    }
}
like image 119
amiry jd Avatar answered Sep 21 '22 14:09

amiry jd