分享
 
 
 

看PETSHP及DU7后的想法

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

//数据库访问的想法:

//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);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有