不忘本~抽象类

返回目录

说在前

这个抽象类在我之前的文章中也有介绍过,而在“不忘本”系列中的抽象类,将会主要介绍它的概念及与接口的区别。

概念:

抽象类不同的普通类,它有自己的标示符abstract,在抽象类里将可以出现抽象方法,它本身只能充当父类的角色,所以,它在真实的生产过程中,都是通过子类去实现的,即抽象类不能被实例化。前面说的父类有时我们经常叫它基类,比如你的WEB层的controller可能需要一个基类,用来存储公用的属性和方法,这时,抽象类是最好的选择,在frameworks里有很多这样的例子,如System.Web.Mvc.Controller这就是一个抽象类,它由一组与控制器相关的方法及属性组件。

在项目中的抽象类:

在我们实现项目中,我提倡每个层中都要有自己的基类,用来存储公用的东西,如controller里的BaseController,BLL层的BaseBll,data层里的RepositoryBase等等,它们都是其它功能类的父类。

与接口的区别:

从概念上来说:

接口用来约束一组行为,实现接口的对象之前没有本质联系,它是一种行为规范,或者是一种标示,通过接口我们可以实现多态!

抽象类一些相关联的对象的一种抽象,将相关联的对象的公用信息进行抽象,放到一个类里,而这个类往往叫它抽象类,用abstract进行标示。

从代码的角度来说:

接口是一组行为规范,看一个简单仓储接口

    /// <summary>
    /// 基础的数据操作规范
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public interface IRepository<TEntity>
           where TEntity : class
    {
        /// <summary>
        /// 添加实体并提交到数据服务器
        /// </summary>
        /// <param name="item">Item to add to repository</param>
        void Insert(TEntity item);

        /// <summary>
        /// 移除实体并提交到数据服务器
        /// 如果表存在约束,需要先删除子表信息
        /// </summary>
        /// <param name="item">Item to delete</param>
        void Delete(TEntity item);

        /// <summary>
        /// 修改实体并提交到数据服务器
        /// </summary>
        /// <param name="item"></param>
        void Update(TEntity item);

        /// <summary>
        /// 得到指定的实体集合(延时结果集)
        /// Get all elements of type {T} in repository
        /// </summary>
        /// <returns>List of selected elements</returns>
        IQueryable<TEntity> GetModel();

        /// <summary>
        /// 根据主键得到实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        TEntity Find(params object[] id);
    }

它会叫每个具体的仓储接口去实现它,如IUserRepository是用户持久化的接口,同时它也可能被持久化基类去实现,如DbContextRepository,它是使用ef来完成持久化的基类,当然你可以使用MemoryContextRepository去实现IRepository这个接口,当然它的功能就是使用内存表来实现持久化的。

抽象类的代码展示:

  /// <summary>
    /// 做为一个持久化机制的实现,它可能是ado.net,linq2sql,entityframeworks,nhibernate,redis,memoryStream,fileStream etc.
    /// 宗旨:我们不应该将数据持久化的方式暴露到业务(领域)层
    /// 建立仓储:为每个聚合根建立仓储接口和实现,而不是为每个实体
    /// 使用仓储:应该根据使用场景去声明为仓储,而不是每次都是IExtensionRepository
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class DemoContextRepository<TEntity> :
        IExtensionRepository<TEntity>
        where TEntity : class
    {
        #region IExtensionRepository<TEntity>成员
        public void Insert(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public void Update(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public void Delete(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public void Update<T>(System.Linq.Expressions.Expression<Action<T>> entity) where T : class
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetModel(EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification)
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetModel(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
        {
            throw new NotImplementedException();
        }

        public TEntity Find(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
        {
            throw new NotImplementedException();
        }

        public TEntity Find(EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification)
        {
            throw new NotImplementedException();
        }

        public void BulkInsert(IEnumerable<TEntity> item, bool isRemoveIdentity)
        {
            throw new NotImplementedException();
        }

        public void BulkInsert(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public void BulkUpdate(IEnumerable<TEntity> item, params string[] fieldParams)
        {
            throw new NotImplementedException();
        }

        public void BulkDelete(IEnumerable<TEntity> item)
        {
            throw new NotImplementedException();
        }

        public event Action<EntityFrameworks.Entity.Core.SavedEventArgs> AfterSaved;

        public event Action<EntityFrameworks.Entity.Core.SavedEventArgs> BeforeSaved;
        #endregion

        #region IOrderableRepository成员
        public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy)
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy, EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification)
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy, System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region IRepository<TEntity>成员
        public void Insert(TEntity item)
        {
            throw new NotImplementedException();
        }

        public void Delete(TEntity item)
        {
            throw new NotImplementedException();
        }

        public void Update(TEntity item)
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> GetModel()
        {
            throw new NotImplementedException();
        }

        public TEntity Find(params object[] id)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

上面的抽象类只是一个DEMO,所以实现持久化的逻辑我并没有实现,当然这并不是它的重点,重点在于你的具体仓储如果继承了它,将会以DemoContextRepository这种方式去持久化对象。

返回目录

 

posted @ 2013-12-17 17:05  张占岭  阅读(958)  评论(1编辑  收藏  举报