Castle ActiveRecord学习笔记三:初始化配置

Posted 杨铭宇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Castle ActiveRecord学习笔记三:初始化配置相关的知识,希望对你有一定的参考价值。

在使用Castle ActiveRecord的时候,需要两种方式来进行初始化,一种就是通过配置文件,另外一种就是将初始配置进行硬编码,写到程序中来。

当然,一般来说,利用配置文件来进行将会大大简化后期维护,我们首先在程序中添加一个App.config的配置文件,记住,一定要将这个配置文件的生成操作设置为“嵌入的资源”才行。否则会提示如学习笔记二中所出现的问题。

具体的配置如下:

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="connection.connection_string" value="UID=sa;Password=*****;Initial Catalog=NewsDemo;Data Source=."/>
</config>
</configuration>
复制代码

然后在程序中的编码如下:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Castle.ActiveRecord.Framework;
using System.Configuration;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using System.Collections;
using CastleLib;

namespace CastleAPP
{
publicpartialclass MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
Init();
}

privatevoid Init()
{
XmlConfigurationSource source =new XmlConfigurationSource("http://www.cnblogs.com/App.config");
ActiveRecordStarter.Initialize(source, typeof(CastleLib.User);
}

privatevoid Form1_Load(object sender, EventArgs e)
{
AddUsers();
IList list = CastleLib.User.FindAll();

dataGridView1.DataSource = list;
}

publicvoid AddUsers()
{
CastleLib.User user =new CastleLib.User();
user.Name ="石朝阳";
user.Password ="ceshi";
user.Address ="河南省信阳市";
user.LastLogon = DateTime.Now;
user.Create();
}
}
}
复制代码

而如果使用硬编码的方式,则相对来说简单一些,只需要申明一个字典类,然后将操作activerecord的键值对写入,最后调用Initialize方法载入即可。具体的代码如下:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Castle.ActiveRecord.Framework;
using System.Configuration;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using System.Collections;
using CastleLib;

namespace CastleAPP
{
publicpartialclass MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
Init();
}

privatevoid Init()
{
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
IDictionary<string, string> properties = new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2008Dialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
properties.Add("connection.connection_string", "UID=sa;Password=*****;Initial Catalog=NewsDemo;Data Source=.;");
source.Add(typeof(ActiveRecordBase), properties);
ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));
}

privatevoid Form1_Load(object sender, EventArgs e)
{
AddUsers();
IList list = CastleLib.User.FindAll();

dataGridView1.DataSource = list;
}

publicvoid AddUsers()
{
CastleLib.User user =new CastleLib.User();
user.Name ="石朝阳";
user.Password ="ceshi";
user.Address ="河南省信阳市";
user.LastLogon = DateTime.Now;
user.Create();
}
}
}
复制代码

希望有用。

以上是关于Castle ActiveRecord学习笔记三:初始化配置的主要内容,如果未能解决你的问题,请参考以下文章

Castle ActiveRecord学习

Castle.ActiveRecord多数据库配置

Castle.ActiveRecord环境配置

想在 C# 中学习 ActiveRecord 模式 [关闭]

Castle ActiveRecord + NHibernate 中的交易傻瓜

在 Castle ActiveRecord 中按 ID 选择