NHibernate小结之六

如果你不喜欢用XML来配置,一个好的办法是用attribute来配。这个attribute的包在sourceforge有下的,2.0版的也有了个beta的可以用,看来还是不错的。

 

在SharpDevlop里面对attribute的支持不太好,提示功能不强,VS2008要好一些,但感觉还是没有XML好。

 

例子还是上次的三个关系里面的,但这次只是给出了带attribute的cs文件,后面有一个生成xml的代码。

先还是1对1的例子。

  1.     [Class]
  2.     public class Person
  3.     {
  4.         public Person()
  5.         {
  6.         }
  7.         [Id(0, TypeType=typeof(int))]
  8.         [Generator(1, Class="native")]
  9.         virtual public int ID {getset;}
  10.         [Property]
  11.         virtual public int Age {getset;}
  12.         [Property]
  13.         virtual public string Name {getset;}
  14.         [OneToOne(ClassType=typeof(Passport))]
  15.         virtual public Passport Passport {getset;}
  16.     }
  1.     [Class]
  2.     public class Passport
  3.     {
  4.         public Passport()
  5.         {
  6.         }
  7.         [Id(0, TypeType=typeof(int))]
  8.         [Generator(1, Class="foreign")]
  9.         [Param(2, Name="property", Content="Person")]
  10.         virtual public int ID {getset;}
  11.         [Property]
  12.         virtual public string Expire {get;set;}
  13.         [OneToOne(ClassType=typeof(Person))]
  14.         virtual public Person Person{get;set;}
  15.     }

 

然后是1对多的

 

  1.     [Class]
  2.     public class Parent
  3.     {
  4.         public Parent()
  5.         {
  6.         }
  7.         [Id(0, TypeType = typeof(int))]
  8.         [Generator(1, Class = "native")]
  9.         virtual public int ID { getset; }
  10.         [Set(0, Inverse = true, Lazy = true)]
  11.         [Key(1, Column = "parent_id")]
  12.         [OneToMany(2, ClassType = typeof(Child))]
  13.         virtual public ISet<Child> Children { getset; }
  14.     }

 

  1.     [Class]
  2.     public class Parent
  3.     {
  4.         public Parent()
  5.         {
  6.         }
  7.         [Id(0, TypeType = typeof(int))]
  8.         [Generator(1, Class = "native")]
  9.         virtual public int ID { getset; }
  10.         [Set(0, Inverse = true, Lazy = true)]
  11.         [Key(1, Column = "parent_id")]
  12.         [OneToMany(2, ClassType = typeof(Child))]
  13.         virtual public ISet<Child> Children { getset; }
  14.     }

 

最后是多对多的

 

  1.     [Class]
  2.     public class Parent
  3.     {
  4.         public Parent()
  5.         {
  6.         }
  7.         [Id(0, TypeType = typeof(int))]
  8.         [Generator(1, Class = "native")]
  9.         virtual public int ID { getset; }
  10.         [Set(0, Inverse = true, Lazy = true)]
  11.         [Key(1, Column = "parent_id")]
  12.         [OneToMany(2, ClassType = typeof(Child))]
  13.         virtual public ISet<Child> Children { getset; }
  14.     }

 

  1.     [Class]
  2.     public class Lesson
  3.     {
  4.         public Lesson()
  5.         {
  6.         }
  7.         [Set(0, Name="students", Table="student_lesson")]
  8.         [Key(1, Column="student_id")]
  9.         [ManyToMany(2, Column="lesson_id", Class="Demo5.Domain.Student, Demo5")]
  10.         virtual public ISet<Student> students{get;set;}
  11.         [Id(0, TypeType = typeof(int))]
  12.         [Generator(1, Class = "native")]
  13.         virtual public int ID{get;set;}
  14.         virtual public string LessonName{get;set;}
  15.         
  16.     }

 

 

最后给出编译的代码

 

  1.         [Test]
  2.         public void TestCreateAttibute()
  3.         {
  4.             // TODO: Add your test.
  5.             var stream = new MemoryStream();
  6.             HbmSerializer.Default.Validate = true;
  7.             HbmSerializer.Default.Serialize(stream, Assembly.GetExecutingAssembly());
  8.             stream.Position = 0;
  9.             for(int i=0; i < stream.Length; i++)
  10.                 Console.Write((char)stream.GetBuffer()[i]);
  11.             stream.Position = 0;
  12.             var cfg = new Configuration();
  13.             cfg.Configure().AddInputStream(stream);
  14.             new SchemaExport(cfg).Create(truefalse);
  15.         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值