• Castle Active Record for .NET2.0快速入门示例


    一.创建Web工程

    创建一个Web站点或者Web应用程序,添加对Castle.ActiveRecord.dll的引用。

    二.创建需要持久化的业务实体

    .NET2.0下,由于引入了泛型,创建业务实体比1.1下简单了许多,业务实体只需要继承于泛型的ActiveRecordBase类,其中默认已经实现了一些静态的方法,不需要我们再在业务实体中实现。

    [ActiveRecord("Employees")]

    public class Employee : ActiveRecordBase<Employee>
    {
        
    private string employeeID;

        
    private string lastName;

        
    private string city;

        
    private string address;

        
    private string homePhone;

        
    private string country;
     

        [PrimaryKey(PrimaryKeyType.Assigned)]
        
    public string EmployeeID
        
    {
            
    get return employeeID; }

            
    set { employeeID = value; }
        }


        [Property]
        
    public string LastName
        
    {
            
    get return lastName; }

            
    set { lastName = value; }
        }


        [Property]
        
    public string City
        
    {
            
    get return city; }

            
    set { city = value; }
        }


        [Property]
        
    public string Address
        
    {
            
    get return address; }

            
    set { address = value; }
        }


        [Property]
        
    public string HomePhone
        
    {
            
    get return homePhone; }

            
    set { homePhone = value; }
        }


        [Property]
        
    public string Country
        
    {
            
    get return country; }

            
    set { country = value; }
        }

    }

    三.设置配置信息

    Web.config中设置如下信息,这部分与1.1没有什么区别

    <?xml version="1.0"?>

    <configuration>

        
    <configSections>

            
    <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>

        
    </configSections>

        
    <connectionStrings>

            
    <add name="NorthWind" connectionString="Data Source=RJ-097;Initial Catalog=Northwind;User ID=sa;Password=sa"/>

        
    </connectionStrings>

        
    <activerecord isWeb="true">

            
    <config>

                
    <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="ConnectionString = ${NorthWind}"/>

            
    </config>

        
    </activerecord>

    </configuration>

    四.初始化ActiveRecord

    Global.asaxApplication_Start添加初始化代码

    void Application_Start(object sender, EventArgs e) 
    {
    // Code that runs on application startup

    Castle.ActiveRecord.Framework.IConfigurationSource source 
    =  System.Configuration.ConfigurationManager.GetSection("activerecord"as Castle.ActiveRecord.Framework.IConfigurationSource;
        Castle.ActiveRecord.ActiveRecordStarter.Initialize(
    typeof(Employee).Assembly, source);

    }

    五.使用业务实体

    这部分也是与1.1一样,同样可以使用Create()Save()Update()等方法,不详细说了,这里我们用一个GridView来展示读取国家为UK的员工列表

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <html>

    <head runat="server">

        
    <title>Castle Active Record for 2.0快速入门示例</title>

    </head>

    <body>

        
    <form id="form1" runat="server">

         
    <h1>Castle Active Record for 2.0快速入门示例</h1>

            
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">

                
    <Columns>

                    
    <asp:BoundField HeaderText="Employee ID" DataField="EmployeeID" />

                    
    <asp:BoundField HeaderText="LastName" DataField="LastName" />

                    
    <asp:BoundField HeaderText="City" DataField="City" />

                    
    <asp:BoundField HeaderText="Address" DataField="Address" />   

                    
    <asp:BoundField HeaderText="HomePhone" DataField="HomePhone" />

                    
    <asp:BoundField HeaderText="Country" DataField="Country" />

                
    </Columns>

            
    </asp:GridView>

        
    </form>

    </body>

    </html>

    后台代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        
    this.GridView1.DataSource = Employee.FindAllByProperty("Country""UK");

        
    this.GridView1.DataBind();
    }
    最后,运行的结果如下:

    内容有些简单,后续有时间会继续介绍Castle Active Record for .NET2.0

    完整代码下载:/Files/Terrylee/ARExamWithAspnet.rar

    更多Castle的文章你可以访问:http://terrylee.cnblogs.com/archive/2006/04/28/castl_ioc_article.html

    (出处:博客园http://terrylee.cnblogs.com

  • 相关阅读:
    tyvjP1078
    红黑树笔记
    红黑树插入代码学习
    tyvjP1082找朋友
    牛棚回声USACO OCT09 3RD
    每日参悟
    全排列学习
    学习1.2
    学习笔记1.1
    学习笔记1.3
  • 原文地址:https://www.cnblogs.com/Terrylee/p/castle_activerecord_for_dotnet2_quickstart.html
Copyright © 2020-2023  润新知