c# - SimpleInjector - Register Object that depends on values from another registered object -


Using SimpleInjector, I am trying to register an institution that relies on values ​​obtained from any other registered entity. For example:

Settings - Settings settings that read that the some other service type app is required.

SomeOtherService - To allow objects to be registered after resolving any object, set a value from Settings to allow some di containers Depends (and so registered).

Then you can do something like pseudo code below:

  Container. Registrar & lt; ISettings, Settings & gt; (); Var settings = container.resolive & lt; ISettings & gt; (); System.TypeTypeWeWantToRegister = Type.GetType (settings.GetTheYouomeOtherServiceType ()); Container. Registration (ISomeOtherService, TheTypeWeWantToRegister);  

Simple inspector does not allow registration after the resolution. Is there a mechanism in the simple insulator that allows the same architecture?

An easy way to get this requirement is to register all the available types that may be required. And the configuration ensures that the container runs at the right time ... it's not so easy to explain in English, so show me.

You can have several implementations of one interface, but at runtime, you want one of them, and the one you want is controlled by a setting in a text file - a string Public class IOneOfMany {} Public class IOneOfMany1: Public class OneOfMany2: IOneOfMany {} Public class GoodSettings: ISettings {public string IWantThisOnePlease {{return "Get OneOfMany2}"; }}

Then proceed and register them all:

  Private Container Container Factor () {var Container = New Container (); Container. Registrar & lt; ISettings, GoodSettings & gt; (); Container.RegisterAll & lt; IOneOfMany & gt; (This.GetAllOfThem (Container)); Container. Registrar & lt; IOneOfMany & gt; (() = & Gt; This. GetTheOneIWant (container)); Return container; } Private IEnumerable & lt; Type & gt; GetAllOfThem (Container Container) {var Type = OpenGenericBatchRegistrationExtensions .GetTypesToRegister (Container, TypeOff (IOneOfMany), AccessibilityAllTypes, typeof (IOneOfMany) .Assembly); Return type; }  

The Magic GetTheOneIWant is in the call - it's a representative and container complete the configuration - There is a logic for representative :

  Private IOneOfMany GetTheOneIWant (container container) {var settings = container.GetInstance & lt; ISettings & gt; (); Var Results = Container .GetAllInstances & lt; IOneOfMany & gt; () .SingleOrDefault (i = & gt; i.GetType () .name == Settings.IWantThisOne please); Return result; }  

A simple test will confirm that it works as expected:

  [test] Public Zero Container_ Registrar All reRurnsTheOneSpecifiedByTheSettings () {var Container = this.ContainerFactory (); Var result = container.gate instance & lt; IOneOfMany & gt; (); Tell me This is the result (no, no.No.); }  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -