火星文 技术研习社

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

Castle ActiveRecord 小试 -单表 CRUD

Posted on 2006-03-29 00:19  剑廿三  阅读(527)  评论(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

Blog.cs 内容:

using System;

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;


        [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; }
        }


        
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 );
        }



    }

}

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) );


            Blog blog 
= new Blog();

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

    }

}

内嵌资源 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>