zoukankan      html  css  js  c++  java
  • Castle ActiveRecord学习笔记一:创建一个实例

    Castle ActiveRecord抛弃了Nhibernate的繁杂的配置,采用了一种更加方便的方式来组织项目结构,是对Nhibernate的再次封装,大大简化了开发,下面就来说明其配置过程。

    工欲善其事必先利其器,首先我们需要下载,下载地址如下:http://sourceforge.net/projects/castleproject/files/ActiveRecord/3.0/Castle.ActiveRecord-3.0.RC.zip/download  这里下载完成后,我们解压到一个文件夹中,就得到如下的文件排列形式:

    然后我们新建一个工程,包含一个User的lib项目和一个winform的项目,具体组织形式如下:

    步骤一、现在我们开始来建表,我们建立一个NewsDemo的数据库,然后在其下建立一个Users表,如下所示:

    CREATETABLE[dbo].[Users] (
    [LogonID][int]IDENTITY (1, 1) NOTNULL ,
    [LogonName][varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
    [Password][varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
    [EmailAddress][varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
    [LastLogon][datetime]NULL
    )
    ON[PRIMARY]
    GO

    步骤二、编写操作Users表的实体类

    这个实体类名称是User.cs,具体代码如下:

    using System;
    using Castle.ActiveRecord;
    using System.Collections;

    namespace CastleLib
    {
    [ActiveRecord(
    "Users")]
    publicclass User:ActiveRecordBase
    {

    #region 公共对应属性
    privateint _id;

    privatestring _name;

    privatestring _password;

    privatestring _emailAddress;

    private DateTime _lastLogon;

    [PrimaryKey(PrimaryKeyType.Identity,
    "LogonID")]
    publicint Id
    {
    get { return _id; }
    set { _id = value; }
    }

    [Property(
    "LogonName")]
    publicstring Name
    {
    get { return _name; }
    set { _name = value; }
    }

    [Property(
    "Password")]
    publicstring Password
    {
    get { return _password; }
    set { _password = value; }
    }

    [Property(
    "EmailAddress")]
    publicstring Address
    {
    get { return _emailAddress; }
    set { _emailAddress = value; }
    }

    [Property(
    "LastLogon")]
    public DateTime LastLogon
    {
    get { return _lastLogon; }
    set { _lastLogon = value; }
    }
    #endregion

    publicstaticvoid DeleteAll()
    {
    DeleteAll(
    typeof(User));
    }

    publicstatic IList FindAll()
    {
    return (IList)FindAll(typeof(User));
    }

    publicstatic User Find(int id)
    {
    return (User)FindByPrimaryKey(typeof(User), id);
    }
    }
    }

    在这里需要注意的是类的属性,这里是ActiveRecord("Users"),表明对应的是数据库中的Users表,而PrimaryKey则是表明主键,Property则是对应的属性列。编写的所有实体类,都要集继承自ActiveRecordBase类,这个是ActiveRecord操作的基类。上面的代码包含了一部分的CRUD操作,这些操作都是继承自ActiveRecordBase类中的,非常方便编写。

    步骤三、配置映射文件

    这里的配置,我是直接写到代码中的:

    privatevoid Init()
    {
    // XmlConfigurationSource source = new XmlConfigurationSource("http://www.cnblogs.com/App.config");
    //// IConfigurationSource source = ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
    // ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));

    InPlaceConfigurationSource source
    =new InPlaceConfigurationSource();
    IDictionary
    <string, string> properties =new Dictionary<string, string>();
    properties.Add(
    "connection.driver_class", "NHibernate.Driver.SqlClientDriver");
    properties.Add(
    "dialect", "NHibernate.Dialect.MsSql2008Dialect");
    properties.Add(
    "connection.provider", "NHibernate.Connection.DriverConnectionProvider");
    properties.Add(
    "proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
    properties.Add(
    "connection.connection_string", "UID=sa;Password=******;Initial Catalog=NewsDemo;Data Source=.;");
    source.Add(
    typeof(ActiveRecordBase), properties);
    ActiveRecordStarter.Initialize(source,
    typeof(CastleLib.User));
    }

    需要注意的是,这个配置节 properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"); 一定要添加上,否则会出现未知的错误。

    步骤四、操作数据库,这里我贴上具体的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Castle.ActiveRecord.Framework;
    using System.Configuration;
    using Castle.ActiveRecord;
    using Castle.ActiveRecord.Framework.Config;
    using System.Collections;
    using CastleLib;

    namespace CastleAPP
    {
    publicpartialclass MainFrm : Form
    {
    public MainFrm()
    {
    InitializeComponent();
    Init();
    }

    privatevoid Init()
    {
    // XmlConfigurationSource source = new XmlConfigurationSource("http://www.cnblogs.com/App.config");
    //// IConfigurationSource source = ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
    // ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));

    InPlaceConfigurationSource source
    =new InPlaceConfigurationSource();
    IDictionary
    <string, string> properties =new Dictionary<string, string>();
    properties.Add(
    "connection.driver_class", "NHibernate.Driver.SqlClientDriver");
    properties.Add(
    "dialect", "NHibernate.Dialect.MsSql2008Dialect");
    properties.Add(
    "connection.provider", "NHibernate.Connection.DriverConnectionProvider");
    properties.Add(
    "proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
    properties.Add(
    "connection.connection_string", "UID=sa;Password=251147;Initial Catalog=NewsDemo;Data Source=.;");
    source.Add(
    typeof(ActiveRecordBase), properties);
    ActiveRecordStarter.Initialize(source,
    typeof(CastleLib.User));
    }

    privatevoid Form1_Load(object sender, EventArgs e)
    {
    AddUsers();
    IList list
    = CastleLib.User.FindAll();

    dataGridView1.DataSource
    = list;
    }

    publicvoid AddUsers()
    {
    CastleLib.User user
    =new CastleLib.User();
    user.Name
    ="***";
    user.Password
    ="ceshi";
    user.Address
    ="河南省信阳市";
    user.LastLogon
    = DateTime.Now;
    user.Create();
    }
    }
    }

    这样运行的时候,我们就可以看到数据被正确的填装到数据库中了。

     

  • 相关阅读:
    在LoadRunner向远程Linux/Unix执行命令行并收集性能数据
    在LoadRunner中执行命令行程序之:popen()取代system()
    Linux基础--分类与合并命令
    sed and awk学习笔记
    vim操作
    生命是一种长期而持续的累积过程
    SQL Server数据导入导出的几种方法
    Http协议
    LeetCode 470. 用 Rand7() 实现 Rand10()(Implement Rand10() Using Rand7())
    LeetCode 238. 除自身以外数组的乘积( Product of Array Except Self)
  • 原文地址:https://www.cnblogs.com/scy251147/p/2155974.html
Copyright © 2011-2022 走看看