ScottD
ScottD

Reputation: 173

DAL methods with lots of parameters

I have several methods in my DAL, with quite a few parameters:

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)

Should I condense these down to take an object parameter? Or leave them the way they are, using optional parameters? Or both?

Edit - I should really have said, that each one of these parameters maps to a parameter for a stored procedure.

Upvotes: 2

Views: 589

Answers (7)

Kishore Kumar
Kishore Kumar

Reputation: 12874

I would suggest you to Create a class for all those parameter as properties.

And then send the class as parameter.

Class SerachAllProformaParameter
{
//All Properties.
}

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

public Collection<RoIVProforma> SearchAllProforma(parameter);

Upvotes: 1

Dennis
Dennis

Reputation: 37770

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.

Upvotes: 0

Mert Akcakaya
Mert Akcakaya

Reputation: 3129

You can pass a predicate lambda expression to the method if all the arguments belong to some entity of yours.

I use following method to search for some criteria among my entities.

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

and then pass a predicate to the method when calling like:

var myFilteredEntities = GetAll(e => e.Name == "John" && e.IsMarried == false);

Upvotes: 0

ta.speot.is
ta.speot.is

Reputation: 27214

Should I condense these down to take an object parameter?

Not necessarily. The default values seem okay (I assume your function can handle null parameters without issue). If you're using a recent version of C# you can call this function like:

SearchAllProforma(institutionID: 33);

Which isn't all that bad, in my opinion.

Upvotes: 1

Talha
Talha

Reputation: 19262

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

Should I condense these down to take an object parameter?

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

Upvotes: 0

Tigran
Tigran

Reputation: 62246

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

...

Upvotes: 0

Related Questions