楼梯模式实现 [英] Stairway pattern implementation

查看:212
本文介绍了楼梯模式实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在碰到楼梯模式描述自适应code通过C#的书,我真的不明白这是怎么应该执行:

I came across "Stairway" pattern description in the "Adaptive code via C#" book and I don't really understand how this is supposed to be implemented:

(<一href="https://books.google.ru/books?id=EJ_HBAAAQBAJ&pg=PT132&lpg=PT132&dq=adaptive%20$c$c%20via%20c%23%20stairway&source=bl&ots=19JCJCRdvR&sig=d5cpH0gMrSuKSuzt19-vqfMTxeM&hl=en&sa=X&ei=7bgSVfq2NcHhywOclIH4BA&ved=0CBwQ6AEwAA#v=onepage&q=adaptive%20$c$c%20via%20c%23%20stairway&f=false">source)

所以,我有客户端组件:

So I have client assembly:

using ServiceInterface;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            // Have to create service implementation somehow
            // Where does ServiceFactory belong?
            ServiceFactory serviceFactory = new ServiceFactory();
            IService service = serviceFactory.CreateService();
            service.Do();
        }
    }
}

服务接口组件:

Service interface assembly:

namespace Service
{
    public interface IService
    {
        void Do();
    }
}

和服务的实现组件:

using ServiceInterface;

namespace ServiceImplementation
{
    public class PrintService : IService
    {
        public void Do()
        {
            Console.WriteLine("Some work done");
        }
    }
}

和问题是:如何将我的客户端命名空间得到 IService 对象?我在什么地方把实际的新的PrintService()创建对象?这不可能是 serviceInterface等的一部分,因为接口组件不依赖于 ServiceImplementation 。但它也不可能是客户端的一部分 ServiceImplementation ,因为客户端应该只依赖于 serviceInterface等

And the question is: how to I get an IService object in the Client namespace? Where shall I place actual new PrintService() object creation? This can't be a part of ServiceInterface, because interface assembly doesn't depend on ServiceImplementation. But it also can't be a part of Client or ServiceImplementation because Client should only depend on ServiceInterface.

我是唯一的解决办法是有应用程序组件之上的它,它引用了三个(客户端 serviceInterface等 ServiceImplementation ),并注入 IService 客户端。我失去了一些东西?

The only solution I came to is having Application assembly on top of it, which has references to all three (Client, ServiceInterface and ServiceImplementation) and injects IService into Client. Am I missing something?

推荐答案

应用程序入口点应该是构成根,按照马克·塞曼的优秀图书的依赖注入。在这里,问题更多的是依赖倒置,即客户端和执行都应该依赖于抽象。

Application entry points should be the composition root, as per Mark Seemann's excellent book on Dependency Injection. Here, the issue is more about Dependency Inversion, whereby both client and implementation should both depend on abstractions.

这也就图没有显示,但是这本书的希望其他地方明确,就是切入点将必然地引用的所有的是需要构建什么是你的分辨率根(控制器,服务等),但是,这是的只有的地方,有这样的知识。

What that diagram doesn't show, but hopefully other parts of the book make clear, is that the entry point will naturally and necessarily reference everything that is needed to construct whatever are your resolution roots (controllers, services, etc.) But this is the only place that has such knowledge.

记住,有时好,为客户拥有的接口所依赖:接口 ISecurityService 可能生活在控制器总成, IUserRepository 可能生活在 ServiceImplementations 组装等。这是不切实际的,当> 1的客户需要,当然访问接口。

Bear in mind that it is sometimes ok for clients to 'own' the interfaces on which they depend: the interface ISecurityService might live in the Controllers assembly, the IUserRepository might live in the ServiceImplementations assembly, and so on. This is impractical when >1 client needs access to the interface, of course.

如果你遵循SOLID,你自然会发现,依赖注入是必要的,但控制容器的反转少了优先级。我发现自己使用纯依赖注入(解决根的人工建筑)越来越频繁。

If you follow SOLID, you will naturally find that Dependency Injection is a necessity, but Inversion of Control containers are less of a priority. I find myself using Pure Dependency Injection (manual construction of resolution roots) more and more often.

这篇关于楼梯模式实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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