2

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)

これらを凝縮してオブジェクトパラメータを取得する必要がありますか?または、オプションのパラメータを使用して、そのままにしますか?または両方?

編集-私は本当に言ったはずですが、これらのパラメーターのそれぞれは、ストアドプロシージャのパラメーターにマップされます。

4

7 に答える 7

1

これらを凝縮してオブジェクトパラメータを取得する必要がありますか?

必ずしも。デフォルト値は問題ないようです(関数はnull問題なくパラメーターを処理できると思います)。最近のバージョンのC#を使用している場合は、次のようにこの関数を呼び出すことができます。

SearchAllProforma(institutionID: 33);

私の意見では、これはそれほど悪いことではありません。

于 2012-05-24T09:17:52.497 に答える
1

これらすべてのパラメーターのクラスをプロパティとして作成することをお勧めします。

次に、クラスをパラメーターとして送信します。

Class SerachAllProformaParameter
{
//All Properties.
}

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

public Collection<RoIVProforma> SearchAllProforma(parameter);
于 2012-05-24T09:22:54.717 に答える
0

Consider that a lot of them have their default values, for usability perspctive I would add several overrides of this method with different quantity of parameters.

In this way, for a consumer of your method would be easier to choose appropriate one without having in front of eyes all that parameters in intellisense window.

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)
{
 ...
}

...
于 2012-05-24T09:15:21.637 に答える
0

Should I condense these down to take an object parameter?

Yes, absolutely. Looking at this method signature makes my eyes start bleeding.

于 2012-05-24T09:15:34.340 に答える
0

Personally, the best way here is to pass an Expression<Func<RoIVProforma, bool>> instance into SearchAllProforma method. But it is more hard to implement parsing expression, if your DAL doesn't use any LINQ-based underlying data source.
The same time, a method with many optional parameters is the worst.

于 2012-05-24T09:16:11.733 に答える
0

Use object as parameter, it is a good approach..

于 2012-05-24T09:16:14.497 に答える
0

すべての引数が自分のエンティティに属している場合は、述語ラムダ式をメソッドに渡すことができます。

次の方法を使用して、エンティティ内のいくつかの基準を検索します。

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;
        }

次に、次のように呼び出すときに、述語をメソッドに渡します。

var myFilteredEntities = GetAll(e => e.Name == "John" && e.IsMarried == false);
于 2012-05-24T09:21:08.027 に答える