框架-ORM-NHbibernate 框架

 

 

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

 

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

1.   数据库E-R 图

 

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

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

如果彼此在一起。

层超类型  EntityBase

 1 using System.Collections.Generic;
 2 
 3 namespace Agathas.Storefront.Infrastructure.Domain
 4 {
 5     public abstract class EntityBase
 6     {
 7         private List _brokenRules = new List();
 8 
 9         public TId Id { get; set; }
10 
11         protected abstract void Validate();
12 
13         public IEnumerable 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
29                && this == (EntityBase)entity;
30         }
31 
32         public override int GetHashCode()
33         {
34             return this.Id.GetHashCode();
35         }
36 
37         public static bool operator ==(EntityBase entity1,
38            EntityBase 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 entity1,
59             EntityBase 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 "1.0" encoding="utf-8" ?>
 2 "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" column="ProductTitleId" type="int" unsaved-value="0">
 9       class="native">
10     
11 
12     "Name">
13       "Name" sql-type="nvarchar(50)" not-null="true">
14     
15 
16     "Price">
17       "Price" sql-type="decimal(18, 2)" not-null="true">
18     
19 
20     "Brands"
21                  class="Brand"
22                  column="BrandId"
23                  not-null="true">    
24       
25     
26 
27     "Color"
28                 class="ProductColor"
29                 column="ColorId"
30                 not-null="true">
31       
32     
33 
34     "Category"
35                  class="Agathas.Storefront.Model.Categories.Category"
36                  not-null="true"
37                  lazy="false">
38       
39     
40 
41     "Product"
42          inverse="true"
43          cascade="all"
44          lazy="false"
45          fetch="join">
46       "ProductTitleId">
47       class="Product">
48 
49     
50     
51   class>
52   
53 
View Code

 

Product  一对多关系

 

数据库 E-R 关系

 

 

 

将所有的 NHibernate XML 配置文件的构建动作修改为 Embeded Resource,这样 NHibernate 框架就能够找到映射元数据。

 

 配置好业务实体与数据表之间的映射关系,就可以编写 NHibernate Repository.

转载于:https://www.cnblogs.com/masterSoul/p/7599573.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部