Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c# - How to use Autofac to inject specific implementation in constructor

I have two classes that take a ILastActivityUpdator as a constructor parameter: UserService and AnonymousUserService.

public AnonymousUserService(ILastActivityUpdator lastActivityUpdator)
{
    if (lastActivityUpdator == null)
    {
        throw new ArgumentNullException("lastActivityUpdator");
    }
    this.lastActivityUpdator = lastActivityUpdator;
}

And similar as above for UserService:

public UserService(ILastActivityUpdator lastActivityUpdator)
{
    if (lastActivityUpdator == null)
    {
        throw new ArgumentNullException("lastActivityUpdator");
    }
    this.lastActivityUpdator = lastActivityUpdator;
}

ILastActivityUpdator interface has one method: UpdateLastActivity(int userId). There are two implementations of the interface, a LastActivityUpdator and a decorator called AnonymousUserLastActivityUpdator which inherits from LastActivityUpdator and adds some extra functionality to the method, like so:

public class AnonymousUserLastActivityUpdator 
    : LastActivityUpdator, IAnonymousUserLastActivityUpdator
{
    public AnonymousUserLastActivityUpdator()
    { }

    public override void UpdateLastActivity(int userId)
    {
        base.UpdateLastActivity(userId);

        // Extra functionality
    }
}

I now want use Autofac to wire up the AnonymousUserService with the AnonymousUserLastActivityUpdator and the UserService with the LastActivityUpdator.

What I tried is to add an interface for the decorator that derives from the base interface like so:

public interface IAnonymousUserLastActivityUpdator : ILastActivityUpdator
{ }

Then I thought I could use the IAnonymousUserLastActivityUpdator in the AnonymousUserService constructor and everything would be autowired properly.

Unfortunately it just always uses the first implementation, being IAnonymousUserLastActivityUpdator since it is registered earlier (alphabetical order).

How can I accomplish that the AnonymousUserService gets the AnonymousUserLastActivityUpdator injected and the UserService the LastActivityUpdator?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Autofac is nicely documented and it looks like you can find what you are after here. From what I can tell, if you have registered your updators with

builder.RegisterType<LastActivityUpdator>();
builder.RegisterType<AnonymousUserLastActivityUpdator>();

then you should be able to register your services with

builder.Register(c => new UserService(c.Resolve<LastActivityUpdator>()));
builder.Register(c => new AnonymousUserService(c.Resolve<AnonymousUserLastActivityUpdator>()));

or

builder.RegisterType<UserService>().WithParameter(
    (p, c) => p.ParameterType == typeof(ILastActivityUpdator),
    (p, c) => c.Resolve<LastActivityUpdator>());

builder.RegisterType<AnonymousUserService>().WithParameter(
    (p, c) => p.ParameterType == typeof(ILastActivityUpdator),
    (p, c) => c.Resolve<AnonymousUserLastActivityUpdator>());

Then when you resolve UserService or AnonymousUserService from the container, they will get the correct dependencies.

As an aside, if an interface is injected into a class, then that class should function correctly with all implementations of that interface (LSP). From the class names, it looks like AnonymousUserService only works with AnonymousUserLastActivityUpdator and not any implementation of ILastActivityUpdator. If that is the case, then it might be appropriate to introduce a different abstraction (like IAnonymousUserLastActivityUpdator) as you suggested.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...