0
我想創建一個GenericDao使用通用類。GenericDao使用通用類
我已經知道下面的代碼是錯誤的,抽象構造函數是不允許的和許多其他的東西,但我希望你明白我在嘗試什麼。
我的問題是,這可能嗎?這是創建GenericDao的正確方法嗎?
public class Business
{
public void Get()
{
var filter = new Entity1();
using (var dao = new GenericDao<Entity1>())
{
dao.Get(filter);
}
}
}
public abstract class GenericEntityBase
{
public abstract GenericEntityBase(IDataReader reader);
public abstract GenericEntityBase();
public abstract void FillParams(SqlParameter[] parameters);
public abstract void FillParams(SqlParameter[] parameters, PagingParameters paging);
public static string SearchProc;
}
public class Entity1 : GenericEntityBase
{
public int ID { get; set; }
#region IGenericEntity Members
public override void FillParams(SqlParameter[] parameters)
{
parameters[0].Value = this.ID;
}
public override void FillParams(SqlParameter[] parameters, PagingParameters paging)
{
}
public Entity1(IDataReader reader)
{
}
public Entity1()
{
}
public static string SearchProc = "SP001";
#endregion
}
public class GenericDao<T>: DataAccessObjectBaseV2 where T: GenericEntityBase, new()
{
public T Get(T filter)
{
var parameters = SqlHelperParameterCache.GetSpParameterSet(ConnectionString,T.SearchProc);
filter.FillParams(parameters);
using (IDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.StoredProcedure, T.SearchProc, parameters))
if (reader.Read())
return new T(reader);
return default(T);
}
}