Reputation: 173
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
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
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
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
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
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
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