程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Active Record學習筆記(五):處理繼承關系

Active Record學習筆記(五):處理繼承關系

編輯:關於.NET

本文主要介紹了如何使用Castle.ActiveRecord來處理繼承關系。

本文涉及兩個實體類:基類(User)、子類(Employee)。以下是類圖:

本文主要內容:

1.編寫數據庫腳本

2.JoinedBase和JoinedKey屬性說明

3.編寫實體類

4.編寫調用代碼

一、編寫數據庫腳本

其實本文涉及的數據表在前面的筆記中都出現過!

Create Table Users
(
 ID int identity(1,1) primary key,
 LoginName Varchar(50) not null,
 Password Varchar(20) not null
)
  
Create Table Employees
(
 ID int primary key,
 Salary money
)

二、JoinedBase和JoinedKey屬性說明

JoinedBase屬性:該屬性是ActiveRecord屬性子屬性,用於基類中;

JoinedKey性性:該屬性用在子類中,用於代替PrimaryKey的位置;

三、編寫實體類

基類:User.cs /**////[email protected]

///2006-09-24

using System;
using System.Collections.Generic;
using System.Text;
  
using Castle.ActiveRecord;
  
namespace Inherit
{
  [ActiveRecord("Users"),JoinedBase] //JoinedBase
  public class User : ActiveRecordBase
  {
    private int intID;
    private string strUserName;
    private string strUserPsd;
  
    public User()
    {
      intID = 0;
      strUserName = string.Empty;
      strUserPsd = string.Empty;
    }
  
    [PrimaryKey(PrimaryKeyType.Identity,"ID")]
    public int ID
    {
      get
      {
        return intID;
      }
      set
      {
        intID = value;
      }
    }
  
    /**//// <summary>
    /// 用戶名
    /// </summary>
    [Property("LoginName")]
    public string Name
    {
      get
      {
        return strUserName;
      }
      set
      {
        strUserName = value;
      }
    }
  
    /**//// <summary>
    /// 用戶密碼
    /// </summary>
    [Property("Password")]
    public string Password
    {
      get
      {
        return strUserPsd;
      }
      set
      {
        strUserPsd = value;
      }
    }
  }
}

子類:Employee.cs /**////[email protected]

///2006-09-24

using System;
using System.Collections.Generic;
using System.Text;
  
using Castle.ActiveRecord;
  
namespace Inherit
{
  [ActiveRecord("Employees")]
  public class Employee : User
  {
    private int intEID;
    private decimal decimalSalary;
  
    public Employee()
    {
      intEID = 0;
      decimalSalary = 0;
    }
  
    [JoinedKey("ID")]  //JoinedKey代替PrimaryKey的位置
    public int EID
    {
      get
      {
        return intEID;
      }
      set
      {
        intEID = value;
      }
    }
    
    /**//// <summary>
    /// 薪水
    /// </summary>
    [Property]
    public decimal Salary
    {
      get
      {
        return decimalSalary;
      }
      set
      {
        decimalSalary = value;
      }
    }
  }
}

四、編寫調用代碼(只列出添加Employee的代碼)

private void button2_Click(object sender, EventArgs e)
{
  IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
  ActiveRecordStarter.Initialize(source, typeof(Inherit.Employee), typeof(Inherit.User));
  
  //使用事務處理
  using (TransactionScope tran = new TransactionScope())
  {
    Inherit.Employee objEmployee = new Inherit.Employee();
  
    objEmployee.Name = "jailu";
    objEmployee.Password = "123456789";
    objEmployee.Salary = 1000;
  
    objEmployee.Save();
  }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved