我的 WCF 方法看起来像这样:
public GetRecordsResponse GetRecords(GetRecordsRequest request)
{
GetRecordsResponse response = new GetRecordsResponse();
Util.GetRecords.VerifyMandatoryParameters(request);
//make sure mandatory parameters have correct values.
Util.GetRecords.SetDefaultValues(request);
Util.GetRecords.SetDefaultResponseValues(request, response);
DataReader.GetRecords.GetAllRecords(request, response);
return response;
}
我将整个 DAL 和许多 Helper 方法作为静态类和方法是错误的吗?如果是这样,为什么?
最佳答案
由于使用静态方法,您可以访问属性和字段等实例变量,这意味着您需要将请求传递给每个方法。
如果您使用 GetRecords 实例,您可以做一些事情
GetRecords gr = new gr();
gr.Request = request;
gr.VerifyMandatoryParameters();
gr.SetDefaultValues();
gr.SetDefaultResponseValues(response);
gr.GetAllRecords();
此外,如果您实现流畅的界面,您可以像这样编写它。
GetRecords gr = new gr();
gr.SetRequest(request)
.VerifyMandatoryParameters()
.SetDefaultValues()
.SetDefaultResponseValues(response)
.GetAllRecords();
但是你所做的并没有什么“错误”
关于c# - 在这种情况下,静态或对象哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588717/