开发者

Passing parameters with conditionally

Update:

I have a dropdownlist with seven different options like (date, subject, press, cia, media...) and user will select one of the item from the dropdownlist and click on search button to get the results.

Here is my repository class GetInquiries method which accepts 7 parameters but it will pass only one parameters at a time to the stored procedure and in case of Date (it will pass both from/to)

public List<Inquiry> GetInquiries(string fromDate, string toDate, 
                                  string subject, string press,
                                  string cia, string media, 
                                  string status) 

Here is what I have come up with passing the parameters to GetInquiries:

 if (!string.IsNullOrEmpty(this.txtSubject.Text.Trim()))
 {
     e.Result = reporterRepo.GetInquiries(null,null,txtSubject.Text,null,null,null,null);
 }
 else
 {
     e.Result = reporterRepo.GetInquiries(null,null,null,null,null,null,null);
 }
 else if (!string.IsNullOrEmpty(this.fromDate.Text.Trim()))
     e.Result = reporterR开发者_开发百科epo.GetInquiries(fromDate.Text,null,null,null,null,null,null)
     .......................
     ...................
      ....................

I have to continue seven times with (if else if conditions) for all seven parameters.

Is there a way I can do it more clearly and more readability?


Well, depending on your exact situation, this might help:

e.Result = reporterRepo.GetInquiries(null, null,
     string.IsNullOrEmpty(this.txtSubject.Text.Trim()) ? null : txtSubject.Text,
     null, null, null, null);

Or if you're using C# 4 and can modify GetInquiries, you could possibly make all the parameters optional and use named arguments to specify which one you're actually providing.

Or write methods such as GetInquiriesByName, GetInquiriesBySubject etc to avoid overloading with clashing parameter types.

EDIT: If there's a dropdown, it sounds like you should be using the value of that to determine the appropriate branch to take (i.e. what to search on) and then go from there.


If you are using .NET 4.0 then you take advantge of optional and default parameters. If you're using an earlier framework version, then try creating overloasd of the routine:

    public List<Inquiry> GetInquiries(string fromDate, string toDate, 
                                                    string subject, string press,
                                                    string cia, string media) 
    {
// Pass empty string for status.
       return this.GetInquiries(fromDate, toDate, subject, press, cia, media, String.Empty) 
    }

    public List<Inquiry> GetInquiries(string fromDate, string toDate, 
                                                    string subject, string press,
                                                    string cia, string media, 
                                                    string status) 


var subjectText = this.txtSubject.Text.Trim();
var dateText = this.fromDate.Text.Trim();

if (subjectText.Length == 0) subjectText = null;
if (dateText .Length == 0) dateText = null;

e.Result = reporterRepo.GetInquiries(dateText ,null,subjectText,
                    null,null,null,null); 

string.IsNullOrEmpty(this.txtSubject.Text.Trim()))

Note that this.txtSubject.Text.Trim() will never be null, and if this.txtSubject.Text is null, this will throw an exception.


You reflection to invoke the method GetInquiries with just the n-th argument set.

public List<Inquiry> GetInquiries(int numparam, string myparameter)
{
    object[] params = new object[7];
    params[numparam] = myparameter;
    reporterRepo.Gettype().GetMethod("GetInquiries").Invoke(reporterRepo, params);
}

I have not tested the code but you get the idea.


public class Inquiries
{
    private SqlParameter[] _parameters;

    public DateTime FromDate { get{ //return value from parameters array} set{ // add a new parameter to your array } }
    public DateTime ToDate { get{ //return value from parameters array} set{ // add a new parameter to your array } }
    public String Subject { get{ //return value from parameters array} set{ // add a new parameter to your array } }
    public String Press { get{ //return value from parameters array} set{ // add a new parameter to your array } }
    public String Cia { get{ //return value from parameters array} set{ // add a new parameter to your array } }
    public String Media { get{ //return value from parameters array} set{ // add a new parameter to your array } }

    public List<Inquiry> Get()
    {
        // Your dal code using the parameters array
    }
}

Then make Inquiries a member level in the formthat's going to get inquiries, and with the changes you do, set the properties appropriately, unless you're only going to do one property at a time then just create the class and toss it each time you use it as:

List<Inquiry> myInquires = new Inquiry() { Subject = txtSubject.Text }.Get();


What about using some sort of a specification pattern

So in your example the usage would be

var querySpecification = new QuerySpecification();

if (!string.IsNullOrEmpty(this.txtSubject.Text.Trim()))
{
     querySpecification = new WhereSubject().Is(txtSubject);
}
else if (!string.IsNullOrEmpty(this.fromDate.Text.Trim()))
{
     querySpecification = new WhereFromDate().Is(fromDate);
}
else
{
     querySpecification = new All();
}
e.Result = reporterRepo.GetInquiries(querySpecification);

WhereSubject, WhereFromDate and All derives from a common IReportReposSpec.

public interface IReportReposSpec
{
      void Is(object value)

      // stored proc translation of the spec
      string Param {get;}
      string DbType {get;}
      string Value {get;}
}

The implementation of reporterRepo.GetInquiries could be as such

public List<Inquiry> GetInquiries(IReportReposSpec spec)
{
      var cmd = new SqlCommand();
      cmd.CommandType = CommandType.StoredProcedure;

      // for simplicity i'm going to use a pseudo code from here on
      cmd.AddParam(spec.Param, spec.DbType, spec.Value));

      //...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜