第一回 要想知道为什么抽象出基类,应该先对基类有一个比较明确的认识

简介:

    基类,即基础类型,也称为父类,祖宗类等,它一般会以抽象类(abstract class)的形式体现出来,它会包括一些具有共性的,一般性的信息(属性和方法等),它在描述一类对象时,经常会被抽象出来,如一个小猫,一只小狗,很容易会让你抽象出一个animal来,这是正常的,是符合面向对象人生观的,呵呵。

      而在实际项目开发中,这种例子也不少,可以说到处都可以看到基类的身影,有人说,请不要使用继承,因为它为使你的程序很糟糕,依赖太多不好,但我要说的是,如果你的程序是一个关系复杂,面向对象的,那你的程序在某些地方必须要去继承(抽象出基类),有时,我们会说,那什么时候去使用基类,我们应该如何去衡量呢?事实上这种衡量是没有意义的,是应试的,就像我国的教育,我是很不喜欢,永远都是应付考试,对于基类这东西,你需要它时,就使用它,不要管什么原则,当你需要解耦时,就使用对象的组合,还有,如果对基类还是有所偏见,那可以去看看微软的System.Collections和System.Web.Mvc下的实现,看看人家是如何设计的。

    以下是一个完整的,简单的数据操作基类,它将子类公用的属性数据库上下文(DataContext)抽象出来,将增删改查的Curd操作也抽象出来,结果,这个基类就变成了这样:

  1     /// <summary>
  2     /// 标准数据操作基类
  3     /// </summary>
  4     public abstract class DataBase : IRepository
  5     {
  6         /// <summary>
  7         /// 数据访问对象(只对子类可见)
  8         /// </summary>
  9         protected DataContext DB;
 10 
 11         #region Constructors
 12         public DataBase()
 13             : this(new LINQ.DataClasses1DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["XXB"].ToString()))
 14         { }
 15         public DataBase(DataContext db)
 16             : this(() => { return db; })
 17         { }
 18         public DataBase(Func<DataContext> func)
 19         {
 20             this.DB = func();
 21         }
 22         #endregion
 23 
 24         #region DBContext SubmitChanges
 25         /// <summary>
 26         /// XXB默认提交【重写时候可能需要写入自定义的类似约束的逻辑】
 27         /// </summary>
 28         protected virtual void SubmitChanges()
 29         {
 30             ChangeSet cSet = DB.GetChangeSet();
 31             if (cSet.Inserts.Count > 0
 32                 || cSet.Updates.Count > 0
 33                 || cSet.Deletes.Count > 0)
 34             {
 35                 try
 36                 {
 37                     DB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
 38                 }
 39                 catch (System.Data.Linq.ChangeConflictException)
 40                 {
 41                     foreach (System.Data.Linq.ObjectChangeConflict occ in DB.ChangeConflicts)
 42                     {
 43                         occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
 44                         occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
 45                         occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
 46                     }
 47                     DB.SubmitChanges();
 48                 }
 49             }
 50         }
 51 
 52         #endregion
 53 
 54         #region IRepository 成员
 55 
 56         public virtual void Update<TEntity>(TEntity entity) where TEntity : class
 57         {
 58             this.SubmitChanges();
 59             
 60         }
 61 
 62         public virtual void Update<TEntity>(IEnumerable<TEntity> list) where TEntity : class
 63         {
 64             list.ToList().ForEach(entity =>
 65             {
 66                 this.Update<TEntity>(entity);
 67             });
 68         }
 69 
 70         public virtual void Insert<TEntity>(TEntity entity) where TEntity : class
 71         {
 72             DB.GetTable<TEntity>().InsertOnSubmit(entity);
 73             this.SubmitChanges();
 74         }
 75 
 76         public virtual void Insert<TEntity>(IEnumerable<TEntity> list) where TEntity : class
 77         {
 78             DB.GetTable<TEntity>().InsertAllOnSubmit<TEntity>(list);
 79             this.SubmitChanges();
 80         }
 81 
 82         public virtual TEntity InsertGetIDENTITY<TEntity>(TEntity entity) where TEntity : class
 83         {
 84             this.Insert<TEntity>(entity);
 85             return GetModel<TEntity>(i => i == entity).FirstOrDefault();
 86         }
 87 
 88         public virtual void Delete<TEntity>(TEntity entity) where TEntity : class
 89         {
 90             DB.GetTable<TEntity>().DeleteOnSubmit(entity);
 91             this.SubmitChanges();
 92         }
 93 
 94         public virtual void Delete<TEntity>(IEnumerable<TEntity> list) where TEntity : class
 95         {
 96             DB.GetTable<TEntity>().DeleteAllOnSubmit<TEntity>(list);
 97             this.SubmitChanges();
 98         }
 99 
100         public virtual IQueryable<TEntity> GetModel<TEntity>() where TEntity : class
101         {
102             return this.DB.GetTable<TEntity>();
103         }
104 
105         public virtual IQueryable<TEntity> GetModel<TEntity>(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) where TEntity : class
106         {
107             return GetModel<TEntity>().Where(predicate);
108         }
109 
110         public virtual TEntity Find<TEntity>(params object[] keyValues) where TEntity : class
111         {
112             var mapping = DB.Mapping.GetTable(typeof(TEntity));
113             var keys = mapping.RowType.IdentityMembers.Select((m, i) => m.Name + " = @" + i).ToArray();
114             TEntity entityTEntity = DB.GetTable<TEntity>().Where(String.Join(" && ", keys), keyValues).FirstOrDefault();
115             if (entityTEntity != null)
116                 DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, entityTEntity);
117             return entityTEntity;
118         }
119 
120         #endregion
121     }
复制代码

对于一个数据操作具体类来说,直接继承它就可以了,然后再去实现自己的业务逻辑,很清晰。

 本文转自博客园张占岭(仓储大叔)的博客,原文链接:第一回 要想知道为什么抽象出基类,应该先对基类有一个比较明确的认识,如需转载请自行联系原博主。

目录
相关文章
|
8月前
|
缓存 边缘计算 网络协议
CDN加速网站的终极指南:关键策略与实战技巧
内容分发网络(CDN)是现代互联网基础设施的核心,通过全球分布式服务器网络加速内容交付。掌握CDN原理与实践,可显著提升网站速度、安全性和用户体验。本文详解CDN基础、优势、实施步骤及未来趋势,帮助您选择合适方案并优化配置,在数字时代中占据竞争优势。无论是高流量网站还是跨国企业,CDN都是不可或缺的技术工具。立即行动,让您的内容交付更高效!
|
数据处理 Python
熵值法计算权重
熵值法计算权重是一种基于信息论的方法,用于多指标综合评价。通过计算各指标的信息熵,反映指标的变异程度,从而确定其在综合评价中的权重。熵值越小,表示信息量越大,指标的重要性越高。该方法适用于样本数据较少的情形,能有效避免主观因素的影响。文中详细介绍了熵值法的原理、计算步骤及Python实现代码。
2132 0
|
存储 Shell 开发工具
查看git 的远程地址
在Git中,你可以通过几种不同的命令来查看远程仓库的地址。以下是一些常用的方法: ### 1. 使用`git remote -v`命令 这是查看远程仓库地址最常用的命令。它会列出所有远程仓库的名称(如`origin`)以及对应的URL(包括fetch和push的URL,如果它们不同的话)。 ```bash git remote -v ``` 输出示例: ``` origin https://github.com/username/repo.git (fetch) origin https://github.com/username/repo.git (push) ``` ###
2334 12
|
数据采集 监控 数据挖掘
ERP系统中的数据分析与报表生成
【7月更文挑战第25天】 ERP系统中的数据分析与报表生成
989 2
|
设计模式 存储 API
C++桥接模式大解析:轻松设计与实现高效软件架构
C++桥接模式大解析:轻松设计与实现高效软件架构
547 0
|
存储 SQL 缓存
C++解释器模式实战:从设计到应用的全面指南
C++解释器模式实战:从设计到应用的全面指南
427 0
|
存储 编译器 程序员
int 和 long 的区别
int 和 long 的区别
|
存储 缓存 NoSQL
在应用中使用缓存服务
【6月更文挑战第24天】本文介绍redis缓存的基本知识和使用。Redis超越简单的键值存储,Redis查询直接针对键,不支持复杂查询,适合特定场景的高性能缓存。用于减少数据库交互,优化性能。并提供练习源码查阅。
166 1
|
前端开发
antd-protable的分页逻辑封装
antd-protable的分页逻辑封装
759 0
|
设计模式 算法 安全
C++常用的11种设计模式解释及示例
C++常用的11种设计模式解释及示例

热门文章

最新文章