Ii. Create a class
Now let's create a Class Based on the table. here you will see how convenient it is. First, let's start with a simplest user class:
Create user class
1. Create a class named "user" and a namespace
Namespace blogsample
{
Using system;
Using Castle. activerecord;
Public class user
{
}
}
2. Now we can use activerecordattribute in actiivrecordbase to extend this class:
Namespace blogsample
{
Using system;
Using Castle. activerecord;
[Activerecord]
Public class user: activerecordbase <user>
{
}
}
3. Add the corresponding class attributes based on the columns in the table.
Namespace blogsample
{
Using system;
Using Castle. activerecord;
[Activerecord]
Public class user: activerecordbase <user>
{
Private int ID;
Private string username;
Private string password;
Private int ID
{
Get {return ID ;}
Set {id = value ;}
}
Public String Username
{
Get {return username ;}
Set {username = value ;}
}
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}
}
}
4. Add an identifier to the class to indicate the attributes corresponding to activerecord (that is, simple attributes are mapped to columns, primary keys, or links ). In this example, ID is the primary key, and other IDs are common attributes:
Namespace blogsample
{
Using system;
Using Castle. activerecord;
[Activerecord]
Public class user: activerecordbase <user>
{
Private int ID;
Private string username;
Private string password;
Public user ()
{
}
Public user (string username, string password)
{
This. Username = username;
This. Password = password;
}
[Primarykey]
Private int ID
{
Get {return ID ;}
Set {id = value ;}
}
[Property]
Public String Username
{
Get {return username ;}
Set {username = value ;}
}
[Property]
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}
}
}
This is simple. Now let's create the blog and post classes separately. The process is the same as above. We suggest you try to write it yourself.
Create a blog
This blog is very simple, but it should be noted that it is incomplete. We still need to create its association with the Post table, but it only takes one minute:
Namespace blogsample
{
Using system;
Using system. collections;
Using Castle. activerecord;
[Activerecord]
Public class blog: activerecordbase <blog>
{
Private int ID;
Private string name;
Private string author;
Public blog ()
{
}
[Primarykey]
Private int ID
{
Get {return ID ;}
Set {id = value ;}
}
[Property]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
[Property]
Public String author
{
Get {return author ;}
Set {author = value ;}
}
}
}
Post class
This post class is also very simple, but it is not complete. We need to associate it with the blog class.
The contents attribute uses [property (columntype = "stringclob")].
This is because I want to bind this column to a column of the text type.
Namespace blogsample
{
Using system;
Using Castle. activerecord;
[Activerecord]
Public class post: activerecordbase <post>
{
Private int ID;
Private String title;
Private string contents;
Private string category;
Private datetime created;
Private bool published;
Public Post ()
{
Created = datetime. now;
}
[Primarykey]
Private int ID
{
Get {return ID ;}
Set {id = value ;}
}
[Property]
Public String title
{
Get {return title ;}
Set {Title = value ;}
}
[Property (columntype = "stringclob")]
Public String Contents
{
Get {return contents ;}
Set {contents = value ;}
}
[Property]
Public String category
{
Get {return category ;}
Set {Category = value ;}
}
[Property]
Public datetime created
{
Get {return created ;}
Set {created = value ;}
}
}
}
Maybe you will say this is easy, even if this is the first time you use activerecord. Adding a link is not too difficult.