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
我可以直接进行的值的设置