castle .net之初级(一)简单示例

配置方法:

(一)配置文件

1单独的配置文件

MsSqlConfigurationSource.xml(用于配置mssql 2000

<?xml version="1.0" encoding="utf-8" ?>

<activerecord>

  <config>

    <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />

    <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql 2000 Dialect" />

    <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />

    <add key="hibernate.connection.connection_string" value="Data Source=.;Initial Catalog=selftest;UID=sa;Password=123" />

  </config>

</activerecord>

2Webconfig中的配置

<config>

      <add key="hibernate.connection.driver class" value="NHibernate.Driver.SqlClientDriver"/>

      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql 2000 Dialect"/>

      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>

      <add key="hibernate.connection.connection_string" value="ConnectionString = ${NorthWind}"/>

      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>

    </config>

(二)初始化

Application_Start事件中初始化。

1,单独的文件:

Castle.ActiveRecord.Framework.Config.XmlConfigurationSource source = new Castle.ActiveRecord.Framework.Config.XmlConfigurationSource(Server.MapPath("MsSqlConfigurationSource.xml"));

Castle.ActiveRecord.ActiveRecordStarter.Initialize(source,typeof(Customer));

2,配置文件中

Castle.ActiveRecord.Framework.IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as Castle.ActiveRecord.Framework.IConfigurationSource;

Castle.ActiveRecord.ActiveRecordStarter.Initialize(typeof(Users).Assembly, source);

(三)实体类

[ActiveRecord("Customer")]//表名

public class Customer : ActiveRecordBase

{

    public Customer()

    {

 

    }

 

    private int _unid;

    private string _FirstName;

    private string _LastName;

    [PrimaryKey(PrimaryKeyType.Assigned,"CustomerId")]

    public int Unid

    {

        get { return _unid; }

        set { _unid = value; }

    }

    [Property("FirstName")]

    public string FirstName

    {

        get { return _FirstName; }

        set { _FirstName = value; }

    }

    [Property("LastName")]

public string LastName

{

        get { return _LastName; }

        set { _LastName = value; }

    }

 

    public static IList FindAll()

    {

        return (IList)FindAll(typeof(Customer));

    }

 

}

(四)调用

Customer.FindAll();

 

更多详细内容请参见:http://terrylee.cnblogs.com/archive/2006/04/03/365762.html

 

 

你可能感兴趣的:(.net)