zoukankan      html  css  js  c++  java
  • FluentNhibernate 组件component及应用

    组件的概念比较简单,用途就是当作公共的组件来用,只定义一次,可以重复使用,
    比如 类:Person 和 company,product 等等都有名字。人的名字有可以细分为firstname,lastname,fullname。
    定义好Name以后这个name就可以让其他类似的类来复用

    1.定义组件实体类Name
         public class Name
        {
             public virtual string FirstName { set;get;}
             public virtual string LastName  { set;get;}
             public virtual string FullName { set; get; }

        }
    2.映射组件类
     public class NameMap:ComponentMap<Name>
        {
             public NameMap()
             {
                 Map(x => x.FirstName);
                 Map(x => x.LastName);
                 Map(x => x.FullName);
             }
        }
    3.修改原来的Person类,用组件来代替原来person类中firstname,lastname,fullname
     public class PersonMap:ClassMap<Person>
        {
            public PersonMap()
            {
                Table("Person");
                Id(x => x.ID);
                Component(x => x.Name);
                //Map(x => x.Name);
                Map(x => x.Sex);
                Map(x => x.Address);
            }
        }
    4.最后调用,添加数据记录
      Person p = new Person();
                p.Name = new Name() { FirstName = "a", LastName = "b", FullName = "ab" };
                p.Sex = "xxxxx";
                p.Address = "aaaaaaaaaa";

                using (var session = SessionFactory.CreateSession())
                {
                    session.Save(p);
                   
                    session.Flush();
                }

    本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

  • 相关阅读:
    计算最大公约数 Exercise05_14
    求满足n^2>12000的n的最大值 Exercise05_13
    依赖注入(DI)
    spring容器
    基于xml文件的bean的配置
    小试牛刀 spring的HelloWorld
    spring 装配Bean
    spring介绍
    hibernate相关类与接口
    hibernate 预习
  • 原文地址:https://www.cnblogs.com/zjypp/p/2319306.html
Copyright © 2011-2022 走看看