[英]Best Practice on .NET BCL Method
在以下情况下需要一些建议
我有一个子方法,该子方法可通过编程设置绑定信息和终结点信息后初始化服务客户端实例。 拥有实例的变量是在类级别定义的,子方法只是将其设置为新实例。 在代码审查会议中,开发人员建议我们将实例作为子方法的参数传递,并将该参数返回给main方法。 Whis是做到这一点的最佳方法
private void InstantiateClient()
{
//do some configurations on bindings and endpoint
_ClassLevelInstanceClient = new ServiceClient(bindingInfo, endpointInfo);
}
要么
private ServiceClient InstantiateClient(ServiceClient myClientInstance)
{
//do some configurations on bindings and endpoint
myClientInstance = new ServiceClient(bindingInfo, endpointInfo);
return myClientInstance;
}
我想我们需要更多有关您的应用程序的知识,以便提供适当的解决方案。 最好的方法是从有关开发商那里找到有关评论的理由。 我个人不同意。 尽管这里我可能会做些小事。
在父/调用方法中:
using(ServiceClient client = InstantiateClient()){
//Make service call here
}
在子方法中,
private ServiceClient InstantiateClient()
{
//do some configurations on bindings and endpoint
return new ServiceClient(bindingInfo, endpointInfo);
}
同样,如果在您的应用程序中有意义,则可以将InstantiateClient设为通用方法,如下所示:
private ClientBase<T> InstantiateClient(){
// create and return specific client here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.