开发者

Build better WCF service

开发者 https://www.devze.com 2023-04-12 13:12 出处:网络
I\'m building WCF service and I have a question about WCF service design: For example: If I have a data accass layer with two class Person and Product:

I'm building WCF service and I have a question about WCF service design:

For example:

If I have a data accass layer with two class Person and Product:

public class Person
{开发者_运维百科
  public DataTable Select()
  {...}
}

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

Both class has Select() method. To use these classes in WCF, I used two ways in my previous procjects

1) Create two service class PersonService and ProductService:

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:

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

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.

The idea is to keep things that tend to change separate so changing one won't affect the other.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号