[英]WCF Service architecture for multiple services
I'm after a bit of advice here, so I go in the right direction.我在这里寻求一些建议,所以我朝着正确的方向前进。
I had one service, which did all my business functions in one service, such as:我有一项服务,它在一项服务中完成了我的所有业务功能,例如:
TestingService
GetProducts()
GetPeople()
Now, I feel, as my system is growing, I want to split these out, so:现在,我觉得,随着我的系统不断发展,我想将它们分开,所以:
ProductService
GetProducts()
PeopleService
GetPeople()
Is the best approach for this to have two .svc files in the same project and call them each individually?在同一个项目中有两个 .svc 文件并分别调用它们是最好的方法吗? I've implemented a repository pattern which is working nicely, and now have IProductService and IPeopleService.
我已经实现了一个运行良好的存储库模式,现在有 IProductService 和 IPeopleService。 If I was to do this, and I used both constructors in my Controller - I'd get something like this:
如果我要这样做,并且我在控制器中使用了两个构造函数 - 我会得到这样的东西:
public TestController(IProductService productService, IPeopleService service)
rather than而不是
public TestController(ITestService service)
which I had originally.我原来有的。
Which could become unwieldy if I'm using 5 services in a single Controller?如果我在单个控制器中使用 5 个服务,这会变得笨拙吗? Is this what Factory classes are used for, as a Service-level wrapper?
这是工厂类用作服务级别包装器的用途吗?
I think you'll want to have your services in repository classes not the controllers.我认为您希望在存储库类而不是控制器中拥有您的服务。 Something like this:
像这样的东西:
public TestRepository(IProductService productService) {}
If you opt for the more granular approach that you mention, you'll have many services instead of one large one - good idea.如果您选择您提到的更细粒度的方法,您将拥有许多服务而不是一个大型服务 - 好主意。 Also, if you have a 1 to 1 to 1 relationship among your controller/repository/service, then you'll have nice and maintainable structure.
此外,如果您的控制器/存储库/服务之间存在 1 对 1 对 1 的关系,那么您将拥有良好且可维护的结构。
However, if your relationship is 1 to many, then a factory approach is certainly an option.但是,如果您的关系是一对多的,那么工厂方法当然是一种选择。 Maybe something along these lines:
也许是这样的:
// 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.