看PETSHP及DU7后的想法(转)

来源:互联网 发布:mysql 不存在就insert 编辑:程序博客网 时间:2024/04/27 16:31


//数据库访问的想法:
//1.按照数据库表中的表定义一个接口,比如Customer-->ICustomer,Orders-->IOrders,Account-->IAccount
// 示例代码[来自PetShop&Du7]:
using System;
namespace MySystem.IDAL
{
/// <summary>
/// Inteface for the Account DAL
/// </summary>
public interface IAccount
{
/// <summary>
/// Authenticate a user
/// </summary>
/// <param name="userId">Unique identifier for a user</param>
/// <param name="password">Password for the user</param>
/// <returns>Details about the user who has just logged in</returns>
AccountInfo SignIn(string userId, string password);
//AccountInfo 登录后返回的信息,数据量返回少的形式用这种格式

/// <summary>
/// Retrieves a book for a specified Account id.
/// <param name="AccountId">Id of Account to retrieve from database.</param>
/// <retvalue>AccountData, a dataset containing detailed Account information.</retvalue>
/// </summary>
AccountData GetAccountById(int AccountId);
//AccountData 根据查询得到的信息,信息数据量多并要由DataGrid显示时,用这种格式

/// <summary>
/// Update an account in the database
/// </summary>
/// <param name="Account">Account information to update</param>
void Update(AccountInfo Account);

}
}
//其它表的接口,和以上的类似
//2.定义工厂
//示例代码[来自PetShop&Du7]:
using System;
using System.Reflection;
using System.Configuration;

namespace MySystem.DALFactory {

/// <summary>
/// Factory implementaion for the Account DAL object
/// </summary>
public class Account
{
public static MySystem.IDAL.IAccount Create()
{
/// Look up the DAL implementation we should be using
string path = System.Configuration.ConfigurationSettings.AppSettings["DAL"];
string className = path + ".Account";

// Using the evidence given in the config file load the appropriate assembly and class
return (MySystem.IDAL.IAccount) Assembly.Load(path).CreateInstance(className);
}
}
}
//3.实现接口
//示例代码[来自PetShop&Du7]:
using System;
using System.Data;
using System.Data.SqlClient;
using PetShop.Model;
using MySystem.IDAL;

namespace MySystem.SQLServerDAL {
/// <summary>
/// Summary description for AccountDALC.
/// </summary>
public class Account : IAccount{

public Account(){
}

/// <summary>
/// Verify the users login credentials against the database
/// If the user is valid return all information for the user
/// </summary>
/// <param name="userId">Username</param>
/// <param name="password">password</param>
/// <returns></returns>
public AccountInfo SignIn(string userId, string password)
{

return new AccountInfo();

}

/// <summary>
/// Return the address information for a user
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public AccountData GetAccountById(string accountId) {

AccountData account= null;
return account;
}

/// <summary>
/// Update an account in the database
/// </summary>
/// <param name="myAccount">Updated account parameters, you must supply all parameters</param>
public void Update(AccountInfo myAccount)
{

}

}
}

//4.定义表的DataSet

namespace MySystem.Data
{
using System;
using System.Data;
using System.Runtime.Serialization;

public class AccountData : DataSet
{
public const String ACCOUNT_TABLE = "Accounts";
public const String PKID_FIELD = "PKId";
public const String ACCOUNTNAME_FIELD = "AccountName";

public enum SearchTypeEnum
{
/// <summary>
/// Id search.
/// </summary>
ID = 0,
/// <summary>
/// AccountName search.
/// </summary>
AccountName = 1

}


/// <summary>
/// Constructor to support serialization.
/// <remarks>Constructor that supports serialization.</remarks>
/// <param name="info">The SerializationInfo object to read from.</param>
/// <param name="context">Information on who is calling this method.</param>
/// </summary>
private AccountData(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

/// <summary>
/// Constructor for AccountData.
/// <remarks>Initialize a AccountData instance by building the table schema.</remarks>
/// </summary>
public BookData()
{
//
// Create the tables in the dataset
//
BuildDataTables();
}

//----------------------------------------------------------------
// Sub BuildDataTables:
// Creates the following datatables: Account
//----------------------------------------------------------------
private void BuildDataTables()
{
//
// Create the Books table
//
DataTable table = new DataTable(ACCOUNT_TABLE);
DataColumnCollection columns = table.Columns;

columns.Add(PKID_FIELD, typeof(System.Int32));
columns.Add(ACCOUNTNAME_FIELD, typeof(System.Int32));


this.Tables.Add(table);
}

//定义表Info
using System;
namespace MySystem.Data {

/// <summary>
/// Business entity used to model accounts
/// </summary>
[Serializable]
public class AccountInfo {

// Internal member variables
private string _Id;
private string _accountname;


/// <summary>
/// Default constructor
/// </summary>
public AccountInfo() {
}

public AccountInfo(string Id, string accountname)
this._Id = Id;
this._accountname = accountname;

}

// Properties
public string AccountId {
get { return _Id; }
}
public string AccountName {
get { return _accountname; }
}

}
}
//5.前台调用
using MySystem.IDAL;

namespace MySystem.BLL {

public class Account
{

public AccountInfo SignIn(string userId, string password) {

// Validate input
if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
return null;

// Get an instance of the account DAL using the DALFactory
IAccount dal = PetShop.DALFactory.Account.Create();

// Try to sign in with the given credentials
AccountInfo account = dal.SignIn(userId, password);

// Return the account
return account;
}

/// <summary>
/// Returns the address information for a specific user
/// </summary>
/// <param name="userId">Unique identifier for an account/customer</param>
/// <returns>Returns the address information for the user</returns>
public AddressInfo GetAddress(string userId)
{

// Validate input
if (userId.Trim() == string.Empty)
return null;

// Get an instance of the account DAL using the DALFactory
IAccount dal = PetShop.DALFactory.Account.Create();

// Return the address information for the given userId from the DAL
return dal.GetAddress(userId);
}


}
}

原创粉丝点击