火星文 技术研习社

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

Castle ActiveRecord 使用内嵌的配置文件

Posted on 2006-03-29 08:35  剑廿三  阅读(415)  评论(0编辑  收藏  举报
在示例 《Castle ActiveRecord 小试 -单表 CRUD》  中,测试行为 CreateBlogTest() 在实施 ActiveRecord 配置时使用了内嵌的配置文件(嵌入资源)。

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

    }

}


使用嵌入资源,需要引用用于反射的 System.Reflection 命名空间,该命名空间包含了一个名为 Assembly 的类。可以使用 Assembly 的方法 GetManifestResourceStream 获得内嵌资源,但是这个方法并不是静态的,所以要先取得 Assembly 的具体实例(程序集)。

可以通过 typeof(Namespace.ClassName).Assembly 方法获得类 Namespace.ClassName 所属的程序集。因此,如果要把 ActiveRecord 的 XML 配置文件作为内嵌资源使用的话,该 XML 配置文件所在的 Namespace 必须至少包括一个类(class)。