如何使用Autofac注入在构造具体的实施 [英] How to use Autofac to inject specific implementation in constructor

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

问题描述

我有采取 ILastActivityUpdator 作为构造函数的参数两大类: UserService AnonymousUserService

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用户id)。有接口,一个 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要连接的 AnonymousUserService AnonymousUserLastActivityUpdator UserService LastActivityUpdator

我试过是增加一个接口,从像这样的基本接口派生装饰:

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

public interface IAnonymousUserLastActivityUpdator : ILastActivityUpdator
{ }

然后我想我可以使用 IAnonymousUserLastActivityUpdator AnonymousUserService 构造,一切都将正常自动装配。

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 得到了 AnonymousUserLastActivityUpdator 注入和 UserService LastActivityUpdator

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

推荐答案

Autofac是很好地记录,它看起来像你可以找到你所追求的此处。从我可以告诉,如果您已经拥有注册您的updators

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

然后,当你解决 UserService AnonymousUserService 从容器中,他们将得到正确的依赖关系。

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天全站免登陆