如何使用 Autofac 在构造函数中注入特定的实现 [英] How to use Autofac to inject specific implementation in constructor

查看:32
本文介绍了如何使用 Autofac 在构造函数中注入特定的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类将 ILastActivityUpdator 作为构造函数参数:UserServiceAnonymousUserService.

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;
}

和上面的 UserService 类似:

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

ILastActivityUpdator 接口有一个方法:UpdateLastActivity(int userId).该接口有两种实现,一个 LastActivityUpdator 和一个名为 AnonymousUserLastActivityUpdator 的装饰器,它继承自 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
    }
}

我现在想使用 Autofac 将 AnonymousUserServiceAnonymousUserLastActivityUpdatorUserServiceLastActivityUpdator 连接起来.

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
{ }

然后我想我可以在 AnonymousUserService 构造函数中使用 IAnonymousUserLastActivityUpdator 并且一切都会正确自动装配.

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

不幸的是,它总是使用第一个实现,即 IAnonymousUserLastActivityUpdator,因为它是较早注册的(按字母顺序).

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

我怎样才能实现 AnonymousUserService 注入 AnonymousUserLastActivityUpdatorUserService 注入 LastActivityUpdator?>

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

推荐答案

Autofac 是 很好的文档 看起来你可以在 此处.据我所知,如果你已经注册了你的更新器

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>()));

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>());

然后当您从容器解析 UserServiceAnonymousUserService 时,它们将获得正确的依赖项.

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

顺便说一句,如果一个接口被注入到一个类中,那么该类应该在该接口的所有实现中都能正常运行(LSP).从类名来看,AnonymousUserService 只适用于 AnonymousUserLastActivityUpdator 而不是 ILastActivityUpdator 的任何实现.如果是这种情况,那么按照您的建议引入不同的抽象(例如 IAnonymousUserLastActivityUpdator)可能是合适的.

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.

这篇关于如何使用 Autofac 在构造函数中注入特定的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆