这是更好的PARAMS或列表 [英] Which is better Params or List

查看:152
本文介绍了这是更好的PARAMS或列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的code为以下,这是暴露给客户端的代理WCF服务的方法:

My current code is following, it is a WCF service method exposed as a proxy to the client:

public UnifiedDTO GetAllCardTitle(string trainSymbolOrCarLocation, 
                                  DateTime startDate, 
                                  DateTime endDate, 
                                  string procedureName = CardTitle.procedureNameTrainRuns)

此方法需要一个过程名称(作为最后一个参数)和参数的其余都是结合的输入参数,然而问题是,我们没有在参数的数量和类型改变的情况下的灵活性。该项目是处于起步阶段,因此改变一定会进行。

This method takes a procedure name (as the last parameter) and rest of the parameters are the binding the input parameters, however the issue is that we do not have the flexibility in the case of parameters numbers and types changing. The project is in its initial stage, so changes will surely be made.

这是我的选择是转换方法如下:

The options that I have is convert the method as follows:

public UnifiedDTO GetAllCardTitle(params object [] parameters)

在哪里,我们必须通过参数和过程的详细信息的自由,可以相应约束。但是,有可能是一个性能问题,由于装箱和拆箱。这需要客户端应用程序传递正确的顺序和值的参数绑定,因为它会预计到基础层

Where we have the freedom to pass the parameters and procedure details and can accordingly bind. However, there might be a performance issue due to boxing and unboxing. It would require client application to pass the parameters with correct order and value to bind as it would be expected by underlying layers

public UnifiedDTO GetAllCardTitle(List<Filter> parameter, string procedureName)

其中,参数类将被定义为:

public class Filter
{
    public string name { set; get; }
    public string value { set; get; }
    public string dataTypeID { set; get; }

    public Filter(string _name, string _value, string _dataTypeID)
    {
        name = _name;
        value = _value;
        dataTypeID = _dataTypeID;
    }
}

在此方法对于给定的过程中,我们每个参数绑定其名称值和数据类型,它需要的价值类型强制转换为正确的数据类型,它有更多的灵活性然后最后一个方法,并且可以以任何顺序来传递,如结合是通过名称。但是,它会从应用程序需要更多的尽职调查。

In this method for a given procedure, we bind each parameter with its name value and DataType, and it would need value to typecasted to the correct data type, it has more flexibility then last method and can be passed in any order, as binding is by name. However, it would need much more due diligence from the application.

是否还有更好的方式使用一些新的东西在C#.NET推出来照顾这个情况?

Is there still a better way to take care of this situation using something new introduced in C#.Net?

推荐答案

是否有可能让一个接口参数。从这一点,你可以处理基于该接口的值设置多件事情。刚刚拍摄了一个简单的示例。

Is it possible to allow an "Interfaced" parameter. From that, you could handle multiple things based on the interfaced value setting. Just shooting out a simple sample.

public enum eWhatAmI
{
   ListedObjects,
   StringArrays,
   Other   
}

public interface IWhatParmType
{
   eWhatAmI whatAmI { get; set; }
}

public class MyListVersion : IWhatParmType
{
   public eWhatAmI whatAmI { get; set; }
   public List<string> whatever { get; set; }

   public MyListVersion()
   {
      whatAmI = eWhatAmI.ListedObjects;
      whatever = new List<string>();
      ... build out list of strings
   }
}

public class MyArrayVersion : IWhatParmType
{
   public eWhatAmI whatAmI { get; set; }
   public string[] whatever { get; set; }

   public MyArrayVersion()
   {
      whatAmI = eWhatAmI.StringArrays;
      ... build out array of strings
   }
}

等等...

etc...

然后在你的程序来处理任何传入的参数,你可以处理任何一种方式。

Then in your process for handling whatever the incoming parameter is, you can handle either way.

public UnifiedDTO GetAllCardTitle(IWhatParmType parameter, string procedureName)
{
   switch( parameter )
   {
      case (eWhatAmI.ListedObjects):
         // Just for grins, test to make sure object really IS expected list version object
         if( parameter is MyListVersion)
            DoViaList( (MyListVersion)parameter );
         break;

      case (eWhatAmI.StringArrays ):
         if( parameter is MyArrayVersion )
            DoViaArray( (MyArrayVersion)parameter );
         break;
   }
}

private void DoViaList( MyListVersion parm1 )
{
   .. do whatever based on the "List<string>" property
}

private void DoViaArray( MyArrayVersion parm1 )
{
   .. do whatever based on the "string []" property
}

然后,如果你需要扩大每一个特定的对象实例的设置,你可以和具体的子处理方法用于填充或者强迫任何违约所隐含内办理。

Then, if you ever needed to expand a setting per a particular object instance, you could and handle within the specific sub-handler method for populating or forcing whatever defaults to be implied.

这篇关于这是更好的PARAMS或列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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