ActiveRecord - Playing More with Hayden.ActiveRecord Framework for .NET

ActiveRecord - Playing More with Hayden.ActiveRecord Framework

by David Hayden ( Florida .NET Developer ) Filed: O/R Mappers

 

This past week I had some time to work on my ActiveRecord “Framework” that I mentioned before:

I am amazed at how useful and productive this and similar ActiveRecord Frameworks are in building applications. I wouldn't use my framework to build enterprise applications, but it certainly has its place in small and medium-sized applications. I timed myself this evening and it took me about 10 minutes to start reading and writing data to an existing database.

The process is simple:

  1. Create your Visual Studio Solution
  2. Reference Hayden.ActiveRecord in your project.
  3. Initialize Hayden.ActiveRecord with classes you want to persist, etc. In this case, Post:
    • DataContext.Instance.Initialize(new Type[] { typeof(Post) })
  4. Create the Post Class with a few attributes:

 

[Table("Posts")]
public class Post:ActiveRecordBase<Post,int>
{
    private int _postId;
    private int _blogId;
    private string _name;
    private string _description;

    [Column(Id = true, AutoGen = true)]
    public int PostId
    {
        get { return _postId; }
        set { _postId = value; }
    }
    
    [Column]
    public int BlogId
    {
        get { return _blogId; }
        set { _blogId = value; }
    }
    
    [Column]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    
    [Column]
    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }
}

 

ConnectionString and Provider information is automatically read from the web.config if not specified during initialization. You can immediately start using the class with no data access layer coding at all:

 

Get One Post:

Post post = Post.FetchById(1);

 

Fetch All Posts Ordered By Name:

Post[] posts = Post.FetchAll(Order.Asc("Name"));

 

Use Expressions to Filter:

Expression expression = Expression.Eq("PostId", 1).
AddWithAnd(
"Name", ComparisonOperator.Equals,
"First Blog Post"); Post[] filteredPosts = Post.FetchAll(expression);

 

Delete All By Property:

Post.DeleteAllByProperty("BlogId", 1);

 

Etc. My goal is to have a 1.0 version by the end of January. The past two weeks I have refactored it quite a bit, improved the quality of the queries by fully qualifying table and column names, created more advanced expressions for more complicated filtering, and created a build process that does everything but wash the dishes :) The build process especially creates a test database and runs several NUnit Tests and NCover/NCoverExplorer for code coverage. Documentation is done with Sandcastle and will look cool as soon as I add some comments :)

I have also added associations with the option of lazyloading, but it is not complete yet. Thus, instead of using BlogId mentioned above, you also have the choice of doing something similar to:

 

[Parent(Storage = “_blogId“)]
public Blog Blog
{
    get { return _blog; }
    set { _blog = value; }
}

 

If you like this ActiveRecord functionality, I highly recommend checking out Castle ActiveRecord which is currently in RC2. I should join their team, assuming they would have me :), as I am impressed with all their tools :)

Source: David Hayden ( Florida .NET Developer )

Filed: O/R Mappers

 

posted on Wednesday, November 22, 2006 7:36 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices