GenericDao使用通用类

时间:2014-03-12 14:38:29

标签: c# generics inheritance interface

我正在尝试使用Generic类创建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);
    } 
}

1 个答案:

答案 0 :(得分: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();

    }