Series of articles
[NHibernate] ISessionFactory configuration
introduction
Persistent class is a class of applications to solve business problems (for example, Customer in electronic trading programs and Order). Persistent class, just as its name implies, is short-lived, it will be a persistent instance stored in the database.
If these simple rules in line with the class best, these rules can work NHibernate is Plain Old CLR Object (POCO, plain old CLR object) programming model.
POCO simple example
A cat with a class description:
1 public class Cat 2 { 3 private long _id; // identifier 4 private string _name; 5 private DateTime _birthdate; 6 private Cat _mate; 7 private char _sex; 8 private float _weight; 9 public long Id 10 { 11 get { return _id; } 12 set { _id = value; } 13 } 14 public string Name 15 { 16 get { return _name; } 17 set { _name = value; } 18 } 19 public DateTime Birthdate 20 { 21 get { return _birthdate; } 22 set { _birthdate = value; } 23 } 24 public Cat Mate 25 { 26 get { return _mate; } 27 set { _mate = value; } 28 } 29 public char Sex 30 { 31 get { return _sex; } 32 set { _sex = value; } 33 } 34 public float Weight 35 { 36 get { return _weight; } 37 set { _weight = value; } 38 } 39 40 }
Four principles
For persistent fields statement accessor (getters and setters)
Cat declares accessor gettes and setters for its all persistent fields. Direct access is used instead of field access is a good habit. However, may also be used by NHibernate field (field).
Properties need not be declared public. NHibernate can default, protected, internal, or private property to perform equally persistent.
Implement a default constructor (Constructor)
Cat no have an explicit argument constructor. All persistent classes must have a default constructor (which may be non-public), so can be used NHibernate Construector.Invoke () to instantiate them.
Providing an identification attribute (identifier property) (optional)
Not recommend the use of sealed classes (optional)
Implementation inheritance (Inheritance)
Implement Equals () and GetHashCode ()
Persistence lifecycle callbacks (callbacks) (Lifecycle) in
Check the validity of (Validatable) callback
XML attribute with alternative
to sum up
This article describes several rules persistent class.
This article from
"NHibernate Chinese Documents"
- Blog address: http://www.cnblogs.com/wolf-sun/
blog Copyright: If there is nothing wrong or the wrong place text experts also hope you noted, so as not to harm the younger generation. If you think this article to help you look as good as [Recommended]! If you have a better suggestion, it is better to discuss with the message, and common progress! Thanks again for your patience to read this article.
Series of articles
[NHibernate] ISessionFactory configuration
introduction
Persistent class is a class of applications to solve business problems (for example, Customer in electronic trading programs and Order). Persistent class, just as its name implies, is short-lived, it will be a persistent instance stored in the database.
If these simple rules in line with the class best, these rules can work NHibernate is Plain Old CLR Object (POCO, plain old CLR object) programming model.
POCO simple example
A cat with a class description:
1 public class Cat 2 { 3 private long _id; // identifier 4 private string _name; 5 private DateTime _birthdate; 6 private Cat _mate; 7 private char _sex; 8 private float _weight; 9 public long Id 10 { 11 get { return _id; } 12 set { _id = value; } 13 } 14 public string Name 15 { 16 get { return _name; } 17 set { _name = value; } 18 } 19 public DateTime Birthdate 20 { 21 get { return _birthdate; } 22 set { _birthdate = value; } 23 } 24 public Cat Mate 25 { 26 get { return _mate; } 27 set { _mate = value; } 28 } 29 public char Sex 30 { 31 get { return _sex; } 32 set { _sex = value; } 33 } 34 public float Weight 35 { 36 get { return _weight; } 37 set { _weight = value; } 38 } 39 40 }
Four principles
For persistent fields statement accessor (getters and setters)
Cat declares accessor gettes and setters for its all persistent fields. Direct access is used instead of field access is a good habit. However, may also be used by NHibernate field (field).
Properties need not be declared public. NHibernate can default, protected, internal, or private property to perform equally persistent.
Implement a default constructor (Constructor)
Cat no have an explicit argument constructor. All persistent classes must have a default constructor (which may be non-public), so can be used NHibernate Construector.Invoke () to instantiate them.
Providing an identification attribute (identifier property) (optional)
Not recommend the use of sealed classes (optional)
Implementation inheritance (Inheritance)
Implement Equals () and GetHashCode ()
Persistence lifecycle callbacks (callbacks) (Lifecycle) in
Check the validity of (Validatable) callback
XML attribute with alternative
to sum up
This article describes several rules persistent class.
This article from
"NHibernate Chinese Documents"