在ASP.NET MVC4中配置Castle

來源:互聯網
上載者:User

標籤:nat   繼承   actor   UI   alert   :active   sql   mvc架構   字元   

 

   Castle是針對.NET平台的一個非常優秀的開源項目,重點是開源的哦。它在NHibernate的基礎上進一步封裝,其原理基本與NHibernate相同,但它較好地解決NHibernate的缺陷,從ORM(對象關係映射)到IOC(inversion of control,控制反轉)容器,再到web層的MVC架構,基本上包括整個開發過程的所有內容。

在VS2013 MVC4+SQL Server 2008的環境下配置Castle可以簡單分為如下4個步驟

(一)引用Castle,有兩種方式

   可以直接使用Nuget直接下載Castle相關組件  Castle.ActiveRecord提供ORM,Castle.Core,Castle.Windsor

   如果有備份的dll檔案,可以直接引用,但一定要引用NHibernate.dll

     

(二)配置webconfig

  

<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
這個section節點必須要位於webconfig的第一個子節點configSection裡 否則就會報錯
<!--註冊Castle配置塊,type指定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=b77a5c561934e089" requirePermission="false" />  </configSections>  <activerecord isWeb="true">    <config>      <!--設定資料庫類型為SQL Server-->      <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />      <!--設定資料庫語言為SQL Server 2008-->      <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="server=.;database=LMS;integrated security=true;" providerName="System.Data.SqlClient" />    </config>  </activerecord>

 

(三)初始化Castle,產生資料庫

   這裡的做法有很多,在頁面初始化時進行或者建立一個事件也是可以的

   比如在單獨的一個aspx頁面的click事件裡進行:

    

    protected void btnCreate_Click(object sender, EventArgs e)        {            try            {                if (!ActiveRecordStarter.IsInitialized)                {                    IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord")                         as IConfigurationSource;
            //activerecord字串要與<section name="activerecord">對應 Type[] param
={typeof(Domain.Years), typeof(Domain.Dog), typeof(Domain.Cat)};//映射類 通過數組參數一次性添加 ActiveRecordStarter.Initialize(source, param);//映射 ActiveRecordStarter.CreateSchema();//產生資料庫 ActiveRecordStarter.GenerateCreationScripts("create.sql"); this.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert(‘建立成功‘)</script>"); } else { this.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert(‘建立失敗‘)</script>"); } } catch (Exception ex) { throw ex; } } }

 這樣就完成了Castle的配置了

 

ps 對象關係映射中要注意類要繼承於Activerecord,和指定資料庫表名  否則會映射失敗

  

  [ActiveRecord()]//指定資料庫表名,預設與類名相同    public class Years:ActiveRecordBase    {        [Property]        public string Name { get; set; }    }

 

在ASP.NET MVC4中配置Castle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.