简体   繁体   English

构建更好的WCF服务

[英]Build better WCF service

I'm building WCF service and I have a question about WCF service design: 我正在构建WCF服务,我对WCF服务设计有疑问:

For example: 例如:

If I have a data accass layer with two class Person and Product : 如果我有一个包含两个类PersonProduct的数据accass图层:

public class Person
{
  public DataTable Select()
  {...}
}

public class Product
{
  public DataTable Select()
  {...}
}

Both class has Select() method. 这两个类都有Select()方法。 To use these classes in WCF, I used two ways in my previous procjects 要在WCF中使用这些类,我在之前的项目中使用了两种方法

1) Create two service class PersonService and ProductService : 1)创建两个服务类PersonServiceProductService

public class PersonService : IPersonService
{ 
   public DataTable Select()
   {
     Person person = new Person();
     return person.Select();
   }
}

public class ProductService : IProductService
{ 
   public DataTable Select()
   {
     Product product = new Product();
     return product.Select();
   }
}

In this case, I have to create/configure service classes separately. 在这种情况下,我必须单独创建/配置服务类。

2) Create one service class and use different names: 2)创建一个服务类并使用不同的名称:

public class MyService : IMyService
{ 
   public DataTable PersonSelect()
   {
     Person person = new Person();
     return person.Select();
   }

   public DataTable ProductSelect()
   {
     Product product = new Product();
     return product.Select();
   }
}

In this case I have to create/configure only one service class. 在这种情况下,我必须只创建/配置一个服务类。 But methods has larger names (for example: PersonSelect() instead of Select() ) 但是方法有更大的名称(例如: PersonSelect()而不是Select()

Which is the better way? 哪种方式更好? and why? 为什么?

Thanks. 谢谢。

First of all, it is not ideal to return DataTable/DataSet from a service, but to answer your questions according to Single-Responsibility principle, a class should be doing only one thing, so if ProductSelect and PersonSelect seem to be related to one thing and one thing only, keep them together, otherwise it should be separated. 首先,从服务返回DataTable / DataSet并不理想,但是根据Single-Responsibility原则回答你的问题,一个类应该只做一件事,所以如果ProductSelect和PersonSelect似乎只与一件事有关只有一件事,将它们放在一起,否则它应该分开。

The idea is to keep things that tend to change separate so changing one won't affect the other. 我们的想法是保持容易分开的东西,这样改变的东西不会影响另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM