Configure Castle and mvc4castle in ASP. NET MVC4

Source: Internet
Author: User

Configure Castle and mvc4castle in ASP. NET MVC4

 

Castle is an excellent. NET platformOpen-source projectsThe focus is on open source. It is further encapsulated on the basis of nhib.pdf. Its principle is basically the same as that of nhib.pdf, but it can better solve the defects of nhib.pdf.ORM (Object relationship ing) to IOC (inversion of control, control reversal) ContainerAnd then to the MVC Framework of the web layer, basically including all the content of the entire development process.

InVS2013 MVC4 + SQL Server 2008To configure Castle in the following four steps:

(1) There are two ways to reference Castle.

You can directly use Nuget to download Castle-related components Castle. ActiveRecord to provide ORM, Castle. Core, and Castle. Windsor.

If you have a backup dll file, you can directly reference it, but you must reference nhib.pdf. dll.

(2) Configure webconfig

  

<Section name = "activerecord" type = "Castle. ActiveRecord. Framework. Config. ActiveRecordSectionHandler, Castle. ActiveRecord"/>
This section node must be located in configSection, the first subnode of webconfig. Otherwise, an error is reported.
<! -- Register the Castle configuration block and type specifies the assembly of ActiveRecord --> <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 = token "requirePermission =" false "/> </configSections> <activerecord isWeb =" true "> <config> <! -- Configure the database type as SQL Server --> <add key = "connection. driver_class" value = "nhib.pdf. Driver. SqlClientDriver"/> <! -- Configure the database language as SQL Server 2008 --> <add key = "dialect" value = "nhib.pdf. Dialect. MsSql2008Dialect"/> <! -- Configure the database connection driver --> <add key = "Connection. provider" value = "nhib.pdf. connection. DriverConnectionProvider"/> <! -- Database connection string --> <add key = "proxyfactory. factory_class "value =" nhib.pdf. byteCode. castle. proxyFactoryFactory, nhib.pdf. byteCode. castle "/> <add key =" connection. connection_string "value =" server = .; database = LMS; integrated security = true; "providerName =" System. data. sqlClient "/> </config> </activerecord>

 

(3) initialize Castle and generate a database

There are many practices here. It is also possible to execute or create an event during page initialization.

For example, in the click Event of a separate aspx page:

Protected void btnCreate_Click (object sender, EventArgs e) {try {if (! ActiveRecordStarter. IsInitialized) {IConfigurationSource source = System. Configuration. ConfigurationSettings. GetConfig ("activerecord") as IConfigurationSource;
// The activerecord string must correspond to the <section name = "activerecord"> Type [] param = {typeof (Domain. years), typeof (Domain. dog), typeof (Domain. cat)}; // The actiing class adds ActiveRecordStarter at one time using array parameters. initialize (source, param); // actiing ActiveRecordStarter. createSchema (); // generates the database ActiveRecordStarter. generateCreationScripts ("create. SQL "); this. clientScript. registerStartupScript (Page. getType (), "", "<script> alert ('created successfully') </script>");} else {this. clientScript. registerStartupScript (Page. getType (), "", "<script> alert ('creation failed') </script>") ;}} catch (Exception ex) {throw ex ;}}}

This completes Castle configuration.

 

Note that the class must inherit from Activerecord in ps object relationship ing, and the specified database table name. Otherwise, the ing will fail.

  

[ActiveRecord ()] // specifies the database table Name. By default, it is the same as the class Name. public class Years: ActiveRecordBase {[Property] public string Name {get; set ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.