CSLA .NET is a framework that my company uses heavily in most of our projects, so some of these constraints are forced by the framework itself and can't be changed. I'll do my best to indicate those constraints.
I have a set of about 50 classes that are all similar in the fact that they are basically a Dictionary (wrapped in a CSLA type) that provides a singleton instance of a lookup for use in various places in our program.
The classes are all roughly structured as follows
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;}
}
A table for a lookup is for example similar to
Table SomeLookUp
ID int PK
Code varchar(2)
Description varchar(50)
So an object referencing the value from this lookup would be modeled in the database with just the ID field being stored so
Table SomeObject
ID int PK
SomeLookupId int FK
.....
But in the class I would want to only display the Description or Code (Description for users, code for internal/business use)
My question is does handling this case where my class needs to access the object as follows
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
}
Feel like there is a better way to handle the setting of the value for the SomeLookupID
where I can do it directly
In my understanding you should write is as a single property:
public SomeLookupPair SomeLookupPair
{
get
{
(SomeLookUp.GetSomeLookup)[_someLookupID] };
}
set
{
_someLookupId = (SomeLookup.GetSomeLookUp).Where(s => s.Value(value)).Select(s => s.Key).SingleOrDefault;
}
}
I fou want to get more performant (actually now you loop over all the values) you can refactor the SomeLookupPair and include ID (i think its designed for that your Lookup, cause now you dont use the performant access over the key!!!). Like that you get access to the selected id directly in the setter
public SomeLookupPair SomeLookupPair
{
get
{
(SomeLookUp.GetSomeLookup)[_someLookupID] };
}
set
{
if(value == null) throw new ArgumentNullException();
_someLookupId = value.ID;
}
}