简体   繁体   English

如何在WCF中使用存储库模式进行CRUD操作并避免代码重复

[英]How to use repository pattern with WCF for CRUD operations and avoid code duplication

I have a service that exposes some CRUD operations through a WCF interface. 我有一个通过WCF接口公开一些CRUD操作的服务。

Service: 服务:

bool SavePerson(Person obj)
{
  Repository<Person> currentRepo = new Repository<Person>();
  currentRepo.Save(obj);
}
bool SaveAddress(Address obj)
{
  Repository<Address> currentRepo = new Repository<Address>();
  currentRepo.Save(obj);
}

with the interface definitions for each 每个接口的定义

[OperationContract()]
bool SavePerson(Person obj);
[OperationContract()]
bool SaveAddress(Address obj);

I have a web client that consumes the methods. 我有一个使用方法的Web客户端。

Web: 网页:

SaveAddress(addr);
SavePerson(pers);

After browsing the site a bit, I know that generic methods don't work over WCF, so the Web can't do something like SaveObject<Address>(addr); SaveObject<Person>(pers); 稍微浏览一下站点后,我知道通用方法无法在WCF上运行,因此Web无法执行SaveObject<Address>(addr); SaveObject<Person>(pers); SaveObject<Address>(addr); SaveObject<Person>(pers);

My question is, is there a way to reduce the amount of code duplication? 我的问题是,有没有办法减少代码重复量? Ideally, I'd like to reduce the size of the WCF interface. 理想情况下,我想减小WCF接口的大小。 Even if that's not doable though, would I at least be able to combine the method implementations on the service side? 即使那不是可行的,我是否至少能够在服务端结合方法实现?

Thanks 谢谢

REST WCF services should help you. REST WCF服务应为您提供帮助。 They will not implement exactly what you want but it is the only thing that can reduce amount interfaces and copy-paste. 它们不会完全实现您想要的功能,但这是唯一可以减少接口数量和复制粘贴的内容。

http://msdn.microsoft.com/library/dd203052.aspx http://msdn.microsoft.com/library/dd203052.aspx

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

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