Entering Typical Type Parameters with AutoFac

I think I'm really confused about what I can do with AutoFac, can someone please put me in the way.

I have a base type

class PersonBase{ 
    public string SaySomething(){
       return "I am base";
    }
}

I get two specific classes

class FakePerson : PersonBase{
    public override string SaySomething(){
       return "I'm so Fake";
    }
}

class RealPerson : PersonBase{
    public override string SaySomething(){
       return "I am For Real";
    }
}

Create a generic PersonHandler class to deal with different types of people and would like PersonHandler to instantiate the person at the appropriate time, so I don’t want the Person instance to be introduced, just need to get the derived type

class PersonHandler<T>
    where T : PersonBase, new() {

    T _Person;    

    public DoWork(){
        _Person = new T();
        _Person.SaySomething();
    }
}

Now I am trying to use a handler after registering types, as described below, with different results.

var ph = contrainer.Resolve<PersonHandler<PersonBase>>();
ph.DoWork();

I tried registering types as follows

1. vBuilder.RegisterType<PersonHandler<FakePerson>>().As<PersonHandler<PersonBase>>();

This gives me an error indicating that it is PersonHandler<FakePerson>not assigned PersonHandler<PersonBase>(or vice versa, I do not repeat which one)

2. vBuilder.RegisterGeneric<typeof(PersonHandler<>)>
   vBuilder.RegisterType<FakePerson>().As<PersonBase>();

PersonBase FakePerson, PersonHandler<PersonBase>, " "

3. vBuilder.RegisterGeneric(typeof(PersonHandler<FakePerson>)).As(typeof(PersonHandler<PersonBase>));

, , PersonHandler<FakePerson>

, , , ,

,

+3
2

() - , :

builder.RegisterType<PersonHandler<FakePerson>>()
    .As<PersonHandler<PersonBase>>();

, Autofac , , # .

:

PersonHandler<PersonBase> ph = new PersonHandler<FakePerson>();

( IDE - .)

, , , # 4, .

, IPersonHandler<T> , , :

interface IPersonHandler<in T>
    where T : PersonBase, new() {

    void DoWork();
}

in .

PersonHandler<T> IPersonHandler<T>:

class PersonHandler<T> : IPersonHandler<T>
    where T : PersonBase, new() {

:

builder.RegisterType<PersonHandler<FakePerson>>()
    .As<IPersonHandler<PersonBase>>();

, :

IPersonHandler<PersonBase> handler =
    container.Resolve<IPersonHandler<PersonBase>>();

handler PersonHandler<FakePerson>.

+10

, (, PersonHandler<>), , , Autofac .

container.Resolve<PersonHandler<PersonBase>>(), PersonHandler<PersonBase>. , PersonHandler PersonBase T, DoWork :

public DoWork(){
    _Person = new PersonBase();
    _Person.SaySomething();
}

, SaySomething() .

, , , , PersonBase . , Factory Autofac:

class PersonHandler
{
    private readonly Func<PersonBase> createPerson;

    public PersonHandler(Func<PersonBase> createPerson)
    {
        this.createPerson = createPerson;
    }

    public void DoWork()
    {
        PersonBase pb = this.createPerson();
        Console.WriteLine(pb.SaySomething());
    }
}

:

ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<RealPerson>().As<PersonBase>();
builder.RegisterType<PersonHandler>();
var context = builder.Build();
var handler = context.Resolve<PersonHandler>();
handler.DoWork();

, PersonBase, .

0

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


All Articles