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.         }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章