标签:des style blog class code java
前面已经简单介绍过了该框架(不一定是框架),本文开始重点记录其使用过程。可能记录的内容不是太详尽,框架也可能非常烂,但是里面的代码句句是实战项目所得。本文非教唆之类的文章,也非批判之类的文章,更不是炫技之类的文章,只是工作的记录和总结,希望能够给大家一些启迪,忘诸位勿喷!
一. 组建项目需要的几个部分
.NET中最为经典的三层结构,众所周知,无人不晓. 在Git.Framework框架中我们也遵循最基本的这种结构,ORM部分我们划分为如下: 数据实体层,数据访问接口层,数据访问层,[层序主入口加载相应的配置]。 在上一篇我们讲到了最基本的配置。这几个层次结构都要遵循一定的规则.
项目结构截图:
以上是用这个框架开发的一套仓库管理系统: 其中Git.Storage.Entity是对应的是实体数据层, Git.Storage.IDataAccess 是数据访问接口层,Git.Storage.DataAccess数据访问层。当然我们也可以将这些内容合并到一个类库中,但是一般不建议这么做。 而Git.Storage.Web 则是项目的视图层,也就是程序的主入口。
二. 实体类的映射
既然做对象关系映射,那么我们就要将Entity和数据库的相关信息关联起来。先看看一个实体类的代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Git.Framework.ORM; namespace Git.Storage.Entity.InStorage { [TableAttribute(DbName = "JooShowGit", Name = "InStorage", PrimaryKeyName = "ID", IsInternal = false)] public partial class InStorageEntity:BaseEntity { public InStorageEntity() { } [DataMapping(ColumnName = "ID", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=true,AutoIncrement=true,IsMap=true)] public Int32 ID { get; set; } public InStorageEntity IncludeID (bool flag) { if (flag && !this.ColumnList.Contains("ID")) { this.ColumnList.Add("ID"); } return this; } [DataMapping(ColumnName = "OrderNum", DbType = DbType.String,Length=50,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string OrderNum { get; set; } public InStorageEntity IncludeOrderNum (bool flag) { if (flag && !this.ColumnList.Contains("OrderNum")) { this.ColumnList.Add("OrderNum"); } return this; } [DataMapping(ColumnName = "InType", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 InType { get; set; } public InStorageEntity IncludeInType (bool flag) { if (flag && !this.ColumnList.Contains("InType")) { this.ColumnList.Add("InType"); } return this; } [DataMapping(ColumnName = "ProductType", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 ProductType { get; set; } public InStorageEntity IncludeProductType (bool flag) { if (flag && !this.ColumnList.Contains("ProductType")) { this.ColumnList.Add("ProductType"); } return this; } [DataMapping(ColumnName = "SupNum", DbType = DbType.String,Length=50,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string SupNum { get; set; } public InStorageEntity IncludeSupNum (bool flag) { if (flag && !this.ColumnList.Contains("SupNum")) { this.ColumnList.Add("SupNum"); } return this; } [DataMapping(ColumnName = "SupName", DbType = DbType.String,Length=100,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string SupName { get; set; } public InStorageEntity IncludeSupName (bool flag) { if (flag && !this.ColumnList.Contains("SupName")) { this.ColumnList.Add("SupName"); } return this; } [DataMapping(ColumnName = "ContactName", DbType = DbType.String,Length=200,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string ContactName { get; set; } public InStorageEntity IncludeContactName (bool flag) { if (flag && !this.ColumnList.Contains("ContactName")) { this.ColumnList.Add("ContactName"); } return this; } [DataMapping(ColumnName = "Phone", DbType = DbType.String,Length=50,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string Phone { get; set; } public InStorageEntity IncludePhone (bool flag) { if (flag && !this.ColumnList.Contains("Phone")) { this.ColumnList.Add("Phone"); } return this; } [DataMapping(ColumnName = "Address", DbType = DbType.String,Length=200,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string Address { get; set; } public InStorageEntity IncludeAddress (bool flag) { if (flag && !this.ColumnList.Contains("Address")) { this.ColumnList.Add("Address"); } return this; } [DataMapping(ColumnName = "ContractOrder", DbType = DbType.String,Length=50,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string ContractOrder { get; set; } public InStorageEntity IncludeContractOrder (bool flag) { if (flag && !this.ColumnList.Contains("ContractOrder")) { this.ColumnList.Add("ContractOrder"); } return this; } [DataMapping(ColumnName = "ContractType", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 ContractType { get; set; } public InStorageEntity IncludeContractType (bool flag) { if (flag && !this.ColumnList.Contains("ContractType")) { this.ColumnList.Add("ContractType"); } return this; } [DataMapping(ColumnName = "Status", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 Status { get; set; } public InStorageEntity IncludeStatus (bool flag) { if (flag && !this.ColumnList.Contains("Status")) { this.ColumnList.Add("Status"); } return this; } [DataMapping(ColumnName = "IsDelete", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 IsDelete { get; set; } public InStorageEntity IncludeIsDelete (bool flag) { if (flag && !this.ColumnList.Contains("IsDelete")) { this.ColumnList.Add("IsDelete"); } return this; } [DataMapping(ColumnName = "Num", DbType = DbType.Int32,Length=4,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 Num { get; set; } public InStorageEntity IncludeNum (bool flag) { if (flag && !this.ColumnList.Contains("Num")) { this.ColumnList.Add("Num"); } return this; } [DataMapping(ColumnName = "Amount", DbType = DbType.Decimal,Length=9,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public decimal Amount { get; set; } public InStorageEntity IncludeAmount (bool flag) { if (flag && !this.ColumnList.Contains("Amount")) { this.ColumnList.Add("Amount"); } return this; } [DataMapping(ColumnName = "NetWeight", DbType = DbType.Double,Length=8,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public double NetWeight { get; set; } public InStorageEntity IncludeNetWeight (bool flag) { if (flag && !this.ColumnList.Contains("NetWeight")) { this.ColumnList.Add("NetWeight"); } return this; } [DataMapping(ColumnName = "GrossWeight", DbType = DbType.Double,Length=8,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public double GrossWeight { get; set; } public InStorageEntity IncludeGrossWeight (bool flag) { if (flag && !this.ColumnList.Contains("GrossWeight")) { this.ColumnList.Add("GrossWeight"); } return this; } [DataMapping(ColumnName = "OrderTime", DbType = DbType.DateTime,Length=8,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public DateTime OrderTime { get; set; } public InStorageEntity IncludeOrderTime (bool flag) { if (flag && !this.ColumnList.Contains("OrderTime")) { this.ColumnList.Add("OrderTime"); } return this; } [DataMapping(ColumnName = "CreateTime", DbType = DbType.DateTime,Length=8,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public DateTime CreateTime { get; set; } public InStorageEntity IncludeCreateTime (bool flag) { if (flag && !this.ColumnList.Contains("CreateTime")) { this.ColumnList.Add("CreateTime"); } return this; } [DataMapping(ColumnName = "CreateUser", DbType = DbType.String,Length=100,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string CreateUser { get; set; } public InStorageEntity IncludeCreateUser (bool flag) { if (flag && !this.ColumnList.Contains("CreateUser")) { this.ColumnList.Add("CreateUser"); } return this; } [DataMapping(ColumnName = "AuditUser", DbType = DbType.String,Length=100,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string AuditUser { get; set; } public InStorageEntity IncludeAuditUser (bool flag) { if (flag && !this.ColumnList.Contains("AuditUser")) { this.ColumnList.Add("AuditUser"); } return this; } [DataMapping(ColumnName = "AuditeTime", DbType = DbType.DateTime,Length=8,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public DateTime AuditeTime { get; set; } public InStorageEntity IncludeAuditeTime (bool flag) { if (flag && !this.ColumnList.Contains("AuditeTime")) { this.ColumnList.Add("AuditeTime"); } return this; } [DataMapping(ColumnName = "PrintUser", DbType = DbType.String,Length=100,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string PrintUser { get; set; } public InStorageEntity IncludePrintUser (bool flag) { if (flag && !this.ColumnList.Contains("PrintUser")) { this.ColumnList.Add("PrintUser"); } return this; } [DataMapping(ColumnName = "PrintTime", DbType = DbType.DateTime,Length=8,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public DateTime PrintTime { get; set; } public InStorageEntity IncludePrintTime (bool flag) { if (flag && !this.ColumnList.Contains("PrintTime")) { this.ColumnList.Add("PrintTime"); } return this; } [DataMapping(ColumnName = "StoreKeeper", DbType = DbType.String,Length=100,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string StoreKeeper { get; set; } public InStorageEntity IncludeStoreKeeper (bool flag) { if (flag && !this.ColumnList.Contains("StoreKeeper")) { this.ColumnList.Add("StoreKeeper"); } return this; } [DataMapping(ColumnName = "Reason", DbType = DbType.String,Length=800,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string Reason { get; set; } public InStorageEntity IncludeReason (bool flag) { if (flag && !this.ColumnList.Contains("Reason")) { this.ColumnList.Add("Reason"); } return this; } [DataMapping(ColumnName = "OperateType", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public Int32 OperateType { get; set; } public InStorageEntity IncludeOperateType (bool flag) { if (flag && !this.ColumnList.Contains("OperateType")) { this.ColumnList.Add("OperateType"); } return this; } [DataMapping(ColumnName = "EquipmentNum", DbType = DbType.String,Length=50,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string EquipmentNum { get; set; } public InStorageEntity IncludeEquipmentNum (bool flag) { if (flag && !this.ColumnList.Contains("EquipmentNum")) { this.ColumnList.Add("EquipmentNum"); } return this; } [DataMapping(ColumnName = "EquipmentCode", DbType = DbType.String,Length=50,CanNull=false,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string EquipmentCode { get; set; } public InStorageEntity IncludeEquipmentCode (bool flag) { if (flag && !this.ColumnList.Contains("EquipmentCode")) { this.ColumnList.Add("EquipmentCode"); } return this; } [DataMapping(ColumnName = "Remark", DbType = DbType.String,Length=800,CanNull=true,DefaultValue=null,PrimaryKey=false,AutoIncrement=false,IsMap=true)] public string Remark { get; set; } public InStorageEntity IncludeRemark (bool flag) { if (flag && !this.ColumnList.Contains("Remark")) { this.ColumnList.Add("Remark"); } return this; } } public partial class InStorageEntity { [DataMapping(ColumnName = "CreateUserName", DbType = DbType.String)] public string CreateUserName { get; set; } public string InTypeLable { get; set; } public string StatusLable { get; set; } } }
[TableAttribute(DbName = "JooShowGit", Name = "InStorage", PrimaryKeyName = "ID", IsInternal = false)]
这个用于实体的描述特性,用于关联数据库中的表,这里简单将各个属性描述一下:
[DBName] 用于适配数据库连接配置文件中的name属性,可以结合下面的xml配置对比看
<?xml version="1.0" encoding="utf-8" ?> <databaseList> <database name="JooShowGit"> <connectionString>Server=127.0.0.1;database=JooShowGit;user id=sa;Password=000000</connectionString> </database> </databaseList>
[Name] 用于关联到这个连接的数据库中对应的哪一张表,比如这个配置就是对应数据库中的InStorage表
[PrimaryKeyName] 指定这张表的主键字段名, 这里我们一般用主键自增列,目前尚不支持联合组建
[IsInternal] 这个暂无特别意义,作为保留属性
[DataMapping(ColumnName = "ID", DbType = DbType.Int32,Length=4,CanNull=false,DefaultValue=null,PrimaryKey=true,AutoIncrement=true,IsMap=true)]
[ColumnName] 对应数据库中的字段名
[DbType] 对应的数据类型
[Length]对应的数据存储为长度
[CanNull] 这个字段是否允许为空
[DefaultValue]默认值
[PrimaryKey]是否主键
[AutoIncrement]是否自增
[IsMap] 是否映射字段 【注意这个标识属性,如果这个字段有对应的数据库映射字段则为true,否则为false。 因为在某些情况下实体类还有扩展字段,但是这些字段在数据库并没有】
public partial class InStorageEntity:BaseEntity
实体类需要集成BaseEntity类,BaseEntity中定义了很多方法,用于便捷操作辅助操作。
三. 数据访问接口
文章开头已经说过了,使用该框架需要遵循一定的规则模式,数据访问接口也是如此,先看看下面一段代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Git.Framework.ORM; using Git.Storage.Entity.InStorage; namespace Git.Storage.IDataAccess.InStorage { public partial interface IInStorage : IDbHelper<InStorageEntity> { } }
这段代码定义了一个接口IInStorage,但是这个接口继承了泛型接口IDbHelper<T>。 而IDbHelper<T>是框架中定义的一个接口,这个接口封装了对数据库操作的一些方法。
public interface IDbHelper<T> : IDisposable where T : Git.Framework.ORM.BaseEntity { int Add(List<T> list); int Add(T entity); int Add(List<T> list, bool isOpenTrans); int Add(T entity, bool isOpenTrans); int Delete(IEnumerable<int> ids); int Delete(int id); int Delete(object value); int Delete(T entity); int Delete(IEnumerable<int> ids, bool isOpenTrans); int Delete(int id, bool isOpenTrans); int Delete(object value, bool isOpenTrans); int Delete(T entity, bool isOpenTrans); int DeleteBatch(IEnumerable<T> list, bool isOpenTrans); int GetCount(); int GetCount(bool isOpenTrans); int GetCount(T entity); int GetCount(T entity, bool isOpenTrans); List<T> GetList(); List<T> GetList(bool isOpenTrans); List<T> GetList(T entity); List<V> GetList<V>(T entity) where V : class, new(); List<T> GetList(T entity, bool isOpenTrans); List<V> GetList<V>(T entity, bool isOpenTrans) where V : class, new(); List<V> GetList<V>(T entity, int pageSize, int pageIndex, out int rowCount) where V : class, new(); List<T> GetList(T entity, int pageSize, int pageIndex, out int rowCount); List<V> GetList<V>(T entity, int pageSize, int pageIndex, out int rowCount, bool isOpenTrans) where V : class, new(); List<T> GetList(T entity, int pageSize, int pageIndex, out int rowCount, bool isOpenTrans); T GetSingle(int id); T GetSingle(object value); T GetSingle(T entity); V GetSingle<V>(T entity) where V : class, new(); T GetSingle(int id, bool isOpenTrans); T GetSingle(T entity, bool isOpenTrans); V GetSingle<V>(T entity, bool isOpenTrans) where V : class, new(); DataTable GetTable(); DataTable GetTable(bool isOpenTrans); DataTable GetTable(T entity); DataTable GetTable(T entity, bool isOpenTrans); DataTable Group(T entity); DataTable Group(T entity, bool isOpenTrans); IEnumerable<Linq.IGrouping<TKey, T>> Group<TKey>(T entity, Func<T, TKey> keySelector); V Max<V>(T entity); V Max<V>(T entity, bool isOpenTrans); V Min<V>(T entity); V Min<V>(T entity, bool isOpenTrans); V Sum<V>(T entity); V Sum<V>(T entity, bool isOpenTrans); List<T> Top(T entity, int pageSize); List<V> Top<V>(T entity, int pageSize) where V : class, new(); List<V> Top<V>(T entity, int pageSize, bool isOpenTrans) where V : class, new(); List<T> Top(T entity, int pageSize, bool isOpenTrans); List<V> Top<V>(T entity, int skipSize, int pageSize) where V : class, new(); List<T> Top(T entity, int skipSize, int pageSize); List<T> Top(T entity, int skipSize, int pageSize, bool isOpenTrans); List<V> Top<V>(T entity, int skipSize, int pageSize, bool isOpenTrans) where V : class, new(); int Update(List<T> list); int Update(T entity); int Update(List<T> list, bool isOpenTrans); int Update(T entity, bool isOpenTrans); }
查看上面的类,里面定义了非常多的方法,包含了增删改查所用的常用方法,使用该接口中的方法可以满足大部分的增删改查操作,当然特别复杂的SQL操作是不能完成的,我们还提供了其他的方式来操作,后面详细说明。
因为IInStorage 接口继承了IDbHelper<T>接口,那IInStorage也用于了IDbHelper中的所有属性和方法,但是这里是一个泛型接口,通过制定操作类我们可以约束该接口操作哪个类,而不会没有方向性的去操作其他无关类。从接口IDbHelper<T>中我们可以看到T 是使用了泛型约束的,T必须继承BaseEntity 类。刚好和上面的实体映射类吻合。
四. 数据访问
数据访问层要实现数据访问接口,先看看一下代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Git.Framework.ORM; using Git.Framework.MsSql; using Git.Storage.Entity.InStorage; using Git.Storage.IDataAccess.InStorage; namespace Git.Storage.DataAccess.InStorage { public partial class InStorageDataAccess : DbHelper<InStorageEntity>, IInStorage { public InStorageDataAccess() { } } }
以上代码也没有什么特别的地方,只是这个类继承了一个实体类,并且实现了一个接口
public partial class InStorageDataAccess : DbHelper<InStorageEntity>, IInStorage
DbHelper<InStorageEntity> 这个是框架中自带的一个泛型类,该泛型类实现了泛型接口IDbHelper<T> , 上面这个类还实现了接口IInStorage接口,所以这个类也必须实现
IInStorage中的扩展的方法。
五. 如何使用
怎么使用以上框架操作数据库,其实非常简单了,使用接口new 一个子类对象即可,没有其他特殊的方式,也没有使用IOC注入等。看看下面的代码
public IInStorage InStorage { get { return new InStorageDataAccess(); } } -------------------------------------------------------------------------------- public override string Create(InStorageEntity entity, List<InStorDetailEntity> list) { using (TransactionScope ts = new TransactionScope()) { int line = 0; entity.OrderNum = entity.OrderNum.IsEmpty() ? (new TNumProivder()).GetSwiftNum(typeof(InStorageEntity), 5) : entity.OrderNum; entity.IncludeAll(); if (!list.IsNullOrEmpty()) { list.ForEach(a => { a.IncludeAll(); a.OrderNum = entity.OrderNum; }); entity.Num = list.Sum(q => q.Num); entity.Amount = list.Sum(a => a.Amount); line = this.InStorage.Add(entity); line += this.InStorDetail.Add(list); } ts.Complete(); return line > 0 ? EnumHelper.GetEnumDesc<EReturnStatus>(EReturnStatus.Success) : string.Empty; } }
上面的代码用于创建一个单据的,单据在系统中一个是一个抽象类,这里不过多的累述。
entity.IncludeAll(); 注意到这个语句,在插入数据的时候要调用这个方法,这个方法用于自定哪些字段插入到数据库,后面详述具体过程。
this.InStorage.Add(entity); 最终我们只需要将调用这个方法Add 就可以将实体InStorageEntity的实例数据插入到对应的数据库表InStorage中。
六. 总结
要使用此框架可以整理为一下结果步骤:
(1)新建工程并且引入框架中提供的类库
(2)在主程序中配置相应的配置文件,用于连接数据库[配置文章必须遵循规则以及存放路径]
(3)编写或者生产实体映射类,数据访问接口,数据访问类三部分,数据访问接口有时候也可以省略
(4)创建接口实例调用接口中提供的方法
总的来说该操作相对于ADO.NET还是比较方便的,但是和目前一些较为流行的框架比如Linq to SQL,Entity Framework 等比较还是比较有差距,所以还需要不断的努力完善。
目前公司一直在使用这个框架,效果还不错。如果你要说这些东西什么什么框架都可以实现,为什么要自己搞一套,我只能说这是积累!
Git.Framework 框架随手记--ORM项目工程,布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/qingyuan/p/3706121.html