Castle ActiveRecord (Active Record Pattern Built on NHibernate) Rapid Application Development.
21795 단어 ActiveRecord
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:
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 ActiveRecordBase
post.Save();
We can load a post by PostId (primary key) like this:// Finds PostId = 2
Post 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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Rails】일대일대다의 어소시에이션결국은 기본적인 것입니다만, 조금 바뀌면 곧바로 빠져 버린다. 어소시에이션이 잘 되지 않고 시간을 녹여 버렸기 때문에, 그런 과거의 자신과 있을지도 모르는 미래의 누군가를 위해서 남겨 둡니다. User 모델과 Tag...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.