DI- Dynamic parameter of type Type, where type is the type of parent objects

I have a dependency that I need to inject into one of my classes. This addiction will be a way of life Transient. It in turn has a type dependency Type. This type must be the type of the source class. I'm just wondering if anyone has an idea on how I can register.

See an example:

public interface ICustomer
{
    .....
}

public class Customer : ICustomer
{
    public Customer(IRegister register)
    { .... }
}

public interface IRegister
{
    .....
}

public class Register
{
    public Register(Type partentType)
    { .... }
}

public class TestExample
{
    public static void TestMe()
    {
        //If i was creating all this manually it would look
        //   something like this
        IRegister myRegister = new Register(typeof(Customer));
        ICustomer myCustomer = new Customer(myRegister);
    }
}

Now I know that I can call Container.Resolvewhen I want Customer, and then insert Registermanually. But I need to introduce Registerin most of my classes, so this is not so real. Therefore, I need to do this through configuration or through container.Register.

+3
source share
4

, ,

public interface IRegister<TParent> { ... }
public class Register<TParent> : IRegister<TParent>
{
    public Register() { ... }
}

public class Customer : ICustomer
{
    public Customer(IRegister<Customer> register) { .... }
}

IRegister .

+1

, , , :

public interface IRegister{
    RegisterResult MyMethod(object thing);
}

, , , DI. , ...

0

? ?

, DI , , Windsor, , . , , .

0

, , . , , . ?

, , . , . , , . , , .

, ( ). , , , , , .

Cheers Anthony

-1
source

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


All Articles