Table of contents
- 1 Creating the User class
- 2 Creating the Blog class
- 3 Creating the Post class
Creating the classes
Now it is time to create the classes that will map to the tables, and here you will see how easy it is. First let's start with the simplest one, the User class:
Creating the User class
Create a empty class named User and add the using statements:
namespace BlogSample { using System; using Castle.ActiveRecord; public class User { } }
Now make the class extend from ActiveRecordBase and use the ActiveRecordAttribute:
namespace BlogSample { using System; using Castle.ActiveRecord; [ActiveRecord] public class User : ActiveRecordBase<User> { } }
Add the fields and the corresponding properties for the columns on the database:
namespace BlogSample { using System; using Castle.ActiveRecord; [ActiveRecord] public class User : ActiveRecordBase<User> { private int id; private string username; private string password; public 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; } } } }
Finally add the attributes that inform ActiveRecord what the properties are (ie simple properties that map to columns, primary keys or relationships). In this case we just need to mark the Id as the primary key and the rest as simple properties:
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] public 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; } } } }
That is it. Now we need to create the classes for the Blog and Post tables. The process is the same and we encourage you to try yourself.
Creating the Blog class
The Blog class is very straightforward. Bear in mind that it is not complete. We still need to create a link between it and the Post. But we will get to that in a 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] public 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; } } } }
Creating the Post class
The Post class is also simple, but it is not completed as well, we need to link it with the Blog class.
You should also note something different. The Contents property uses [Property(ColumnType="StringClob")]. This is required as this property is bound to a text column.
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] public 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; } } } }
You probably agree that was fairly easy, even if this is the first time you have used ActiveRecord. Adding relationships will not be difficult too.
Proceed with adding the relationships.