C# 多参数DAL方法

C# 多参数DAL方法,c#,methods,parameters,C#,Methods,Parameters,我的DAL中有几种方法,有很多参数: public Collection<Proforma> SearchAllProforma(DateTime? startDate = null, DateTime? endDate = null, int? institutionID = null, int? inspectionID = null, bool? isFollowup = null, bool? excludeDeleted = null, bo

我的DAL中有几种方法,有很多参数:

public Collection<Proforma> SearchAllProforma(DateTime? startDate = null, DateTime? endDate = null,
        int? institutionID = null, int? inspectionID = null, bool? isFollowup = null, bool? excludeDeleted = null,
        bool? nutritionalOnly = null, int? parentInspectionID = null)
公共集合SearchAllForma(DateTime?startDate=null,DateTime?endDate=null, int?institutionID=null,int?inspectionID=null,bool?isFollowup=null,bool?excludeDeleted=null, bool?nutritionalOnly=null,int?parentInspectionID=null) 我应该把它们压缩成一个对象参数吗?或者让它们保持原样,使用可选参数?或者两者都有


编辑-我真的应该说,这些参数中的每一个都映射到存储过程的一个参数。

考虑到它们中的许多都有它们的默认值,出于可用性考虑,我将使用不同数量的参数添加此方法的几个覆盖

这样,对于您的方法的消费者来说,选择合适的方法将更容易,而不需要在intellisense窗口中的所有参数

public Collection<RoIVProforma> SearchAllProforma(DateTime? startDate = null) 
{
   ...
}

public Collection<RoIVProforma> SearchAllProforma(DateTime? startDate = null, DateTime? endDate = null)
{
 ...
}

public Collection<RoIVProforma> SearchAllProforma(DateTime? startDate = null, DateTime? endDate = null,
        int? institutionID = null)
{
 ...
}

...
public Collection searchAllForma(DateTime?startDate=null)
{
...
}
公共集合SearchAllForma(DateTime?startDate=null,DateTime?endDate=null)
{
...
}
公共集合SearchAllForma(DateTime?startDate=null,DateTime?endDate=null,
int?机构ID=null)
{
...
}
...
我应该把它们压缩成一个对象参数吗


是的,绝对是。看着这个方法签名,我的眼睛开始流血。

就我个人而言,最好的方法是将一个
表达式
实例传递到SearchAllProforma方法中。但是,如果DAL不使用任何基于LINQ的底层数据源,那么实现解析表达式就更加困难。

同时,具有许多可选参数的方法是最差的。

使用对象作为参数,这是一种很好的方法

我应该把它们压缩成一个对象参数吗


不一定。默认值似乎没有问题(我假设您的函数可以处理
null
参数而没有问题)。如果您使用的是最新版本的C#,您可以调用以下函数:

SearchAllProforma(institutionID: 33);

在我看来,这并不坏。

如果所有参数都属于您的某个实体,那么您可以将谓词lambda表达式传递给该方法

我使用以下方法在我的实体中搜索一些条件

public List<Personel> GetAll(Func<Personel, bool> predicate = null)
        {
            List<Personel> result = new List<Personel>();

            if (predicate == null)
            {
                result = personelRepo.Table.ToList();
            }
            else
            {
                foreach (var item in personelRepo.Table)
                {
                    if (predicate(item))
                        result.Add(item);
                }
            }

            return result;
        }

我建议您为所有这些参数创建一个类作为属性

然后将类作为参数发送

Class SerachAllProformaParameter
{
//All Properties.
}

SerachAllProformaParameter parameter= new SerachAllProformaParameter();
parameter.PropertyName="value";

public Collection<RoIVProforma> SearchAllProforma(parameter);
类SerachalProformParameter
{
//所有属性。
}
SerachAllProformaParameter=新的SerachAllProformaParameter();
parameter.PropertyName=“value”;
公共集合SearchAllForma(参数);

这里有什么问题要解决?如果有的话,可以考虑使用实体框架。如果你使用的是C版本的最新版本,你可以使用命名参数。我在这里问了一个类似的问题:我不使用实体框架,因为目前我们的SQL服务器仍然使用2000。有些表也已经存在了几年,它们的设计不是特别好。这正是我调用方法的方式。我不认为为一个已经将每个参数都作为可选参数的方法添加多个重载有什么好处。对我来说,这似乎是额外的噪音和我必须维护的东西。@ScottD:正如我所写的,好处是当你写“.”在intellisense中,你看不到所有的参数,但就像第一个选项会看到没有参数的方法一样,之后可以使用“.”来获得其他定义。我想我将尝试这种方法,与我现有的方法并驾齐驱,看看我更喜欢哪种方法。我不确定我是否喜欢调用这些方法的语法。看起来很笨重(特别是在VB.NET中)。而且,我有很多方法都有很多参数,所以为每个方法实现类的加载可能需要很多工作。这也是一个温和的例子。你的意思是你有更多的参数?我有更多参数的方法。
Class SerachAllProformaParameter
{
//All Properties.
}

SerachAllProformaParameter parameter= new SerachAllProformaParameter();
parameter.PropertyName="value";

public Collection<RoIVProforma> SearchAllProforma(parameter);