Build a chain of dependencies with arguments at the root

I the following chain of dependencies:

Handler() [depends on]--> Repository(string connectionString)

So, I have an IHandler, which depends on the IRepository, which in turn requires a connection string. The connection string is dynamic and passed at runtime (therefore, it cannot be read from the configuration file, etc.)

Imagine that the system creates a handler with the following code:

var handler = ObjectFactory.GetInstance<IHandler>();

This fails because the Repository (connectionString) dependency cannot be satisfied. My next idea was to use StructureMap ExplicitArguments to provide arguments at the beginning of building a dependency chain, i.e.:

var arguments = new ExplicitArguments();
arguments.SetArg("connectionString", "SOME CONNECTION STRING");

var handler = ObjectFactory.GetInstance<IHandler>(arguments);

, StructureMap connectionString ( , ).

: , StructureMap , connectionString?

+3
2

Repository, , IConnectionStringProvider , factory.

+1
container.Configure(r => r
    .ForConcreteType<Repository>()
    .Configure.Ctor<string>().Is("some connection string"));
+3

Source: https://habr.com/ru/post/1794767/


All Articles