前面已经简单介绍过了该框架(不一定是框架),本文开始重点记录其使用过程。可能记录的内容不是太详尽,框架也可能非常烂,但是里面的代码句句是实战项目所得。本文非教唆之类的文章,也非批判之类的文章,更不是炫技之类的文章,只是工作的记录和总结,希望能够给大家一些启迪,忘诸位勿喷!
一. 组建项目需要的几个部分
.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; } } }