Castle.activerecord模式.Web.dll是做什么的,要详细点介绍。

用户名: @
密&&码:&&&|
密&&码:&|
密&&码:&|
&&&&&&&&&&&&&&&&&&&&&&&&&&
您的位置: &
Castle Active Record for .NET2.0快速入门示例
内容导航:
【IT168技术文档】& 一.创建Web工程 & 创建一个Web站点或者Web应用程序,添加对Castle.ActiveRecord.dll的引用。 & 二.创建需要持久化的业务实体 & 在.NET2.0下,由于引入了泛型,创建业务实体比1.1下简单了许多,业务实体只需要继承于泛型的ActiveRecordBase类,其中默认已经实现了一些静态的方法,不需要我们再在业务实体中实现。
[ActiveRecord("Employees")]
public class Employee : ActiveRecordBase&Employee&
private string employeeID;
private string lastN
private string
private string
private string homeP
private string
[PrimaryKey(PrimaryKeyType.Assigned)]
public string EmployeeID
get { return employeeID; }
set { employeeID = }
[Property]
public string LastName
get { return lastN }
set { lastName = }
[Property]
public string City
get { return }
set { city = }
[Property]
public string Address
get { return }
set { address = }
[Property]
public string HomePhone
get { return homeP }
set { homePhone = }
[Property]
public string Country
get { return }
set { country = }
相关关键词
【内容导航】
&版权所有。未经许可,不得转载。
[责任编辑:]
热点文章排行
All Rights Reserved. 北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号四、框架的初始化
Castle ActiveRecord 在应用它的类之前必须初始化。只有在程序启用的时候初始一次。在这里我们假设我们开发的是WinForm应用程序,在窗体显示之前我们必须为它初始化。
ActiveRecord也需要配置一次信息来告诉它是用了哪一个数据库,怎么样和它会话。其实这就是NHibernate所用到的配置,所以你现在要决定在哪里保存这些配置。
ActiveRecord配置
我们可以选择不同的方式来配置ActiveRecord里的配置也可以选择保存到哪里。在文档里有更详细的说明。为了简单起见我们将使用一个简单的独立的XML文件:
&?xml version=&1.0& encoding=&utf-8& ?&
&activerecord&
& &config&
&&&&&&& key=&connection.driver_class&&&&&&&&
&&&&&&& value=&NHibernate.Driver.SqlClientDriver& /&
&&&&&&& key=&dialect&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&& value=&NHibernate.Dialect.MsSql2005Dialect& /&
&&&&&&& key=&connection.provider&&&&&&&&&&&&
&&&&&&& value=&NHibernate.Connection.DriverConnectionProvider& /&
&&&&&&& key=&connection.connection_string&&&&
&&&&&&& value=&Data Source=.;Initial Catalog=Integrated Security=SSPI& /&
& &/config&
&/activerecord&
注意:这里的配置是用的MS SQL Server 2005.如果你是用的其他的数据库你就需要改一下这里面的驱动了。详细怎么请参考 Xml Configuration Reference 文档。
现在我们就保存这个文档并命名为:appcofig.xml。下面一段代码我们来访问这个文件。
namespace BlogSample
&&& using System.Windows.F
&&& using BlogSample.UI;
&&& using Castle.ActiveR
&&& using Castle.ActiveRecord.Framework.C
&&& public class App
&&&&&&& public static void Main()
&&&&&&&&&&& XmlConfigurationSource source = new XmlConfigurationSource(&appconfig.xml&);
初始化ActiveRecord
最后我们来通过这个配置文件来初始ActivRecord。我们也需要来指定ActiveRecord的检查类型。为此我们可以指定一个程序集或一个程序集数组。为了简单我们就只用一个数组类型:
(我在这里的理解是ActivRecord为我们提供了自己检查功能,可能包括语法和逻辑错误吧,如果只有几个类就单独的列出来,要有多个的话可以生成一个dll文件来做检查。这只是一个排错过程。。。)
namespace BlogSample
&&& using System.Windows.F
&&& using BlogSample.UI;
&&& using Castle.ActiveR
&&& using Castle.ActiveRecord.Framework.C
&&& public class App
&&&&&&& public static void Main()
&&&&&&&&&&& XmlConfigurationSource source = new XmlConfigurationSource(&appconfig.xml&);
&&&&&&&&&&&
&&&&&&&&&&& ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post), typeof(User) );
做完这一步我们为应用ActiveRecord铺平了道路。
你可以使用ActiveRecord来创建表:
public class App
&&& public static void Main()
&&&&&&& XmlConfigurationSource source = new XmlConfigurationSource(&appconfig.xml&);
&&&&&&& ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post), typeof(User) );
&&&&&&& if (MessageBox.Show(&Do you want to let ActiveRecord create the database tables?&,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &Schema&, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
&&&&&&&&&&& ActiveRecordStarter.CreateSchema();
(就是在写好类的情况下自动创建表)
现在我的事情并未做完,我们要用上面的类开始自己真正的代码。我们可以展示出更多的方法来实现数据库的相关操作。
继续使用这些类。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:53894次
排名:千里之外
原创:11篇
转载:12篇
译文:10篇
(1)(1)(1)(1)(2)(3)(2)(3)(1)(1)(2)(1)(1)(1)(6)(1)(1)(1)(3)Castle ActiveRecord环境搭建和配置
- heros - ITeye技术网站
博客分类:
ActiveReocrd是Castle中一ORM框架,它对NHibernate做了进一步的封装。 1.环境搭建
a.程序集引用 (注意各程序集间版本兼容性)
Castle.ActiveRecord.dll
Castle.DynamicProxy2.dll
Iesi.Connections.dll
NHibernate.dll
Nhibernate.ByteCode.Castle.dll
这一项根据自己需要来选择不同的延迟加载代理工厂
NHibernate.ByteCode.Castle.dll
NHibernate.ByteCode.LinFu.dll
NHibernate.ByteCode.Spring.dll
可以在应用程序配置文件和单独的配置文件中来进行配置,也可以通过编程方式配置
配置项参考nhibernate hibernate.cfg.xml
在应用程序配置文件中配置
&?xml version="1.0" encoding="utf-8" ?&
&configuration&
&configSections&
&section name="activerecord"
type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler,Castle.ActiveRecord"/&
&/configSections&
&activerecord&
&add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /&
&add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/&
&add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/&
&add key="connection.connection_string" value="server=.;database=uid=pwd=sa"/&
&add key="show_sql" value="true"/&
&add key="proxyfactory.factory_class"
value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" /&
&/activerecord&
&/configuration&
IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationS
ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);
在xml文件中配置
&?xml version="1.0" encoding="utf-8" ?&
&activerecord&
&add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /&
&add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/&
&add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/&
&add key="connection.connection_string" value="server=.;database=uid=pwd=sa"/&
&add key="show_sql" value="true"/&
&add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" /&
&/activerecord&
IConfigurationSource source = new XmlConfigurationSource("XMLFile2.xml");
ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);
编程方式配置
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
Hashtable properties = new Hashtable();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2000Dialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("connection.connection_string", "UID=Password=;Initial Catalog=ARDData Source=.");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
source.Add(typeof(ActiveRecordBase), properties);
ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), source);
浏览: 27102 次
来自: 上海评论-3591&
trackbacks-74
  前后差不多两年没使用过Castle.ActiveRecord做ORM了,也近两年没有关注Castle的版本变化了,最近关注了发现很多地方都改变了。今天在一个现有的小项目中尝试添加Castle.ActiveRecord来做数据持久化操作,以前不需要做ProxyFactory配置,现在必须要ProxyFactory配置。使用到了NHibernate.ByteCode.Castle.dll组件。
  ActoiveRecord的配置文件有了明显的变化,需要配置ProxyFactory才能完成持久化操纵,详细配置如下:
&&&activerecord&&&&&&config&&&&&&&&add&key="connection.driver_class"&value="NHibernate.Driver.SqlClientDriver"/&&&&&&&&add&key="dialect"&value="NHibernate.Dialect.MsSql2000Dialect"&/&&&&&&&&add&key="connection.provider"&value="NHibernate.Connection.DriverConnectionProvider"&/&&&&&&&&add&key="connection.connection_string"&value="Data&Source=SQL05;Initial&Catalog=uid=pwd=12345;"&/&&&&&&&&add&key="proxyfactory.factory_class"&value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,&NHibernate.ByteCode.Castle"/&&&&&&/config&&&&/activerecord&
  其他的使用基本上没有变化,下面是一个ORM的对象配置。
[ActiveRecord("UserInfo")]public&class&UserInfo&:&ActiveRecordBase&UserInfo&{&&&&[PrimaryKey(PrimaryKeyType.Identity,&"ID")]&&&&public&int&ID&{&get;&set;&}&&&&[Property("Sex",&Length&=&<span style="color: #)]&&&&public&string&Sex&{&get;&set;&}&&&&[Property("Name",&NotNull&=&true)]&&&&public&string&Name&{&get;&set;&}}
  初始化配置也还是和以前的版本使用方式一样的,支持单个对象、多个对象、单个程序集、多个程序集等等不同方式的关系映射对象进行初始化。
protected&void&Application_Start(object&sender,&EventArgs&e){&&&&IConfigurationSource&source&=&ConfigurationManager.GetSection("activerecord")&as&IConfigurationS&&&&//ActiveRecordStarter.Initialize(source,&typeof(Entity.UserInfo));&&&&ActiveRecordStarter.Initialize(Assembly.Load("Entity"),&source);}
  由于关系映射对象继承于ActiveRecordBase&T&泛型版本,因此可以直接基于关系映射对象使用数据持久化操纵行为,和老版本的使用方式是一样的。
protected&void&Page_Load(object&sender,&EventArgs&e){&&&&if&(!IsPostBack)&&&&{&&&&&&&&UserInfo&info&=&new&UserInfo();&&&&&&&&info.Name&=&"zhangsan";&&&&&&&&info.Sex&=&"男";&&&&&&&&//插入info数据到数据库中所映射的关系表&&&&&&&&info.Create();&&&&}}
&  至于ProxyFactory的具体作用暂时没有深入研究,也不知道AR为什么会在新版本中添加这个功能,我想应该是想通过NHibernate对Castle的AR进行某些控制,暂时没有时间研究,期待有研究过的朋友分享,谢谢。
阅读(...) 评论()Castle ActiveRecord (Active Record Pattern Built on NHibernate) Rapid Application Development. - 好代码编程网
Castle ActiveRecord (Active Record Pattern Built on NHibernate) Rapid Application Development.
Castle Project's ActiveRecord
Castle Project's ActiveRecord is a unique rapid lication development Active Record implementation built on top of NHibernate. From the Castle Project:
“ActiveRecord is an implementation of the ActiveRecord pattern
. built on top of NHibernate, without all the
map of usg Nhibernate directly. An ActiveRecord instance represents a row in the database, and the static methods act on all rows.”
ActiveRecord removes much of the burden of NHibernate for applications that need a simple implementation of NHibernate for their applications.
ActiveRecord and ging
If we go back to my example of using Castle Project's ActiveRecord for use in making a blogging engine for a , we only need to do a few things to get up and running quickly:
Download the latest version of Castle Project's ActiveRecord
Create a new website in Visual Studio
Reference Castle.ActiveRecord.dll
Add Web.Config rmation
Add Global.asax Information
Create a Business Object
Begin Using Your Business Object
Download Castle Project ActiveRecord
You can download the latest version of ActiveRecord &and create a new website in Visual Studio and reference the assembly Castle.ActiveRecord.dll from your download files.
ActiveRecord Web.Config Information
You will need to add some information to your Web.Config for ActiveRecord.&configSections&
&section name="activeRecord" type="Castle.ActiveRecord.
Framework.Config.ActiveRecordSectionHandler,
Castle.ActiveRecord"/&
</&/configSections&
&activeRecord isWeb="true"&
&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="Data Source=.;Initial Catalog=BIntegrated
Security=True"/&
&/</config&
</&/activeRecord&
In this case I am using ActiveRecord with a
Server 2000 Server and a database, called “Blog“, that contains the tables.
ActiveRecord Global.asax Settings
Add a couple of lines to your Global.asax file that specifies your ActiveRecord configuration information is in web.config and where the business objects are located that map to the database tables:void Application_Start(object sender, EventArgs e)
Castle.ActiveRecord.Framework.IConfigurationSource source
= System.Configuration.ConfigurationManager.
GetSection("activeRecord") as Castle.ActiveRecord.
Framework.IConfigurationS
Castle.ActiveRecord.ActiveRecordStarter.Initialize(
typeof(Post).Assembly, source);
Above I am specifying that CastleRecord should look into the same assembly as the&Post Class for all ActiveRecord Business Objects. In my case for the demo,&Post is sitting in the&AppCode Folder.
Create an ActiveRecord Business Object
We need to decorate the ActiveRecord Business Object with attributes that map to a database table and columns. Here is a partial description of the Post Class that maps to a Posts Table in the database.///
/// Summary description for Post
[ActiveRecord("Posts")]
public class Article : ActiveRecordBase&Post&
private int _
[PrimaryKey(PrimaryKeyType.Native, "PostId")]
public int Id
private int _blogId;
[Property]
public int BlogId
return _blogId;
private int _categoryId;
[Property]
public int CategoryId
return _categoryId;
_categoryId =
private string _title = string.E
[Property]
public string Title
private string _description = string.E
[Property]
public string Description
_description =
Persist and Load ActiveRecord Objects
I won't go into every conceivable method on the ActiveRecordBase Class, but suffice to say that using Post is a no-brainer when it es to using CRUD methods on the business objects. We can create a save a blog post as simple as:Post post = new Post();
post.BlogId = 1;
post.CategoryId = 2;
post.Title = "Test";
post.Description = "This is a test.";
// Didn't need to write this.
// Provided by ActiveRecordBase
post.Save();
We can load a post by PostId (primary key) like this:// Finds PostId = 2
Post post = Post.Find(2);
Get&top 25&posts for BlogId = 1 sorted by PublishedDate:Article[] articles = Article.FindAll(new ICriterion[]
{ Expression.Eq("BlogId", 0) }, new Order[]
{ Order.Desc("PublishedDate") }, 0, 25);
Conclusion
Not only is the&Active Record Design Pattern extremely useful and intuitive for applications that are mainly forms-over-data applications, but Castle Project's ActiveRecord implementation packages&all the&benefits of Nhibernate in a nice Rapid Application Development Package that makes Active Record easy to implement.
.Net 文章一周点击
.Net 文章一月点击
HaoGongJu.Net ( 好代码 ) All Rights Reserved}

我要回帖

更多关于 activerecord模式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信