Petshop4

Petshop4一、Petshop4解决方案Petshop4代码和数据库:下载Petshop4详解:下载安装要求:OperatingSystem:WindowsXPSP2orWindowsServer2003Microsoft.NETFramework2.0MicrosoftSQLServer2005,SQLServerExp…


Jetbrains全系列IDE稳定放心使用

 

一、Petshop4解决方案

 

Petshop4

 

Petshop4

 

Petshop4代码和数据库:下载

Petshop4详解:下载

 

安装要求:

Operating System: Windows XP SP2 or Windows Server 2003
Microsoft.NET Framework 2.0
Microsoft SQL Server 2005, SQL Server Express, or Oracle 10g
Microsoft Internet Explorer 6 or greater
Microsoft Visual Studio® .NET 2005

 

后续步骤:

1、手动编译:运行<Pet Shop 4 Install Folder>\Build.bat,将编译整个解决方案。

 

2、用VisualStudio自带的浏览器运行:

(1)打开解决方案。Start Menu | All Programs | Microsoft .NET Pet Shop 4 | Petshop.sln

(2)设置Web项目为启动项目

(3)右键单击“解决方案”节点,点“构建”

(4)打开Web项目,右键单击“Default.aspx”,点“在浏览器中查看”

(5)如果安装了Membership/Profile数据库,系统会创建12个帐号,即demo, AdamBarr, KimAbercrombie,RobYoung, TomYoutsey, GaryWYukish, RobCaron, KarinZimprich, RandallBoseman, KevinKennedy, DianeTibbott, 或GarrettYoung。密码是pass@word1

 

 

3、运行在windows server 2003的IIS上

(1)先解密web.config文件中的连接串。

   点击<Pet Shop 4 Install Folder>\DecryptWebConfig.bat解密连接串。不解密的话IIS会报错。

 

(2)由于2003IIS的.net版本最高为2.0。笔者在编译Petshop的Web项目时,需要设置属性页的“目标Framework”为2.0。如下图:

Petshop4

 

(3)视情况修改web.config中的数据库连接串,确保能正确连上数据库。

(4)将Web文件夹实施到IIS中。

     Petshop4

 

     运行效果如下:

    Petshop4

 

 

二、Petshop4体系结构

 

Petshop4

 

 

三、Petshop三层结构总结

 

1、表记录 –> 实体类

    将Category数据库表中的单个记录定义为一个类(叫产品实体),类的属性就是数据库表中每个记录的字段。产品实体承载数据表的记录。所有实体类的集合形成了Petshop中Model模块。Category实体的代码如下所示:

 

using System;

namespace PetShop.Model {

    
public 
class CategoryInfo {

        
//属性

        
private 
string id;

        
private 
string name;

        
private 
string description;

        
public CategoryInfo() { }

        
public CategoryInfo(
string id, 
string name, 
string description) {

            
this.id = id;

            
this.name = name;

            
this.description = description;

        }

        
//
 属性的get操作

        
public 
string Id {

            
get { 
return id; }

        }

        
public 
string Name {

            
get { 
return name; }

        }

        
public 
string Description {

            
get { 
return description; }

        }

    }

}

 

 

2、记录的删改增查等操作 -> 转换成访问数据库的函数 -> 函数组织到一起形成类

 

3、-> 由于数据库多样性&访库操作的一致性(不外乎删改增查操作) -> 抽象出一个接口

    数据访问层采用“面向接口编程”思想,抽象出来的IDAL模块,脱离了与具体数据库的依赖,从而使得整个数据访问层利于数据库迁移。SQLServerDAL和OracleDAL模块均实现IDAL模块的接口,其中包含的逻辑就是对数据库的Select,Insert,Update和Delete操作。因为数据库类型的不同,对数据库的操作也有所不同,代码也会因此有所区别。

 

using System;


using PetShop.Model;


using System.Collections.Generic;

namespace PetShop.IDAL{

    
///
 
<summary>

    

///
 类别数据访问层的接口
    

///
 
</summary>

    
public 
interface ICategory {

        
///
 
<summary>

        

///
 此方法用于获得所有宠物类别
        

///
 
</summary>
        
        

///
 
<returns>
此接口用于生成普通类别的模型集合
</returns>

        IList<CategoryInfo> GetCategories();

        
///
 
<summary>

        

///
 获取某个宠物类别的信息
        

///
 
</summary>

        

///
 
<param name=”categoryId”>
宠物的唯一标示符
</param>

        

///
 
<returns>
描述一个宠物业务逻辑的实体类
</returns>

        CategoryInfo GetCategory(
string categoryId);

    }

}

 

针对SQLServer数据库的实现

using System;


using System.Data;


using System.Data.SqlClient;


using PetShop.Model;


using PetShop.IDAL;


using System.Collections.Generic;


using PetShop.DBUtility;

namespace PetShop.SQLServerDAL {

    
public 
class Category : ICategory {

        
//
 声明静态变量

        
private 
const 
string SQL_SELECT_CATEGORIES = 
“
SELECT CategoryId, Name, Descn FROM Category
“;

        
private 
const 
string SQL_SELECT_CATEGORY = 
“
SELECT CategoryId, Name, Descn FROM Category WHERE CategoryId = @CategoryId
“;

        
private 
const 
string PARM_CATEGORY_ID = 
“
@CategoryId
“;

        
///
 
<summary>

        

///
 此方法用于获得所有宠物类别信息
        

///
 
</summary>
             

        
public IList<CategoryInfo> GetCategories() {

            IList<CategoryInfo> categories = 
new List<CategoryInfo>();

            
//
执行获取所有宠物类别的SQL语句

            
using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, 
null)) {

                
while (rdr.Read()) {

                    CategoryInfo cat = 
new CategoryInfo(rdr.GetString(
0), rdr.GetString(
1), rdr.GetString(
2));

                    categories.Add(cat);

                }

            }  

            
return categories;

        }

        
///
 
<summary>

        

///
 根据提供的id ,获取指定的单个宠物类别信息
        

///
 
</summary>

        

///
 
<param name=”categoryId”>
宠物类别标识码d
</param>

        

///
 
<returns>
单个宠物类别的详细资料
</returns>

        
public CategoryInfo GetCategory(
string categoryId) {

            
//
设置一个返回值

            CategoryInfo category = 
null;

            
//
创建一个参数

            SqlParameter parm = 
new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 
10);

            
//
绑定参数

            parm.Value = categoryId;

            
//
执行查询

            
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORY, parm)) {

                
if (rdr.Read())

                    category = 
new CategoryInfo(rdr.GetString(
0), rdr.GetString(
1), rdr.GetString(
2));

                
else

                    category = 
new CategoryInfo();

            }

            
return category;

        }

        
///
 
<summary>

        

///
 为创建宠物类别创建一个SqlCommand对象
        

///
 
</summary>

        

///
 
<param name=”id”>
宠物类别码
</param>

        

///
 
<returns>
用于找回宠物类别的sql命令
</returns>

        
public 
static SqlCommand GetCommand() {

            
return 
new SqlCommand(SQL_SELECT_CATEGORIES);               

        }

    }

}

 

针对Oracle数据库的实现

using System;


using System.Data;


using System.Data.OracleClient;


using PetShop.Model;


using PetShop.IDAL;


using System.Collections.Generic;


using PetShop.DBUtility;

namespace PetShop.OracleDAL {

    
public 
class Category : ICategory {

        
//
 Static constants

        
private 
const 
string SQL_SELECT_CATEGORIES = 
“
SELECT CategoryId, Name, Descn FROM Category
“;

        
private 
const 
string SQL_SELECT_CATEGORY = 
“
SELECT CategoryId, Name, Descn FROM Category WHERE CategoryId = :CategoryId
“;

        
private 
const 
string PARM_CATEGORY_ID = 
“
:CategoryId
“;

        
///
 
<summary>

        

///
 Method to get all categories
        

///
 
</summary>

        
public IList<CategoryInfo> GetCategories() {

            IList<CategoryInfo> categories = 
new List<CategoryInfo>();

            
//
Execute a query to read the categories

            
using(OracleDataReader rdr = OracleHelper.ExecuteReader(OracleHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, 
null)) {

                
while (rdr.Read()) {

                    CategoryInfo cat = 
new CategoryInfo(rdr.GetString(
0), rdr.GetString(
1), rdr.GetString(
2));

                    categories.Add(cat);

                }

            }

            
return categories;

        }

        
///
 
<summary>

        

///
 Get an individual category based on a provided id
        

///
 
</summary>

        

///
 
<param name=”categoryId”>
Category id
</param>

        

///
 
<returns>
Details about the Category
</returns>

        
public CategoryInfo GetCategory(
string categoryId) {

            
//
Set up a return value

            CategoryInfo category = 
null;

            
//
Create a parameter

            OracleParameter parm = 
new OracleParameter(PARM_CATEGORY_ID, OracleType.Char, 
10);

            
//
Bind the parameter

            parm.Value = categoryId;

            
//
Execute the query    

            
using (OracleDataReader rdr = OracleHelper.ExecuteReader(OracleHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORY, parm)) {

                
if (rdr.Read())

                    category = 
new CategoryInfo(rdr.GetString(
0), rdr.GetString(
1), rdr.GetString(
2));

                
else

                    category = 
new CategoryInfo();

            }

            
return category;

        }

    }

}

 

4、到配置文件中做“决定”

     用户如何决定用哪种访问数据的类来访问数据库?用户不必关心。因为这个决定写在配置文件web.config中了。迁移到另一种数据时,只需修改web.config,而不需要修改代码。 为了做到这一点,.NET用反射。代码如下:

web.config如下:

<?xml version=
“
1.0
“?>

<configuration xmlns=
“
http://schemas.microsoft.com/.NetConfiguration/v2.0
“>

    <connectionStrings>

        <!– SQL connection 
string 
for Profile database –>

        <add name=
“
SQLProfileConnString
“ connectionString=
“
server=(local);user id=sa;password=jazzking;database=MSPetShop4Profile;min pool size=4;max pool size=4;packet size=3072
“ providerName=
“
System.Data.SqlClient
“/>

        <!– SQL connection 
string 
for Membership database –>

        <add name=
“
SQLMembershipConnString
“ connectionString=
“
server=(local);user id=sa;password=jazzking;database=MSPetShop4Services;min pool size=4;max pool size=4;packet size=3072
“ providerName=
“
System.Data.SqlClient
“/>

        <!– SQL connection 
string 
for Inventory database lookup –>

        <add name=
“
SQLConnString1
“ connectionString=
“
server=(local);user id=sa;password=jazzking;database=MSPetShop4;min pool size=4;max pool size=4;packet size=3072
“ providerName=
“
System.Data.SqlClient
“/>

        <!– SQL connection 
string 
for handling transactions on the Inventory database–>

        <add name=
“
SQLConnString2
“ connectionString=
“
server=(local);user id=sa;password=jazzking;database=MSPetShop4;min pool size=4;max pool size=4;packet size=1024
“ providerName=
“
System.Data.SqlClient
“/>

        <!– SQL connection 
string 
for Orders database–>

        <add name=
“
SQLConnString3
“ connectionString=
“
server=(local);user id=sa;password=jazzking;database=MSPetShop4Orders;min pool size=4;max pool size=4;packet size=1024
“ providerName=
“
System.Data.SqlClient
“/>

        <!– Oracle connection strings–>

        <add name=
“
OraProfileConnString
“ connectionString=
“
Data Source=localhost;user id=MSPETSHOPPROFILE;password=pass@word1;min pool size=4;max pool size=4
“ providerName=
“
System.Data.OracleClient
“/>

        <add name=
“
OraMembershipConnString
“ connectionString=
“
Data Source=localhost;user id=MSPETSHOPMEMBERSHIP;password=pass@word1;min pool size=4;max pool size=4
“ providerName=
“
System.Data.OracleClient
“/>

        <add name=
“
OraConnString1
“ connectionString=
“
Data Source=localhost;user id=MSPETSHOP;password=pass@word1;min pool size=4;max pool size=4
“ providerName=
“
System.Data.OracleClient
“></add>

        <add name=
“
OraConnString2
“ connectionString=
“
Data Source=localhost;user id=MSPETSHOP;password=pass@word1;min pool size=4;max pool size=4
“ providerName=
“
System.Data.OracleClient
“></add>

        <add name=
“
OraConnString3
“ connectionString=
“
Data Source=localhost;user id=MSPETSHOPORDERS;password=pass@word1;min pool size=4;max pool size=4
“ providerName=
“
System.Data.OracleClient
“></add>

    </connectionStrings>

    <appSettings>

        <!– Pet Shop DAL configuration settings –>

        <!–设置数据库连接类型  value 此数据库联接类 –>


        <add key=“WebDAL“ value=“PetShop.SQLServerDAL“/>
        <add key=“OrdersDAL“ value=“PetShop.SQLServerDAL“/>


        <add key=
“
ProfileDAL
“ value=
“
PetShop.SQLProfileDAL
“/>

 

对数据库中所有表的操作接口打成包方便统一访问,Petshop中叫数据库访问层工厂。如下:

using System.Reflection;


using System.Configuration;

namespace PetShop.DALFactory {

    
public 
sealed 
class DataAccess {

        
//
 查找我们将要使用的数据访问层

        
private 
static 
readonly 
string path = ConfigurationManager.AppSettings[
“
WebDAL
“];
//对照web.config,此时path的值为“PetShop.SQLServerDAL
”。表示该系统使用了SQLServer数据库。

        
private 
static 
readonly 
string orderPath = ConfigurationManager.AppSettings[
“
OrdersDAL
“];

        

        
private DataAccess() { }

        
//
实现 CreateCategory 方法,用于创建Category类实例

        
public 
static PetShop.IDAL.ICategory CreateCategory() {

            
string className = path + 
“
.Category
“;

            
//
利用反射技术,动态加载指定类型

            
return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);

        }

        
//
实现 CreateInventory 方法,用于创建Inventory类实例  

        
public 
static PetShop.IDAL.IInventory CreateInventory() {

            
string className = path + 
“
.Inventory
“;

            
return (PetShop.IDAL.IInventory)Assembly.Load(path).CreateInstance(className);

        }

        
//
实现 CreateItem 方法,用于创建Item类实例  

        
public 
static PetShop.IDAL.IItem CreateItem() {

            
string className = path + 
“
.Item
“;

            
return (PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);

        }

        
//
实现 CreateOrder 方法,用于创建Order类实例  

        
public 
static PetShop.IDAL.IOrder CreateOrder() {

            
string className = orderPath + 
“
.Order
“;

            
return (PetShop.IDAL.IOrder)Assembly.Load(orderPath).CreateInstance(className);

        }

        
//
实现 CreateProduct 方法,用于创建Product类实例  

        
public 
static PetShop.IDAL.IProduct CreateProduct() {

            
string className = path + 
“
.Product
“;

            
return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);

        }

    }

}

 

以上是数据库访问层的内容

 

5、业务逻辑层必须独立

既然是逻辑,那么该层处理的内容可以说基本上是一些if…else等等。另外,业务逻辑层需要访问数据库时,是通过“数据库访问层工厂”进行的,不再是直接在里面傻傻的写sql语句。

using System.Collections.Generic;


using PetShop.Model;


using PetShop.IDAL;

namespace PetShop.BLL 

{

    
///
 
<summary>

    

///
 实现有关宠物类别的业务逻辑类
    

///
 
</summary>

    
public 
class Category 

    {

        
//
从数据访问层工厂类得到一个种类的数据访问层的实例
        

//
确保这个静态可以在初始化后缓存数据访问层

       
 private static readonly ICategory dal = PetShop.DALFactory.DataAccess.CreateCategory(); 通过数据访问层工厂访问数据库

          

        
public IList<CategoryInfo> GetCategories() 

        {

            
return dal.GetCategories();

        }

        
public CategoryInfo GetCategory(
string c
ategoryId) 


        {

            
//
 确认输入参数

            
if
 (
string
.IsNullOrEmpty(catego
ryId)) ///逻辑层的if语句

                
return 
null;

            
//
通过类别ID到数据访问层去查询

            
return dal.GetCategory(categoryId);

        }

    }

}

 

 

 

四、Global.asax和web.config

 

Petshop4

 

Global.asax:http://hi.baidu.com/mycolorwind/blog/item/45384980228cbfdf9023d960.html

global.asax详解:http://club.topsage.com/thread-485397-1-1.html

global.asax介绍:http://blog.csdn.net/suntanyong88/article/details/6009278

关于global.asax总结经验:http://hi.baidu.com/chinadaima/blog/item/32e0c9ef53b81f32adafd566.html

 

web.config详解:http://blog.csdn.net/zhoufoxcn/article/details/3265141

 

数据连接串:http://www.cnblogs.com/zlytu/archive/2011/11/22/2258364.html

 

Petshop分析:http://www.cnblogs.com/sunfishlu/archive/2008/02/02/1062049.html

走进.net架构:http://www.cnblogs.com/yanyangtian/category/265762.html

 

转载于:https://www.cnblogs.com/JCSU/articles/2542868.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181004.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ps测量精灵图[通俗易懂]

    ps测量精灵图[通俗易懂]1.调出图片信息2.测量图片大小、3.把鼠标放在需要用到的小图片的左上角  精灵图的测量:B站中有详细的教程    第43集  京东项目精灵图的使用:B站精灵图的使用第24集   李江南讲解精灵图第44集  定位口诀:子绝父相(孩子是绝对定位,父亲是相对定位;)…

    2022年6月10日
  • C++函数指针、指针函数、返回值为函数指针的函数浅谈

    C++函数指针、指针函数、返回值为函数指针的函数浅谈C++函数指针、指针函数、返回值为函数指针的函数浅谈引言函数指针、指针函数是C中重要而容易混淆的概念,博主将通过两个实例来说明这两个截然不同的概念。而返回值为函数指针的指针函数就更难理解了,放在文章的最后来介绍。函数指针函数指针是一种特殊的指针,它指向函数的入口。例如int(*add)(int,int);定义了一个函数指针add,用于指向返回值为int,并且有两个int参数的函数…

    2022年6月22日
  • eclipse安卓项目_eclipse开发android

    eclipse安卓项目_eclipse开发androideclipse之安卓开发环境搭建一、安装JAVAJDK:二、安装AndroidSDK:三、为eclipse安装ADT插件。五、检查eclipse中的安卓SDK路径六、创建eclipse中的安卓模拟器网上不知道哪个哥们教程里提供的JAVAJDK15的安装包有问题,安装AndroidSDK的时候一直找不到它,干脆自己重新整理一份安装资料出来。一、安装JAVAJDK:下载JDK15的官网下载地址点这里,只需要下载下图中这个exe文件。安装一直按照提示点击即可,主要是选择安装路径,没啥其它

    2022年9月26日
  • 优化算法——牛顿法(Newton Method)

    优化算法——牛顿法(Newton Method)一、牛顿法概述

    2022年5月6日
  • linux的vi命令详解_linux中如何更新vi

    linux的vi命令详解_linux中如何更新vi最近vi用的多,很多技巧不知道,备注一份,vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令。由于对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方进一步了解它。Vi也是Linux中最基本的文本编辑器,学会它后,您将在Linux的世界里畅行无阻。

    2022年10月31日
  • c# savefiledialog_game是什么意思中文

    c# savefiledialog_game是什么意思中文1、这里通过一个例子来讲解saveFileDialog的使用方法,首先创建一个小的winform程序,然后在主界面中放入一个按钮、一个textbox,按钮用来弹出saveFileDialog,然后textbox用来接收saveFileDialog选择之后的文件路径,如下图:2、下图中的代码就是程序的全部代码了,对其中重要的代码进行解释:InitialDirectory方法:设置save…

    2022年9月25日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号