MyORM的使用(一)

 之前把自己写过的一个简单ORM类库介绍了下,考虑到代码会有改动而blog中编辑代码太麻烦,在CodePlex上建了一个项目:MyORM,相关代码和文件维护起来方便不少。

CodePlex下载地址http://www.codeplex.com/MyOrm/Release/ProjectReleases.aspx?ReleaseId=18323,包含了源码、一个Sample工程和CodeSmith的模板文件。

简单说一下自己写ORM的目的,个人不喜欢把实体对象搞得太复杂,因为在系统中实体是作为数据载体存在的,如果还要让它负责数据库操作、实体关系,往往会造成很多问题。

另外就是现有的ORM并不是太好用,比如说在显示到界面时不方便,或者效率太低,我的方法是给实体类型定义一个View用来显示。还有查询也不是很好用,我的想法是简单的查询可以定义统一的查询方式,复杂的查询通过自己写SQL然后封装成方法,如果复杂的查询还要定义成统一的方式那就另外自己开发吧。

 

以Northwind的数据库做例子,实体的定义可能是下面这样:

  1.     [Table("Products")]
  2.     [Serializable]
  3.     public class Products 
  4.     {
  5.         #region Member Variables        
  6.         private int productID;
  7.         private string productName;
  8.         private int? supplierID;
  9.         private int? categoryID;
  10.         private string quantityPerUnit;
  11.         private decimal? unitPrice;
  12.         private short? unitsInStock;
  13.         private short? unitsOnOrder;
  14.         private short? reorderLevel;
  15.         private bool discontinued;
  16.         #endregion
  17.         #region Public Properties
  18.         [Column(IsPrimaryKey = true)]
  19.         public int ProductID
  20.         {
  21.             get { return productID; }           
  22.             set { productID = value; }
  23.         }
  24.         
  25.         [Column]
  26.         public string ProductName
  27.         {
  28.             get { return productName; }         
  29.             set { productName = value; }
  30.         }
  31.         
  32.         [Column]
  33.         public int? SupplierID
  34.         {
  35.             get { return supplierID; }          
  36.             set { supplierID = value; }
  37.         }
  38.         
  39.         [Column]
  40.         public int? CategoryID
  41.         {
  42.             get { return categoryID; }          
  43.             set { categoryID = value; }
  44.         }
  45.         
  46.         [Column]
  47.         public string QuantityPerUnit
  48.         {
  49.             get { return quantityPerUnit; }         
  50.             set { quantityPerUnit = value; }
  51.         }
  52.         
  53.         [Column]
  54.         public decimal? UnitPrice
  55.         {
  56.             get { return unitPrice; }           
  57.             set { unitPrice = value; }
  58.         }
  59.         
  60.         [Column]
  61.         public short? UnitsInStock
  62.         {
  63.             get { return unitsInStock; }            
  64.             set { unitsInStock = value; }
  65.         }
  66.         
  67.         [Column]
  68.         public short? UnitsOnOrder
  69.         {
  70.             get { return unitsOnOrder; }            
  71.             set { unitsOnOrder = value; }
  72.         }
  73.         
  74.         [Column]
  75.         public short? ReorderLevel
  76.         {
  77.             get { return reorderLevel; }            
  78.             set { reorderLevel = value; }
  79.         }
  80.         
  81.         [Column]
  82.         public bool Discontinued
  83.         {
  84.             get { return discontinued; }            
  85.             set { discontinued = value; }
  86.         }
  87.         
  88.         #endregion
  89.     }

而在显示到界面的时候直接显示SupplierID和CategoryID是不合适的,而应该显示对应对象的名称或者其他内容,因此需要再定义一个View:

  1.     [TableJoin(typeof(Categories), "CategoryID", AliasName = ProductsView.Category)]
  2.     [TableJoin(typeof(Suppliers), "SupplierID", AliasName = ProductsView.Supplier)]
  3.     public class ProductsView : Products
  4.     {
  5.         #region Constant        
  6.         public const string Category = "Category";
  7.         public const string Supplier = "Supplier";
  8.         #endregion
  9.         
  10.         #region Member Variables        
  11.         private string category_CategoryName;           
  12.         private string category_Description;            
  13.         private byte[] category_Picture;            
  14.         private string supplier_CompanyName;            
  15.         private string supplier_ContactName;            
  16.         private string supplier_ContactTitle;           
  17.         private string supplier_Address;            
  18.         private string supplier_City;           
  19.         private string supplier_Region;         
  20.         private string supplier_PostalCode;         
  21.         private string supplier_Country;            
  22.         private string supplier_Phone;          
  23.         private string supplier_Fax;            
  24.         private string supplier_HomePage;           
  25.         #endregion
  26.         #region Public Properties
  27.         [Column("CategoryName", Foreign = ProductsView.Category, ColumnMode = ColumnMode.Read)]
  28.         public string Category_CategoryName
  29.         {
  30.             get { return category_CategoryName; }           
  31.             set { category_CategoryName = value; }
  32.         }
  33.         
  34.         [Column("Description", Foreign = ProductsView.Category, ColumnMode = ColumnMode.Read)]
  35.         public string Category_Description
  36.         {
  37.             get { return category_Description; }            
  38.             set { category_Description = value; }
  39.         }
  40.         
  41.         [Column("Picture", Foreign = ProductsView.Category, ColumnMode = ColumnMode.Read)]
  42.         public byte[] Category_Picture
  43.         {
  44.             get { return category_Picture; }            
  45.             set { category_Picture = value; }
  46.         }
  47.         
  48.         [Column("CompanyName", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  49.         public string Supplier_CompanyName
  50.         {
  51.             get { return supplier_CompanyName; }            
  52.             set { supplier_CompanyName = value; }
  53.         }
  54.         
  55.         [Column("ContactName", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  56.         public string Supplier_ContactName
  57.         {
  58.             get { return supplier_ContactName; }            
  59.             set { supplier_ContactName = value; }
  60.         }
  61.         
  62.         [Column("ContactTitle", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  63.         public string Supplier_ContactTitle
  64.         {
  65.             get { return supplier_ContactTitle; }           
  66.             set { supplier_ContactTitle = value; }
  67.         }
  68.         
  69.         [Column("Address", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  70.         public string Supplier_Address
  71.         {
  72.             get { return supplier_Address; }            
  73.             set { supplier_Address = value; }
  74.         }
  75.         
  76.         [Column("City", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  77.         public string Supplier_City
  78.         {
  79.             get { return supplier_City; }           
  80.             set { supplier_City = value; }
  81.         }
  82.         
  83.         [Column("Region", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  84.         public string Supplier_Region
  85.         {
  86.             get { return supplier_Region; }         
  87.             set { supplier_Region = value; }
  88.         }
  89.         
  90.         [Column("PostalCode", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  91.         public string Supplier_PostalCode
  92.         {
  93.             get { return supplier_PostalCode; }         
  94.             set { supplier_PostalCode = value; }
  95.         }
  96.         
  97.         [Column("Country", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  98.         public string Supplier_Country
  99.         {
  100.             get { return supplier_Country; }            
  101.             set { supplier_Country = value; }
  102.         }
  103.         
  104.         [Column("Phone", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  105.         public string Supplier_Phone
  106.         {
  107.             get { return supplier_Phone; }          
  108.             set { supplier_Phone = value; }
  109.         }
  110.         
  111.         [Column("Fax", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  112.         public string Supplier_Fax
  113.         {
  114.             get { return supplier_Fax; }            
  115.             set { supplier_Fax = value; }
  116.         }
  117.         
  118.         [Column("HomePage", Foreign = ProductsView.Supplier, ColumnMode = ColumnMode.Read)]
  119.         public string Supplier_HomePage
  120.         {
  121.             get { return supplier_HomePage; }           
  122.             set { supplier_HomePage = value; }
  123.         }
  124.         
  125.         #endregion
  126.     }

然后在使用的时候像下面这样:

  1. dataGridView1.DataSource = new ProductsViewDAO().Search(null);

就可以显示所有ProductsView的内容。Products和ProductsView是通过CodeSmith生成,完全可以按照自己的需要修改。

ProductsDAO和ProductsViewDAO下次再介绍。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值