在使用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(); } }}
希望有用。
时间: 2024-11-11 22:21:18