Castle ActiveRecord (Active Record Pattern Built on NHibernate) Rapid Application Development.

來源:互聯網
上載者:User
Castle Project's ActiveRecord

Castle Project's ActiveRecord is a unique rapid application development Active Record implementation built on top of NHibernate. From the Castle Project:

“ActiveRecord is an implementation of the ActiveRecord pattern for .Net built on top of NHibernate, without all the XML mapping of using Nhibernate directly. An ActiveRecord instance represents a row in the database, and the static methods act on all rows.”

ActiveRecord removes much of the burden of NHibernate for applications that need a simple implementation of NHibernate for their applications.

ActiveRecord and Blogging Engine

If we go back to my example of using Castle Project's ActiveRecord for use in making a blogging engine for a website, we only need to do a few things to get up and running quickly:

  1. Download the latest version of Castle Project's ActiveRecord
  2. Create a new website in Visual Studio
  3. Reference Castle.ActiveRecord.dll
  4. Add Web.Config Information
  5. Add Global.asax Information
  6. Create a Business Object
  7. Begin Using Your Business Object
Download Castle Project ActiveRecord

You can download the latest version of ActiveRecord here and create a new website in Visual Studio and reference the assembly Castle.ActiveRecord.dll from your download files.

ActiveRecord Web.Config Information

You will need to add some information to your Web.Config for ActiveRecord.

<configSections>    <section name="activeRecord" type="Castle.ActiveRecord.
Framework.Config.ActiveRecordSectionHandler,
Castle.ActiveRecord"/></configSections><activeRecord isWeb="true"> <config> <add key="hibernate.connection.driver class"
value="NHibernate.Driver.SqlClientDriver"/> <add key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"/> <add key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/> <add key="hibernate.connection.connection_string"
value="Data Source=.;Initial Catalog=Blog;Integrated
Security=True"/> </config></activeRecord>

In this case I am using ActiveRecord with a SQL Server 2000 Server and a database, called “Blog“, that contains the tables.

ActiveRecord Global.asax Settings

Add a couple of lines to your Global.asax file that specifies your ActiveRecord configuration information is in web.config and where the business objects are located that map to the database tables:

void Application_Start(object sender, EventArgs e) {    Castle.ActiveRecord.Framework.IConfigurationSource source
= System.Configuration.ConfigurationManager.
GetSection("activeRecord") as Castle.ActiveRecord.
Framework.IConfigurationSource; Castle.ActiveRecord.ActiveRecordStarter.Initialize(
typeof(Post).Assembly, source);}

Above I am specifying that CastleRecord should look into the same assembly as the Post Class for all ActiveRecord Business Objects. In my case for the demo, Post is sitting in the AppCode Folder.

Create an ActiveRecord Business Object

We need to decorate the ActiveRecord Business Object with attributes that map to a database table and columns. Here is a partial description of the Post Class that maps to a Posts Table in the database.

/// /// Summary description for Post/// [ActiveRecord("Posts")]public class Article : ActiveRecordBase<Post>{    private int _id;    [PrimaryKey(PrimaryKeyType.Native, "PostId")]    public int Id    {        get        {            return _id;        }        set        {            _id = value;        }    }    private int _blogId;    [Property]    public int BlogId    {        get        {            return _blogId;        }        set        {            _blogId = value;        }    }    private int _categoryId;    [Property]    public int CategoryId    {        get        {            return _categoryId;        }        set        {            _categoryId = value;        }    }    private string _title = string.Empty;    [Property]    public string Title    {        get        {            return _title;        }        set        {            _title = value;        }    }    private string _description = string.Empty;    [Property]    public string Description    {        get        {            return _description;        }        set        {            _description = value;        }    }}
Persist and Load ActiveRecord Objects

I won't go into every conceivable method on the ActiveRecordBase Class, but suffice to say that using Post is a no-brainer when it comes to using CRUD methods on the business objects. We can create a save a blog post as simple as:

Post post = new Post();post.BlogId = 1;post.CategoryId = 2;post.Title = "Test";post.Description = "This is a test.";// Didn't need to write this.// Provided by ActiveRecordBasepost.Save();

We can load a post by PostId (primary key) like this:

// Finds PostId = 2Post post = Post.Find(2);

Get top 25 posts for BlogId = 1 sorted by PublishedDate:

Article[] articles = Article.FindAll(new ICriterion[]
{ Expression.Eq("BlogId", 0) }, new Order[]
{ Order.Desc("PublishedDate") }, 0, 25);
Conclusion

Not only is the Active Record Design Pattern extremely useful and intuitive for applications that are mainly forms-over-data applications, but Castle Project's ActiveRecord implementation packages all the benefits of Nhibernate in a nice Rapid Application Development Package that makes Active Record easy to implement.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.