Writing a library that is turned on,

I am currently working on a project, and it will be mainly based on the library.

I want the library to be consumed using dependency injection, but I want the library to be largely agnostic to the container used.

I wrote a “bridge” library some time ago to make it easier, but I was not sure if this was the correct approach? (library: https://github.com/clintkpearson/IoCBridge )

I do not want to refer to the DI technology (Ninject, Windsor, etc.) directly from my library, and then makes it inflexible for people using it.

There are a few more questions on SO in a similar vein, but none of them seem to satisfy the problem satisfactorily.

As an additional note : I understand that I can just make sure that the library adheres to a common idiom and uses the ctor interfaces and arguments for dependencies, and then just leave it to the consumer application to register types in containers.

The only problem I see with this (and correct me if I am wrong) is that this requires a consumer application, actually know , that defines a link to which interfaces are registered as singleton, etc. .. and in terms of using plug-and-play, which are pretty poor.

+3
source
3

DI DI , , DI- .

- , , (2 ), , DI-, [Injection]. , DI, Spring Framework ( DI Java, . NET, Spring.NET), DI - xml , . , dll .

Spring , - , , , , , .

, DI CLR (.NET), a.k.a. POCO-s. DI- . DI , , , (, , ) .

+2

, . , , ( , Service Locator) . , libray IoC. .

. .

internal SitemapProvider(IActionResultFactory actionResultFactory, IBaseUrlProvider baseUrlProvider)
{
    _actionResultFactory = actionResultFactory;
    _baseUrlProvider = baseUrlProvider;
}

public SitemapProvider() : this(new ActionResultFactory(), new BaseUrlProvider()) { }

, , . . , InternalsVisibleTo . IoC.

, IoC .NET. , , , . .

+2

, Injection Dependency. DI - , IoC IoC DI - . IoC , DI .

IoC, .

IoC DI (.. ), /, , . . , , .

, ,

To achieve this, you do not need an IoC infrastructure. If your designers have their own parameters, defined as interfaces, then, in fact, you have already reached DI - the dependency is introduced during construction, and you do not know anything about the actual concrete implementation. Let the caller worry about the detailed details about which implementation of this interface he wants to pass.

0
source

All Articles