0

CSLA .NET 是我的公司在我们的大多数项目中大量使用的框架,因此其中一些约束是由框架本身强制执行的,无法更改。我会尽力说明这些限制。

我有一组大约 50 个类,它们基本上都是一个 Dictionary(包装在 CSLA 类型中),它们提供了一个单例的查找实例,以便在我们程序的各个地方使用。

这些类的结构大致如下

public class SomeLookup : Csla.NameValueListBase<Integer, SomeLookupPair>
{
    private static SomeLookup _list
    private SomeLookup
    {
        //DataPortal.Fetch Calls the DataPortal_Fetch and returns the <T>
        if (_list != null) { _list = DataPortal.Fetch<SomeLookup>; }
    }

    public static SomeLookup GetSomeLookup(Object criteria)
    {
        return new SomeLookup;
    } 

    public override void DataPortal_Fetch(Object criteria)
    {
        using(SqlClient.SqlConnection cn = new SqlClient.SqlConnection(ConnectionString))
        {
            cn.Open();
            using(SqlClient.SqlCommand = new SqlClient.SqlCommand)
            {
                cm.Connection = cn;
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "getSomeLookup"

                using(var dr = new Csla.Data.SafeDataReader(cm.ExecuteReader)
                {
                    while(dr.Read)
                    {
                        //populates the interal nameValueList with lookup key/value pairs
                        Add(new NameValuePair(dr.GetInt32("Id"), 
                                              new SomeLookupPair { Code = dr.GetString("code"), 
                                                                   Description = dr.GetString("Description") });
                    }
                }
            }
        }
    }
}

public class SomeLookupPair
{
   public string Code {get; set;}
   public string Description {getl set;}
}

例如,用于查找的表类似于

Table SomeLookUp
    ID int PK
    Code varchar(2)
    Description varchar(50)

因此,引用此查找中的值的对象将在数据库中建模,仅存储 ID 字段,因此

Table SomeObject
   ID int PK
   SomeLookupId int FK
   .....

但在课堂上我只想显示描述或代码(用户描述,内部/业务使用代码)

我的问题是处理这种情况,我的班级需要按如下方式访问对象

private integer _someLookupID  { get { //retrived from database and stored }; set { _someLookupId = value; }

public SomeLookupPair _someLookupPair { get { (SomeLookUp.GetSomeLookup)[_someLookupID] }; }

public void setSomeLookupID(SomeLookupPair pair)
{
   _someLookupId = (SomeLookup.GetSomeLookUp).Where(s => s.Value(pair)).Select(s => s.Key).SingleOrDefault
}

感觉有更好的方法来处理SomeLookupID我可以直接进行的值的设置

4

1 回答 1

1

据我了解,您应该将其写为单个属性:

public SomeLookupPair SomeLookupPair 
{ 
   get 
   { 
      (SomeLookUp.GetSomeLookup)[_someLookupID] }; 
   }
   set
   {
     _someLookupId = (SomeLookup.GetSomeLookUp).Where(s => s.Value(value)).Select(s => s.Key).SingleOrDefault;
   }
}

我想获得更高的性能(实际上现在你遍历所有值)你可以重构 SomeLookupPair 并包含 ID (我认为它是为你的查找而设计的,因为现在你不使用对密钥的高性能访问!!!) . 像这样,您可以直接在 setter 中访问选定的 id

public SomeLookupPair SomeLookupPair 
{ 
   get 
   { 
      (SomeLookUp.GetSomeLookup)[_someLookupID] }; 
   }
   set
   {
     if(value == null) throw new ArgumentNullException();
     _someLookupId = value.ID;
   }
}
于 2011-12-20T18:04:17.247 回答