【问题标题】:WCF Service architecture for multiple services用于多个服务的 WCF 服务架构
【发布时间】:2012-11-19 16:51:31
【问题描述】:

我在这里得到了一些建议,所以我朝着正确的方向前进。

我有一项服务,它在一项服务中完成了我的所有业务功能,例如:

TestingService
      GetProducts()
      GetPeople()

现在,我觉得,随着我的系统不断增长,我想把这些分开,所以:

ProductService
    GetProducts()
PeopleService
    GetPeople()

最好的方法是在同一个项目中拥有两个 .svc 文件并分别调用它们吗?我已经实现了一个运行良好的存储库模式,现在有了 IProductService 和 IPeopleService。如果我要这样做,并且我在 Controller 中使用了这两个构造函数 - 我会得到这样的结果:

public TestController(IProductService productService, IPeopleService service)

而不是

public TestController(ITestService service)

我原来的。

如果我在单个控制器中使用 5 个服务,这会变得笨拙吗?这就是 Factory 类的用途,作为服务级别的包装器吗?

【问题讨论】:

    标签: c# .net wcf design-patterns


    【解决方案1】:

    我认为您希望将服务放在存储库类中,而不是控制器中。像这样的:

    public TestRepository(IProductService productService)  {}
    

    如果您选择您提到的更精细的方法,您将拥有许多服务,而不是一项大型服务 - 好主意。此外,如果您的控制器/存储库/服务之间存在一对一的关系,那么您将拥有良好且可维护的结构。

    但是,如果您的关系是一对多,那么工厂方法当然是一种选择。也许是这样的:

    // Factory
         public class ServiceFactory : IServiceFactory
            {
             public IProductService GetProductService()
                {
                    return new ProductService();
                }
             public IPeopleService GetPeopleService ()
                {
                    return new PeopleService ();
                }
              }
    
    // Repository
        public class ProductRepository
            {
            public void DoSomething()
            {
                // use dependecy injection to avoid this tight coupling
                var factory = new ServiceFactory(); 
                var service = factory.GetProductService();
                service.DoMyStuff();
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2019-03-13
      • 2010-12-28
      相关资源
      最近更新 更多