码迷,mamicode.com
首页 > 其他好文 > 详细

NHbibernate ORM 框架

时间:2017-09-27 00:50:45      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:namespace   lap   2-2   cte   自身   type   view   ber   接下来   

 

 

NHibernate ORM 框架是从 Java Hibernate 移植到 .NET 平台的 对象关系映射框架

 

项目验证测试,基于 . Net 3.5 平台  NHibernate   MS-SqlServce 

1.   数据库E-R 图

技术分享

 

以 Size ( 数据库表) ProductSize ( Class ) 为例 ,通过 NHibernate 中的 XML 文件文件  Brand.hbm.xml

这个 XML 文件存放着一些元数据,NHibernate 框架通过这些元数据了解领域模型中的业务实体与数据模型中的数据表

如果彼此在一起。

层超类型  EntityBase<Tid>

技术分享
 1 using System.Collections.Generic;
 2 
 3 namespace Agathas.Storefront.Infrastructure.Domain
 4 {
 5     public abstract class EntityBase<TId>
 6     {
 7         private List<BusinessRule> _brokenRules = new List<BusinessRule>();
 8 
 9         public TId Id { get; set; }
10 
11         protected abstract void Validate();
12 
13         public IEnumerable<BusinessRule> GetBrokenRules()
14         {
15             _brokenRules.Clear();
16             Validate();
17             return _brokenRules;
18         }
19 
20         protected void AddBrokenRule(BusinessRule businessRule)
21         {
22             _brokenRules.Add(businessRule);
23         }
24 
25         public override bool Equals(object entity)
26         {
27             return entity != null
28                && entity is EntityBase<TId>
29                && this == (EntityBase<TId>)entity;
30         }
31 
32         public override int GetHashCode()
33         {
34             return this.Id.GetHashCode();
35         }
36 
37         public static bool operator ==(EntityBase<TId> entity1,
38            EntityBase<TId> entity2)
39         {
40             if ((object)entity1 == null && (object)entity2 == null)
41             {
42                 return true;
43             }
44 
45             if ((object)entity1 == null || (object)entity2 == null)
46             {
47                 return false;
48             }
49 
50             if (entity1.Id.ToString() == entity2.Id.ToString())
51             {
52                 return true;
53             }
54 
55             return false;
56         }
57 
58         public static bool operator !=(EntityBase<TId> entity1,
59             EntityBase<TId> entity2)
60         {
61             return (!(entity1 == entity2));
62         }
63     }
64 
65 }
View Code

 

类 ProductSize

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using Agathas.Storefront.Infrastructure.Domain;
 7 
 8 namespace Agathas.Storefront.Model.Products
 9 {
10     public class ProductSize:EntityBase<int>, IProductAttribute
11     {
12         public string Name { get; set; }
13 
14         protected override void Validate()
15         {
16             throw new NotImplementedException();
17         }
18     }
19 }
View Code

 

技术分享

 

在 XML 文件配置: 此为一个简单的 匹配

技术分享

 

 接下来 以  ProductTitle ,其包含 Brand, Categories, Colors  外键 ,1对多关系, 且自身为 Product 的多对一关系

productTitle 类

技术分享

 

技术分享

 

技术分享

 

继续配置 Category

技术分享

 

 技术分享

 

 技术分享

技术分享

 

 

技术分享
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
 3                    namespace="Agathas.Storefront.Model.Products"
 4                    Assembly="Agathas.Storefront.Model">
 5 
 6   <class name="ProductTitle" table="ProductTitles" lazy="false">
 7 
 8     <id name="Id" column="ProductTitleId" type="int" unsaved-value="0">
 9       <generator class="native"></generator>
10     </id>
11 
12     <property name="Name">
13       <column name="Name" sql-type="nvarchar(50)" not-null="true"></column>
14     </property>
15 
16     <property name="Price">
17       <column name="Price" sql-type="decimal(18, 2)" not-null="true"></column>
18     </property>
19 
20     <many-to-one name="Brands"
21                  class="Brand"
22                  column="BrandId"
23                  not-null="true">    
24       
25     </many-to-one>
26 
27     <may-to-one name="Color"
28                 class="ProductColor"
29                 column="ColorId"
30                 not-null="true">
31       
32     </may-to-one>
33 
34     <many-to-one name="Category"
35                  class="Agathas.Storefront.Model.Categories.Category"
36                  not-null="true"
37                  lazy="false">
38       
39     </many-to-one>
40 
41     <bag name="Product"
42          inverse="true"
43          cascade="all"
44          lazy="false"
45          fetch="join">
46       <key column="ProductTitleId"></key>
47       <one-to-many class="Product"></one-to-many>
48 
49     </bag>
50     
51   </class>
52   
53 </hibernate-mapping>
View Code

 

NHbibernate ORM 框架

标签:namespace   lap   2-2   cte   自身   type   view   ber   接下来   

原文地址:http://www.cnblogs.com/masterSoul/p/7599573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!