2014-03-12 45 views
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); 
    } 
} 

回答

0
public class GenericDao<T,M> : DaoBase where M :T, IGenericEntity<T>, new() 
    { 

     #region singleton 

     private M Instance 
     { 
      get 
      { 
       if (_inst == null) 
       { 
        _inst = new M(); 
       } 
       return _inst; 
      } 
      set { 
       _inst = value; 
      } 
     } 
     private M _inst; 

     #endregion 

     #region Constructor 
     /// <summary> 
     /// Constructeur. 
     /// </summary> 
     public GenericDao(): base() 
     { 

      this.DataSourceName = Instance.GetDataSourceName(); 

      base.InitializeConnection(); 

     } 
     #endregion 

     #region Public Methods 

     /// <summary> 
     /// Get First Element 
     /// </summary> 
     /// <param name="filter"></param> 
     /// <returns></returns> 
     public T Get(T filter) 
     { 
      var parameters = SqlHelperParameterCache.GetSpParameterSet(ConnectionString, Instance.GetSearchProc());    
      Instance.FillParams(filter,parameters); 
      using (IDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.StoredProcedure, Instance.GetSearchProc(), parameters)) 
       if (reader.Read()) 
       { 
        var item = new M(); 
        item.FillEntity(reader); 
        return item; 
       } 
      return default(T); 
     } 

     /// <summary> 
     /// Lists All records 
     /// </summary> 
     /// <returns></returns> 
     public List<T> List() 
     { 
      var list = new List<T>(); 
      using (IDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.StoredProcedure, Instance.GetSearchProc())) 
       while (reader.Read()) 
       { 
        var item = new M(); 
        item.FillEntity(reader); 
        list.Add(item); 
       } 
      return list; 
     } 

     /// <summary> 
     /// Searchs Elements 
     /// </summary> 
     /// <param name="filter"></param> 
     /// <returns></returns> 
     public List<T> Search(T filter,PagingParameters paging) 
     { 
      var parameters = SqlHelperParameterCache.GetSpParameterSet(ConnectionString, Instance.GetSearchProc()); 
      Instance.FillParams(filter,parameters,paging); 
      var list = new List<T>(); 
      using (IDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.StoredProcedure, Instance.GetSearchProc(), parameters)) 
       while (reader.Read()) 
       { 
        var item = new M(); 
        item.FillEntity(reader); 
        list.Add(item); 
       } 
      return list; 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="Item"></param> 
     /// <returns></returns> 
     public int Insert(T Item) 
     { 
      var ret = 0; 
      throw new NotImplementedException(); 
      return ret; 
     } 


     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="Item"></param> 
     /// <returns></returns> 
     public int Update(T Item) 
     { 
      var ret = 0; 
      throw new NotImplementedException(); 
      return ret; 
     } 


     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="Item"></param> 
     /// <returns></returns> 
     public int Delete(T Item) 
     { 
      var ret = 0; 
      throw new NotImplementedException(); 
      return ret; 
     } 
     #endregion 
    } 

    public interface IGenericEntity<T> 
    { 
     void FillEntity(IDataReader reader); 
     void FillParams(T filter,SqlParameter[] parameters); 
     void FillParams(T filter,SqlParameter[] parameters, PagingParameters paging); 
     string GetSearchProc(); 
     string GetDataSourceName(); 

    }