Re: Question about data layer
- From: "Paul" <paulriley@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 May 2009 09:53:20 +0100
Here is a ModelBase interface that alloows you to do that even over SOAP.
You may well ask why is this in the model? Well that is because I have
created these everywhere and IMHO this functionality is best here.
public interface IModelBase<TModelType>
{
int Id { get; set; }
DateTime DateArchived { get; set; }
TModelType UnderlyingValues { get; set; }
}
public interface IModelConcurrency
{
void PopulateUnderlyingValues();
void UndoChanges();
bool IsNew();
bool IsDirty(DataLoadingEnum dataLoadingLevel, int level);
bool IsArchive();
bool IsDirty(DataLoadingEnum dataLoadingLevel);
bool HasChanges
{
get;
set;
}
bool HasCompositeChanges
{
get;
set;
}
}
To get you going these are refection based but you can create an abstract
class and make each method virtual to allow you to ovveride in each to
remove the perfromance hit of reflection. but these reletive based methods
are good for prototypes. You DAL calls populate after it has populated the
object. Whan you call Save you identify if isNew, isDirty, Isarchive/delete.
I will not give you all the code because you need to understand and probably
do it your own way Data Access is personal and each has their opinion, but
hopefully it gives you an idea of how the model can support the DAL, and
still be useable over SOAP, rather than methods in properties which in my
opinion do not work as well but thats just my opinion and any way that work
well is fine.
public abstract class ModelBase<TModelType> : IModelBase<TModelType>,
IModelConcurrency where TModelType : ModelBase<TModelType>, new()
{
public virtual void PopulateUnderlyingValues()
{
Type type = typeof(TModelType);
//Get a list of public properties
if (UnderlyingValues == null)
UnderlyingValues = new TModelType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance |
BindingFlags.Public);
//for each property populate the corresponding underlying value from the
object property
foreach (PropertyInfo property in properties)
{
if (property.CanWrite && property.Name != "UnderlyingValues")
{
property.SetValue(UnderlyingValues, property.GetValue(this, null), null);
}
}
}
public bool IsNew()
{
if (this.UnderlyingValues == null)
return true;
else
return false;
}
}
.
- References:
- Question about data layer
- From: someone@xxxxxxxxxxxxx
- Re: Question about data layer
- From: Mr. Arnold
- Re: Question about data layer
- From: someone@xxxxxxxxxxxxx
- Question about data layer
- Prev by Date: Re: String and StringBuilder
- Next by Date: Re: Question about data layer
- Previous by thread: Re: Question about data layer
- Next by thread: Hex to Long
- Index(es):
Relevant Pages
|