在依赖注入声明门面类 [英] Declaring Facade Class in Dependency Injection

查看:132
本文介绍了在依赖注入声明门面类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用依赖注入。说我有一个OrderService类是这样的:

I am using Dependency Injection. Say that I has an OrderService class like this:

public class OrderService{
    public OrderService(
        IOrderValidator validator
        , IOrderRepository repository
        , IOrderNotificator notificator){
        //assign global fields
    }

    public void SubmitOrder(Order ord){
        if(validator.IsOrderValid(ord)){
            repository.InsertNew(ord);
            notificator.Notify(ord);
        }
    }
}

现在我想创建例如外观类 TypeAOrderService ,由OrderService继承,并在构造函数中声明,如组件:

Now I wonder to create a facade class for example TypeAOrderService, as inherited by OrderService, with components declared in constructor such as:

public class TypeAOrderService : OrderService{
    public TypeAOrderService() : base(
        new OrderValidator(),
        new OrderRepository(),
        new OrderNotificator()) { }
}

(注意,注射成分复杂的实现并不重要,一个适配器模式也可以替换继承虽然)。

(Note that the implementation with injected component complexity is not important here, an adapter pattern also acceptable to replace inheritance though).

有可能是缺点,因为在这里我们没有定义在根组成的依赖性。但是我想知道它是否在某些情况下可以接受的。特别是在框架组件时,这是相当奇怪的通过访问DI容器使用的框架和自己解决问题。

There may be downsides here because we don't define the dependency at composition root. However I wonder whether it is acceptable in some situation. Especially at framework component when it is rather weird to use the framework by accessing the DI Container and resolve it yourself.

更新:

正如评论中提及,目前我没有使用任何IOC容器。我的观点是,这是不可思议的框架使用IOC容器,因为这将意味着,每一个应用程序使用了框架,将需要使用IOC容器。如果我的观点是错误的,随时纠正我。

As mentioned in comment, currently I don't use any IOC container. My perspective is, it is weird to use IOC container in Framework, since it will means that every application uses the framework will need to use IOC container. If my perspective is wrong, feel free to correct me.

什么我指为骨架的例子是,如 System.Windows.Forms.Form中,我不使用任何IOC容器和并不确定的依赖

What I am refering as the framework example is such as System.Windows.Forms.Form where I don't use any IOC container and don't determine the dependency.

推荐答案

关于是否使用了IoC,我认为这是细到一个框架库中使用的IoC。这只是意味着你的框架有其自己的容器和写作的根被完全来自任何消费者的隐患。您可以创建外观类,这样更容易消耗。事情是这样的:

Regarding whether to use IoC, I think it's fine to use IoC within a framework library. It just means your framework has its own Container and Composition Root that are entirely hidden from any of its consumers. You can create Facade classes that make it easy to consume. Something like this:

public class OrderServiceFacade
{
    private readonly IOrderService OrderService;

    public class OrderServiceFacade()
    {
        this.OrderService = ContainerWrapper.Container.Resolve<IOrderService>();
    }

    public void SubmitOrder(Order ord) {
        OrderService.SubmitOrder(ord);
    }
}

在哪里ContainerWrapper是你的根组成,围绕DI容器的包装。

Where ContainerWrapper is your Composition Root, a wrapper around the DI Container.

internal class ContainerWrapper
{
    private Container _Container;
    public Container Container
    {
        if(_Container == null)
        {
            //initialize it
        }
        return _Container;
    }
}

当然,你需要OrderService和TypeAOrderService从一个新的IOrderService接口继承。这也分离出来的TypeAOrderService从OrderService,因为它可以再取接口,在其构造函数,而不是直接实例具体实现。在事件中,你确实需要TypeAOrderService调用OrderService上的方法,就可以使用Decorator模式,并有TypeAOrderService采取IOrderService作为一个额外的依赖。

Of course you would need OrderService and TypeAOrderService to inherit from a new IOrderService interface. This also decouples your TypeAOrderService from your OrderService, as it can then take interfaces in its constructor rather than directly instantiate particular implementations. In the event you actually need TypeAOrderService to call methods on OrderService, you can use the Decorator Pattern and have TypeAOrderService take IOrderService as an additional dependency.

这篇关于在依赖注入声明门面类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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