首先,我對問題標題的模糊性以及是否已經在其他地方提出過,表示抱歉。由於其他「這種模式稱爲」問題的數量,我努力尋找類似的答案。C# - 這是一個'好'的數據訪問模式,它叫什麼?
我有以下的抽象類:
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.");
}
}
}
然後我有一個具體的實現這個類的:
public class SqlPositionProvider<T>
: PositionProvider<T> where T : IPositionEntity
{
public override List<T> GetList()
{
int positionTypeId = (int)this.PositionType;
// Return the matching items here
}
}
然後,這是由許多不同的「項目利用'類,如下所示,但用SiteEntity/MajorEntity/MinorEntity代替CustomerEntity:
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;
}
CustomerProvider,SiteProvider等提供者都只是持有PositionProvider類的特定實例。它們唯一的區別是實體類型和RP_PositionType枚舉。這樣我可以在GetList()中使用相同的Sql具體實現來基於PositionType(當枚舉轉換爲int值時的PositionTypeId)從特定表中撤回所有記錄。
我不想在我的財產集,因爲我只想過它設置一次。因爲它可能是5個枚舉之一,所以我想確保一旦設置它就永遠不會改變。這裏有一個Singleton的確定的方面,但這只是針對事物的實例方面。此外,它不是一個正確的單身人士,因爲它不是線程安全的。 – GenericTypeTea 2009-10-27 15:06:17