IoC and managing interfaces

2019-05-21 09:40发布

问题:

Say I had a business object library which was using IoC to implement a data access library. Where should I define the Data Access Interface? Which library does it belong? Or should it be in a separate library just for interfaces?

回答1:

I would define the interfaces within the business domain. Then the implementations of the interfaces would be in a library that references the business domain (and is referenced by whatever the application context is, or by an IoC library which is referenced by the application context).

Then swapping out one implementation with another is just a matter of creating another library and swapping the reference in the application context.

In a .NET project structure it would look something like this:

Domain Logic Project
    (references nothing)
    Domain Models
    Repository Interfaces
    IoC Service Locator Interface
Repository Project
    (References Domain Logic Project)
    Repository Implementations
IoC Project
    (References Domain Logic Project)
    (References Repository Project)
    IoC Service Locator Implementation
    IoC Bootstrapping
Application Project
    (References IoC Project)
    (References Domain Logic Project)
    (May need to reference Repository Project, not sure)
    Implements UI which interacts with Domain Models