看PETSHP及DU7后的想法

//数据库访问的想法:
//1.按照数据库表中的表定义一个接口,比如Customer-->ICustomer,Orders-->IOrders,Account-->IAccount
// 示例代码[来自PetShop&Du7]:
using System;
namespace MySystem.IDAL
{
///

1<summary>   
2/// Inteface for the Account DAL   
3/// </summary>

public interface IAccount
{
///

1<summary>   
2/// Authenticate a user   
3/// </summary>

///

1<param name="userId"/>

Unique identifier for a user
///

1<param name="password"/>

Password for the user
///

1<returns>Details about the user who has just logged in</returns>

AccountInfo SignIn(string userId, string password);
//AccountInfo 登录后返回的信息,数据量返回少的形式用这种格式

///

1<summary>   
2/// Retrieves a book for a specified Account id.   
3/// <param name="AccountId"/>Id of Account to retrieve from database.   
4/// <retvalue>AccountData, a dataset containing detailed Account information.</retvalue>   
5/// </summary>

AccountData GetAccountById(int AccountId);
//AccountData 根据查询得到的信息,信息数据量多并要由DataGrid显示时,用这种格式

///

1<summary>   
2/// Update an account in the database   
3/// </summary>

///

1<param name="Account"/>

Account information to update
void Update(AccountInfo Account);

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

namespace MySystem.DALFactory {

///

1<summary>   
2/// Factory implementaion for the Account DAL object   
3/// </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 {
///

1<summary>   
2/// Summary description for AccountDALC.   
3/// </summary>

public class Account : IAccount{

public Account(){
}

///

1<summary>   
2/// Verify the users login credentials against the database   
3/// If the user is valid return all information for the user   
4/// </summary>

///

1<param name="userId"/>

Username
///

1<param name="password"/>

password
///

1<returns></returns>

public AccountInfo SignIn(string userId, string password)
{

return new AccountInfo();

}

///

1<summary>   
2/// Return the address information for a user   
3/// </summary>

///

1<param name="userId"/>

///

1<returns></returns>

public AccountData GetAccountById(string accountId) {

AccountData account= null;
return account;
}

///

1<summary>   
2/// Update an account in the database   
3/// </summary>

///

1<param name="myAccount"/>

Updated account parameters, you must supply all parameters
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
{
///

1<summary>   
2/// Id search.   
3/// </summary>

ID = 0,
///

1<summary>   
2/// AccountName search.   
3/// </summary>

AccountName = 1

}

///

1<summary>   
2/// Constructor to support serialization.   
3/// <remarks>Constructor that supports serialization.</remarks>   
4/// <param name="info"/>The SerializationInfo object to read from.   
5/// <param name="context"/>Information on who is calling this method.   
6/// </summary>

private AccountData(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

///

1<summary>   
2/// Constructor for AccountData.   
3/// <remarks>Initialize a AccountData instance by building the table schema.</remarks>   
4/// </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 {

///

1<summary>   
2/// Business entity used to model accounts   
3/// </summary>

[Serializable]
public class AccountInfo {

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

///

1<summary>   
2/// Default constructor   
3/// </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;
}

///

1<summary>   
2/// Returns the address information for a specific user   
3/// </summary>

///

1<param name="userId"/>

Unique identifier for an account/customer
///

1<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);
}

}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus