C # - Is this a "good" data access pattern and what is it called?
First of all, I apologize for the vagueness of the title of the question and if this has already been asked elsewhere. I have struggled to find a similar answer due to the number of other "what is a pattern called" questions.
I have the following abstract class:
public abstract class PositionProvider<T> : DalProvider<T>, IDalProvider
where T : IPositionEntity
{
protected PositionProvider()
: base() { }
public RP_PositionType PositionType
{
get
{
return _positionType;
}
}
private RP_PositionType _positionType;
public abstract List<T> GetList();
internal void SetPositionType(RP_PositionType positionType)
{
if (_positionType == null)
{
_positionType = positionType;
}
else
{
throw new NotSupportedException(
"PositionType can only be set once.");
}
}
}
Then I got a concrete implementation of this class:
public class SqlPositionProvider<T>
: PositionProvider<T> where T : IPositionEntity
{
public override List<T> GetList()
{
int positionTypeId = (int)this.PositionType;
// Return the matching items here
}
}
This is then used by several different "item" classes such as the following, but replacing CustomerEntity with SiteEntity / MajorEntity / MinorEntity:
public class CustomerProvider
{
public static PositionProvider<CustomerEntity> Instance
{
get
{
if (_instance == null)
{
DalHelper.CreateInstance<PositionProvider<CustomerEntity>>(
out _instance);
_instance.SetPositionType(RP_PositionType.Customer);
}
return _instance;
}
}
private static PositionProvider<CustomerEntity> _instance;
}
ClientProvider, SiteProvider, etcProvider all just contain a specific instance of the PositionProvider class. The only difference between them is the entity type and the RP_PositionType enumeration. This way I can use the same concrete Sql implementation in GetList () to drop all records from a specific table based on PositionType (PositionTypeId when the enum is converted to int).
source to share