在C#中处理多参数查询的模式或最佳实践

Enr*_*ani 1 c# parameters design-patterns

我在编写winforms或wpf应用程序以对数据库执行查询时总是采用的方法如下:

  • 设计一个带有多个控件的接口,以将参数传递给我的查询类

  • 使用Linq或Entity Framework作为数据源,使用查询的字段,属性和方法构建"DataAccess"类.

  • Managing the events generated by the controls, to pass parameters and select wich method to use to retrieve data.

Example from the DataAccess class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    public class DataAccess
    {
        public LinqDataContext db = new LinqDataContext();

#region Private Fields
#region Properties(Constructors)
#region Methods

// Sample method
public List<DataAccess> Mymethod(string valoredata, int esenzione,
            string valorebatch)
{
    if (esenzione == 0)
    {
        return (from elementi in db.IndexTables
            from elementi2 in db.DocumentTables
            where elementi.ID == elementi2.ID 
                && elementi.DataScansione == Convert.ToDateTime(valoredata)
                && elementi.Batch == valorebatch  
                && elementi.NonEsente == true                    
            select associazionePropieta(elementi, elementi2)).ToList();
    }
    else if (esenzione == 1) 
    {
        return (from elementi in db.IndexTables
            from elementi2 in db.DocumentTables
            where elementi.ID == elementi2.ID 
                && elementi.DataScansione == Convert.ToDateTime(valoredata)
                && elementi.Batch == valorebatch 
                && elementi.BiffaturaReddito == false 
                && elementi.FirmaAutocertificazione == false 
                && elementi.NonEsente == false
            select associazionePropieta(elementi, elementi2)).ToList();
    }
    else
    {
        return (from elementi in db.IndexTables
            from elementi2 in db.DocumentTables
            where elementi.ID == elementi2.ID 
                && elementi.DataScansione == Convert.ToDateTime(valoredata)
                && elementi.Batch == valorebatch 
                && (elementi.BiffaturaReddito == true 
                    || elementi.FirmaAutocertificazione == true)
            select associazionePropieta(elementi, elementi2)).ToList();
    }

}

#endregion

// From Refactoring...
private static DataAccess associazionePropieta(IndexTable elementi, 
                             DocumentTable elementi2)
{
    return new DataAccess
    {
        codiceImpegnativa = elementi.CodiceImpegnativa,
        nominativo = elementi.Nominativo,
        codiceFiscale = elementi.CodiceFiscale,
        dataImpegnativa = elementi.DataImpegnativa,
        nonEsente = Convert.ToBoolean(elementi.NonEsente),
        biffaturaReddito = Convert.ToBoolean(elementi.BiffaturaReddito),
        autocertificazione = Convert.ToBoolean(elementi.FirmaAutocertificazione),
        codiceEsenzione = elementi.CodiceEsenzione,
        raoU = Convert.ToBoolean(elementi.RaoU),
        raoB = Convert.ToBoolean(elementi.RaoB),
        raoD = Convert.ToBoolean(elementi.RaoD),
        raoP = Convert.ToBoolean(elementi.RaoP),
        dataScansione = Convert.ToDateTime(elementi.DataScansione),
        batch = elementi.Batch,
        documentID = elementi.DcumentID,
        path = elementi2.Path,
        ID = elementi2.ID.ToString()
    };
}
Run Code Online (Sandbox Code Playgroud)

Here is a little piece from the "MainWindow" code, i have 7 controls (1 datepicker, 3 comboboxes and 3 textboxes) to use as parameters for the query:

if (datePickerData.SelectedDate != null 
        && comboBatch.SelectedValue == null
        && comboEsenzione.SelectedValue != null 
        && nome == true 
        && impegnativa == true 
        && fiscale == true)
{
    this.dataGridRisultati.ItemsSource =
        dati.Mymethod(datePickerData.SelectedDate.ToString(),
            comboEsenzione.SelectedIndex);
}

}
Run Code Online (Sandbox Code Playgroud)

Has you can imagine, handling all the combinations of parameters that can be passed and the decisional structures (if or switch case) is becoming a huge work... i diveded the decisional methods in regions (1 parameter, 2 parameters, 3.... 7 parameters) and that is giving me some rest, but today i just had to stop at the 4 parameters region, i don't know if i was just tired or what, but i could not figure out a pattern to match all the combinations.

So finally my question is:

我确信有一种更简单的方法来处理用户使用组合框而不是文本框的概率,或者同时使用所有7个控件来查询我的数据库,但我无法弄明白.

是否有最佳实践或模式可以提供帮助?

提前致谢

Cly*_*yde 5

我不确定我是否完全理解你的问题100%,但我相信你在询问有多种参数时构建数据访问调用的有效方法,但它们是可选的.

您可能不知道可以分阶段构建LINQ查询.例如,想一想:

var query = from record in datasource
    select record;

if (Parameter1HasValue) query = query.Where(record => record.Field1 == Parameter1);
if (Parameter2HasValue) query = query.Where(record => record.Field2 == Parameter2);
if (Parameter3HasValue) query = query.Where(record => record.Field3 == Parameter3);
return query.ToList();
Run Code Online (Sandbox Code Playgroud)

您可能在将所有过滤器作为可空参数的方法中使用此方法,或者过滤器值可能是数据访问类中的其他属性 - 这取决于您.