问题
NInject's module architecture seems useful but I'm worried that it is going to get in a bit of a mess.
How do you organise your modules? Which assembly do you keep them in and how do you decide what wirings go in which module?
回答1:
Each subsystem gets a module. Of course the definition of what warrants categorisation as a 'subsystem' depends...
In some cases, responsibility for some bindings gets pushed up to a higher level as a lower-level subsystem/component is not in a position to make a final authoritative decision - in some cases this can be achieved by passing parameters into the Module.
回答2:
Replying to my own post after a couple of years of using NInject.
Here is how I organise my NInjectModules, using a Book Store as an example:
- BookStoreSolution
- Domain.csproj
- Services.csproj
- CustomerServicesInjectionModule.cs
- PaymentProcessingInjectionModule.cs
- DataAccess.csproj
- CustomerDatabaseInjectionModule.cs
- BookDatabaseInjectionModule.cs
- CustomSecurityFramework.csproj
- CustomSecurityFrameworkInjectionModule.cs
- PublicWebsite.csproj
- PublicWebsiteInjectionModule.cs
- Intranet.csproj
- IntranetInjectionModule.cs
What this is saying is that each project in the system comes prepackaged with one or more NInject modules that know how to setup the bindings for that project's classes.
Most of the time an individual application is not going to want to make significant changes to the default injection modules provided by a project. For example, if I am creating a little WinForm app which needs to import the DataAccess project, normally I am also going to want to have all the project's Repository<> classes bound to their associated IRepository<> interfaces.
At the same time, there is nothing forcing an individual application to use a particular injection module. An application can create its own injection module and ignore the default modules provided by a project that it is importing. In this way the system still remains flexible and decoupled.
来源:https://stackoverflow.com/questions/1699197/how-do-you-organise-your-ninject-modules