火星文 技术研习社

Noname Cat, Keep Thinking
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Castle ActiveRecord 小试 - 双表一对多映射

Posted on 2006-03-29 00:34  剑廿三  阅读(737)  评论(0编辑  收藏  举报
参考:http://www.castleproject.org/index.php/ActiveRecord:Getting_Started

映射关系文档:http://www.castleproject.org/index.php/ActiveRecord:Mappings

依赖包:

You must reference the following set of assemblies to use ActiveRecord:

  • Castle.ActiveRecord.dll
  • Castle.Model.dll
  • Nullables.dll

But ActiveRecord also depends on NHibernate, so you must reference the following as well:

  • NHibernate.dll
  • Castle.DynamicProxy.dll (Curious? Check DynamicProxy)
  • Nullables.NHibernate.dll
  • log4net.dll
  • Iesi.Collections.dll

总之只要安装了 Castle.msi ,把以上包全部加进去就好了。

另外,本试验用到了 VS.NET 的测试驱动外挂 TestDriver.NET 2.0,需要安装的

下载:TestDriven.NET-2.0.1545d.zip

注意 ActiveRecord 的反射机制:

只要实体类的属性被标注为 [Property],ActiveRecord 就会自动映射到数据表中的“实体类名_属性名”字段。但是,要是在标注中也声明字段名,则需要写出完整的字段名。

Blog.cs 内容:

using System;
using System.Collections;

using Castle.ActiveRecord;

namespace test
{
    
/// <summary>
    
/// Blog 的摘要说明。
    
/// </summary>

    [ActiveRecord("Blogs")]
    
public class Blog : ActiveRecordBase
    
{
        
public Blog()
        
{
        }


        
private int _id;
        
private String _name;
        
private String _author;

        
private IList _posts;



        [PrimaryKey(PrimaryKeyType.Native, 
"blog_id")]
        
public int Id
        
{
            
get return _id; }
            
set { _id = value; }
        }


        [Property(
"blog_name")]
        
public String Name
        
{
            
get return _name; }
            
set { _name = value; }
        }


        [Property(
"blog_author")]
        
public String Author
        
{
            
get return _author; }
            
set { _author = value; }
        }


        [HasMany(
typeof(Post), Table="posts", ColumnKey="post_blogid")]
        
public IList Posts
        
{
            
get return _posts; }
            
set { _posts = value; }
        }



        
public static void DeleteAll()
        
{
            DeleteAll( 
typeof(Blog) );
        }


        
public static Blog[] FindAll()
        
{
            
return (Blog[]) FindAll( typeof(Blog) );
        }


        
public static Blog Find(int id)
        
{
            
return (Blog) FindByPrimaryKey( typeof(Blog), id );
        }



    }

}

Post.cs 内容:

using System;

using Castle.ActiveRecord;

namespace test
{
    
/// <summary>
    
/// Post 的摘要说明。
    
/// </summary>

    [ActiveRecord("Posts")]
    
public class Post : ActiveRecordBase
    
{
        
private int _id;
        
private String _title;
        
private String _contents;
        
private String _category;
        
private DateTime _created;
        
private bool _published;
        
private Blog _blog;

        
public Post()
        
{
            _created 
= DateTime.Now;
        }


        
public Post(Blog blog, String title, String contents, String category) : this()
        
{
            _blog 
= blog;
            _title 
= title;
            _contents 
= contents;
            _category 
= category;
        }


        [PrimaryKey(PrimaryKeyType.Native)]
        
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; }
        }


        [BelongsTo(
"blogid")]
        
public Blog Blog
        
{
            
get return _blog; }
            
set { _blog = value; }
        }


        [Property]
        
public DateTime Created
        
{
            
get return _created; }
            
set { _created = value; }
        }


        [Property]
        
public bool Published
        
{
            
get return _published; }
            
set { _published = value; }
        }


        
public static void DeleteAll()
        
{
            ActiveRecordBase.DeleteAll( 
typeof(Post) );
        }


        
public static Post[] FindAll()
        
{
            
return (Post[]) ActiveRecordBase.FindAll( typeof(Post) );
        }

    }


}

BlogManager.cs 内容:

using System;
using System.Collections;


namespace test
{
    
/// <summary>
    
/// BlogManager 的摘要说明。
    
/// </summary>

    public class BlogManager
    
{

        
private static BlogManager instance = null;

        
public BlogManager()
        
{

        }


        
public static BlogManager getInstance()
        
{
            
if(instance==null)
                instance
=new BlogManager();
            
return instance;
        }


        
public bool CreateBlog(Blog blog)
        
{
            blog.Create();
            
return true;
        }



    }

}

_TestUnitCase\BlogTest.cs 内容:

using System;
using NUnit.Framework;

using test;

using System.Reflection;

using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord;

namespace test._TestUnitCase
{
    
/// <summary>
    
/// BlogTest 的摘要说明。
    
/// </summary>

    [TestFixture]
    
public class BlogTest
    
{
        
public BlogTest()
        
{
        }

        
        [Test]
        
public void CreateBlogTest()
        
{
            Assembly assembly 
= typeof(test.Blog).Assembly;
            XmlConfigurationSource src 
= new XmlConfigurationSource(assembly.GetManifestResourceStream("test.ActiveRecord.config"));
            ActiveRecordStarter.Initialize( src, 
typeof(Blog),typeof(Post) );

            
// 注意:由于存在关联,这里要初始化两个实体。
            
//       否则会抛出 unmapped: Post 的 Exception。


            Blog blog 
= new Blog();

            blog.Name
="abcde1234";
            blog.Author
="stephenabcdef";
            
            Console.WriteLine(BlogManager.getInstance().CreateBlog(blog));
        }

    }

}

SQL 脚本:

CREATE TABLE Blogs (
    blog_id     
int IDENTITY(11PRIMARY KEY,
    blog_name   
varchar(50),
    blog_author 
varchar(50)
)

CREATE TABLE Posts (
    post_id        
int IDENTITY(11PRIMARY KEY,
    post_title     
varchar(50),
    post_contents  
text,
    post_category  
varchar(50),
    post_blogid    
int FOREIGN KEY REFERENCES Blogs (blog_id),
    post_created   
datetime,
    post_published 
bit
)

内嵌资源 test.ActiveRecord.config 内容:

<?xml version="1.0" encoding="utf-8" ?> 
<activerecord>
      
    
<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="UID=sa;Password=*****;Initial Catalog=test;Data Source=." />
    
</config>
      
</activerecord>