zoukankan      html  css  js  c++  java
  • NHibernate 3.2 mapping by code

    NHibernate 3.2 will come with its own embedded mapping by code.GalloAlHornoConPapas

    If you want know it is not based in fluent-interface, instead it is based on “loquacious”. That said you should understand that it has nothing related with Fluent-NHibernate.

    The main idea under the NHibernate’s “sexy mapping” came from my dear ConfORM. In the past year the no conformist red man was running a lot and now I’m ready to transfer most of ConfORM’s intelligence directly inside NHibernate. To continue reading this post you have to run this song.

    It’s simple (I’m too sexy)

    Simple model

    public class MyClass
    {
        public int Id { getset; }
        public string Something { getset; }
    }

    Simple mapping

    var mapper = new ModelMapper();
    mapper.Class<MyClass>(ca =>
    {
        ca.Id(x => x.Id, map =>
        {
            map.Column("MyClassId");
            map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
        });
        ca.Property(x => x.Something, map => map.Length(150));
    });

     

    It’s flexible (I’m too sexy for my application)

    You can organize your mapping as you want, class-by-class, different concerns about a class in different places and so on… (yes!! if you are a ConfORM user you know the concept)

    var mapper = new ModelMapper();
    mapper.Class<MyClass>(ca =>
    {
        ca.Id(x => x.Id, map =>
        {
            map.Column("MyClassId");
        });
        ca.Id(x => x.Id, map =>
        {
            map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
        });
        ca.Property(x => x.Something);
        ca.Property(x => x.Something, map => map.Length(150));
    });

    As you can see there is no problem duplicating the declaration of a mapped element and you can even do the same with the whole mapping of a class in two different places:

    mapper.Class<MyClass>(ca =>
    {
        ca.Id(x => x.Id, map =>
        {
            map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
        });
        ca.Property(x => x.Something);
    });

    mapper.Class<MyClass>(ca =>
    {
        ca.Id(x => x.Id, map =>
        {
            map.Column("MyClassId");
        });
        ca.Property(x => x.Something, map => map.Length(150));
    });

     

    It’s conventions friendly (too sexy by far)

    You can apply “democratic” conventions

    var mapper = new ModelMapper();

    mapper.BeforeMapClass += 
        (mi, t, map) => map.Id(x => x.Column((t.Name+"id").ToUpper()));
    mapper.BeforeMapProperty += 
        (mi, propertyPath, map) => map.Column(propertyPath.ToColumnName().ToUpper());

    mapper.Class<MyClass>(ca =>
    {
        ca.Id(x => x.Id, map => { });
        ca.Property(x => x.Something);
    });

    or you may want “republican” conventions

    var mapper = new ModelMapper();

    mapper.AfterMapClass +=
        (mi, t, map) => map.Id(x => x.Column((t.Name + "id").ToUpper()));
    mapper.AfterMapProperty +=
        (mi, propertyPath, map) => map.Column(propertyPath.ToColumnName().ToUpper());

    mapper.Class<MyClass>(ca =>
    {
        ca.Id(x => x.Id, map => map.Column("Whatever"));
        ca.Property(x => x.Something, map => map.Column("Whatever"));
    });

    NHibernate 3.2 too sexy by far!!!

  • 相关阅读:
    IIS网站应用偶尔出现"服务不可用"或者显示乱码字体
    mac os下切换pip3国内源并安装requests库
    mysql5.6运行一段时间之后网站页面出现乱码解决办法
    mac pro下安装安装 SymPy 和 matplotlib报错解决方案
    python3汉诺塔简单实现代码
    用python提取xml里面的链接源码
    Mac环境下 jieba 配置记录
    AngularJS 整理学习
    Java有关List的stream基本操作
    Callable的简单使用
  • 原文地址:https://www.cnblogs.com/sunjie9606/p/2169728.html
Copyright © 2011-2022 走看看