我们正在开发一个CRM应用程序。所有业务逻辑和数据访问都通过WCF服务进行。我们在WCF和客户端之间有两个通信选项(目前: ASP.NET MVC 2)
一个选项是为每个操作创建方法。示例
GetCustomers()
GetCustomersWithPaging(int take, int skip)
GetCustomersWithFilter(string filter)
GetCustomersWithFilterPaging(string filter, int take, int skip)
or // new .net 4 feature
GetCustomers(string filter = null, int take = 0, int skip = 0)
... list goes..另一个选项是创建一个通用的单个服务操作,名为
Response InvokeMessage(Messege请求)。示例
wcfservice.InvokeMessage(
new CustomerRequest {
Filter = "google",
Paging = new Page { take = 20, skip = 5}
});
// Service Implementation.
public Response InvokeMessage(Message request)
{
return request.InvokeMessage();
}InvokeMessage =用于所有操作的通用单个服务调用。
CustomerRequest =从 Message 抽象类继承的类,因此我可以根据输入需求从消息基类创建多个类。这是CustomerRequest类。
public class CustomerRequest : Message
{
public string Filter {get;set;}
public Page Paging {get;set} // Paging class not shown here.
Public override Response InvokeMessage()
{
// business logic and data access
}
} 编辑
public abstract class Message
{
public abstract Response InvokeMessage();
}//所有服务调用将仅通过InvokeMessage方法,但具有不同的消息请求。
基本上,我可以避免每次服务调用和代理关闭等等。这种方法的一个直接含义是,我不能将此服务用作REST。
如果服务需要调用很多方法,那么推荐的方法是什么?
谢谢。
发布于 2010-10-12 21:34:04
如果您使用外观模式,您可以两者兼得。
首先,使用第一个选项构建服务。这允许您有一个REST接口。如果需要,可以在外部使用。
然后,您可以创建一个使用Invoke消息样式的facade,这将根据参数转换请求,并调用第一步中创建的单个服务之一。
发布于 2010-10-12 21:51:47
对于多个特定操作与一个通用查询的问题,这两种方法都可能是有效的;定义细粒度和粗粒度操作在某种程度上是一个品味问题。
在我们的RESTful服务中遇到类似的需求时,我选择创建一个过滤器类,它从查询字符串中读取参数,这是可用的:
public NameValueCollection ReadQuerystring()
{
return WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;
}我在这里看到的更大的问题是,您正在对操作参数的消息进行子类化--有什么原因要这样做吗?最佳实践是为此目的创建数据契约(带有DataContract属性注释的对象)。
https://stackoverflow.com/questions/3918726
复制相似问题