Castle is. an open-source project of the. NET platform, from the data access framework ORM to the IOC container, to the MVC Framework and AOP at the WEB layer, basically covers everything in the entire development process, it provides good services for us to quickly build enterprise-level applications.
Simple O/R ing provided by ActiveRecord
ActiveRecord is a data access framework provided by Castle. It encapsulates nhib.pdf operations at the underlying layer and uses features to replace ing files, the concise O/R ing provided by the O/R ing will surprise you that the original implementation of the persistent data layer is so simple.
1. Create an object class and complete the ing to the database.
Namespace ZDS. Test. Model
{
[ActiveRecord ("Users")]
Public class Users: ActiveRecordBase <Users> // inherits the ActiveRecordBase of Castle.
{
Private int oid;
[PrimaryKey (PrimaryKeyType. Identity, "Oid")]
Public int Oid
{
Get {return oid ;}
Set {oid = value ;}
}
Private string name;
[Property ("Name", Length = 100, NotNull = true)]
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
Private string sex;
[Property ("Sex")]
Public string Sex
{
Get {return sex ;}
Set {sex = value ;}
}
Private string age;
[Property ("Age")]
Public string Age
{
Get {return age ;}
Set {age = value ;}
}
}
}
You may have noticed that the Property [Property ()] is added to each Property.
If the attribute name and field name are the same, you can enter a field name in [Property.
Here, the [PrimaryKey] feature is used to specify the Id as the primary key, and the primary key type is auto-incrementing. PrimaryKeyType. Identity is used to describe.
In the next step, we add static operation methods for the object class as needed. For example, Create (), Update (), Delete (), Find (), FindAll () and other methods are directly inherited from the ActiveRecordBase base class. The sample code is as follows:
2. Establish the Implementation Layer class and directly inherit from the ActiveRecordBase Method
Namespace ZDS. Test. Component
{
Public class UserComponent: ActiveRecordBase <Users> // You must inherit ActiveRecordBase.
{
Public static void Create (Users user)
{
User. Create ();
}
Public static IList <Users> QueryAll ()
{
Return (IList <Users>) FindAll (typeof (Users ));
}
Public static Users QueryByOid (int Oid)
{
// Return (Users) FindByPrimaryKey (typeof (Users), Oid );
Return (Users) Find (Oid); // both methods can be used here.
}
}
}
3. Create a test program to call these methods
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
GridViewDataBind ();
}
}
/// <Summary>
/// Add a user
/// </Summary>
Protected void Button2_Click (object sender, EventArgs e)
{
Users user = new Users ();
User. Name = this. TextBox1.Text. Trim ();
User. Sex = this. TextBox2.Text. Trim ();
User. Age = this. TextBox3.Text. Trim ();
UserComponent. Create (user );
GridViewDataBind ();
}
/// <Summary>
/// Query all
/// </Summary>
Public void GridViewDataBind ()
{
This. GridView1.DataSource = UserComponent. QueryAll ();
This. GridView1.DataBind ();
}
/// <Summary>
/// Query but records
/// </Summary>
Protected void Button3_Click (object sender, EventArgs e)
{
Users user = UserComponent. QueryByOid (int. Parse (this. TextBox4.Text. Trim ()));
This. Label1.Text = string. Format ("No.: {0}, name: {1}, Gender: {2}, age: {3 }",
User. Oid,
User. Name,
User. Sex,
User. Age );
}
}
This note is recorded first. You have to configure some files for normal running of the program. For detailed configuration, see Castle learning notes ---- Castle. ActiveRecord configuration.
Http://www.cnblogs.com/beniao/archive/2007/11/18/963392.html