Castle is a very good open source project for the. NET platform, with a focus on open source. It is further encapsulated on a nhibernate basis, with the same principle as the nhibernate, but it solves the nhibernate defect better, from the ORM (Object Relational mapping) to the IOC (inversion of control, inversion) container , and then to the MVC framework of the Web layer, basically includes all the content of the entire development process.
Configuring Castle in the VS2013 mvc4+sql Server environment can be easily divided into the following 4 steps
(a) There are two ways of quoting Castle
Directly download Castle related components directly using NuGet Castle.activerecord provides orm,castle.core,castle.windsor
If you have a backup DLL file, you can refer to it directly, but be sure to refer to NHibernate.dll
(ii) Configuration Webconfig
<section name= "ActiveRecord" type= "Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.activerecord "/>
This section node must be located in the first child node of Webconfig configsection or it will be an error.
<!--Register Castle configuration block, type specify ActiveRecord assembly-<configSections> <section Name="ActiveRecord"Type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.activerecord"/> <!--For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?linkid=237468 --<section name="EntityFramework"Type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=5.0.0.0, culture= Neutral, publickeytoken=b77a5c561934e089"requirepermission="false"/> </configSections> <activerecord isweb="true"> <config> <!--configuration database type is SQL Server--> <add key="Connection.driver_class"Value="NHibernate.Driver.SqlClientDriver"/> <!--configuration database language for SQL Server --<add key="dialect"Value="NHibernate.Dialect.MsSql2008Dialect"/> <!--configuration database connection driver-<add key="Connection.provider"Value="NHibernate.Connection.DriverConnectionProvider"/> <!--Database connection String-<add key="Proxyfactory.factory_class"Value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/> <add key="connection.connection_string"Value="server=.; database=lms;integrated security=true;"Providername="System.Data.SqlClient"/> </config> </activerecord>
(iii) Initialize castle, generate database
There are many ways to do this, or you can create a new event when the page is initialized.
For example, in the click event of a separate ASPX page:
protected voidBtncreate_click (Objectsender, EventArgs e) { Try { if(!activerecordstarter.isinitialized) {Iconfigurationsource source= System.Configuration.ConfigurationSettings.getconfig ("ActiveRecord") asIconfigurationsource;
ActiveRecord string to <section name= "ActiveRecord" > corresponding type[] param={typeof(domain.years),typeof(Domain.dog),typeof(DOMAIN.CAT)};//Map class is added activerecordstarter.initialize (source, param) by array parameters once;//mapping Activerecordstarter.createschema ();//Generate Database Activerecordstarter.generatecreationscripts ("Create.sql"); This. Clientscript.registerstartupscript (Page.gettype (),"","<script>alert (' creation success ') </script>"); } Else { This. Clientscript.registerstartupscript (Page.gettype (),"","<script>alert (' Create failed ') </script>"); } } Catch(Exception ex) {Throwex; } } }
This completes the configuration of the castle.
PS Object Relational mapping note that classes are inherited from ActiveRecord, and the database table name is specified otherwise mapping fails
[ActiveRecord ()]//specifies the database table name, the default is the same as the class name public class years:activerecordbase { [property] publicstringgetset;} }
Configuring Castle in ASP. MVC4