Declaring Facade Class in Dependency Injection

狂风中的少年 提交于 2020-01-05 05:55:44

问题


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

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).

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.

Update:

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.

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.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/16580062/declaring-facade-class-in-dependency-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!