好从一个门面招牌返回与所有重要的返回值的通用对象? [英] Is good to return a generic object with all important returning values from a facade signature?

查看:147
本文介绍了好从一个门面招牌返回与所有重要的返回值的通用对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我问<一个href=\"http://stackoverflow.com/questions/4539186/how-has-to-be-written-the-signature-in-facade-pattern\">How已被写入在外观模式问题的签名?,我想过如何创建一个具有既实用,高效的API签名(及美学很好的解决方案!)。我看到在顶部的一些API及其边界接口暴露签字的下列方式:

Since I asked How has to be written the signature in facade pattern? question, I've thought about how to create a signature for an API that has to be both useful and efficient (and a aesthetically nice solution!) . I've seen some APIs and their boundary interfaces at the top expose the following style of signature:

public List<InterestingDTO> 
    ANecessaryMethodToCompleteABusinessProcess(int aParameter, 
    InterestingDTO aSecondParamenter)

在这种风格,业务规则的侵犯和其他正常/非正常业务的情况下不得不使用特定异常为此设计的签名或采用类似返回空值声明在方法的执行结束的情况的一些约定进行报告。

In this style, business rules violations and other normal/abnormal business situations had to be reported using an specific exception designed for this signature or adopting some convention like returning nulls to state the situation at the end of method's execution.

我认为要使用异常,显示业务问题可能会导致可维护性的问题,那肯定是不好的做法(有一堆技术书目争论这一点)。所以,应付这种问题,我建议使用一个结构或类是这样的:

I think that to use exceptions to show business problems can lead to maintainability problems and it surely is a bad practice (there is a bunch of technical bibliography arguing about this). So, to cope with this problems I suggest to use an structure or a class like this:

public class ReturnValue<T>
{
    public T returnedValue;
    public string message;
    public Status status;    
}

enum Status {SUCCESS, FAILURE, ANY_OTHER_STATUS}; 

那么前者签名可以这样写:

the former signature can then be written like:

 public ReturnValue<List<InterestingDTO>> 
        ANecessaryMethodToCompleteABusinessProcess(int aParameter, 
        InterestingDTO aSecondParamenter)

凡任何消费层的所有有趣的事情可以知道,至少,高效。请注意,有没有例外流量控制(除了可能是那些你想要外层就知道了),和业务层可以对企业的错误消息的整个控制。你认为这种做法有任何瑕疵?

Where all interesting things for any consuming layers can be known, at least, efficiently. Notice that there are not exceptions to control flow (except probably for those you want outer layers to know), and business layer can have the entire control about business error messages. Do you think this approach has any flaw?

请,如果可能的话,添加一些参考书目的回答。

Please, if possible, add some bibliography for your answer.

推荐答案

我们$我们整个企业应用p $ ptty多大用处一样,用两个加法,即1)交易服务,额外的列表与LT;>包含一个属性验证结果列表中每个模型的单一业务规则或违反的规则,然后可以报告给客户端(用户或服务消费者)提供尽可能多的上下文信息成为可能,和2)的数据服务,我们的添加的寻呼信息,表示多少总数据如何提供给客户机(因为我们只允许返回的行有限数目)。这允许客户端绑成一个分页策略。

We pretty much use the same throughout our enterprise apps, with two additions, viz 1) for transactional services, an additional List<> property containing a list of "Validation Results", each of which models a single business rule or validation rule violation, which can then be reported back to the client (user or service consumer) with as much context information as possible, and 2) for data services we add paging information, indicating how much total data is available to the client (given that we only allow a finite number of rows to be returned). This allows the client to tie into a pagination strategy.

唯一的抱怨迄今为止是服务消费者 - 当我们接触的服务方法返回整个企业(ESB / SOA)类型化通用的,是仿制药的WSDL /命名可能会很麻烦(例如ReturnResultOfPOUpdateRequestJQ3KogUE)。这是没有太大的关注.NET客户端,如果我们共享客户端和服务的实体,但对于其他客户,如Java,移动等可能会产生问题,有时我们需要这样的客户不提供一种替代门面泛型。

The only complaint thus far is for Service Consumers - when we exposed service methods returning the typed generic across the enterprise (ESB / SOA), is that the WSDL / naming of the generics can be cumbersome (e.g. ReturnResultOfPOUpdateRequestJQ3KogUE). This isn't of much concern to .NET clients if we share the Entities on both client and service, but for other clients such as Java, Mobile etc can be problematic, and sometimes we need to provide an alternative facade for such clients without the generics.

这篇关于好从一个门面招牌返回与所有重要的返回值的通用对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆