PetShop4.0,是微软公司为了展示他的Visual Studio的功能,而提供的一个示例源代码,程序虽小,却包含了ASP.NET的很多技术,非常适合初学者拿来学习、研究。

这个PetShop虽然是宠物商店,但由于他的代码规范、结构经典,可以很容易地改写成其它的网上购物系统,很想照葫芦画瓢,做一个NetShop,就叫做山塞版的PetShop吧。 

1、“文件”→“新建项目”,打开“新建项目”对话框,在“项目类型”中选择“Visual Studio解决方案”,在“模板”中选择“空白解决方案”

2、“文件”→“添加”→“新建网站”

3、“文件”→“添加”→“新建项目”,打开“新建项目”对话框,新建类库BLL

4、同样步骤,添加项目DALFactory、DBUtility、IDAL、Model和SQLServerDAL


5、修改6个类库属性中的“程序集名称”和“默认命名空间”


求教:类库项目的属性中可以更改“程序集名称”和“默认命名空间”,以后理添加类文件时,命名空间会自动生成。

Web项目中却不能,有没有类似的方法,让设置Web中的文件的命名空间更方便一点呢??

6、 复制DBUtility中的SQLHelper.cs

    复制Model中的所有*Info.cs文件,AssemblyInfo.cs除外。

    复制IDAL中的ICategory.cs

    复制SQLServerDAL中的Category.cs

    复制BLL中的Category.cs

    复制DALFactory中的DataAccess.cs,修改其中的内容如下:


    using System.Reflection;

    using System.Configuration;

    namespace NetShop.DALFactory

    {

        /// <summary>

        /// This class is implemented following the Abstract Factory pattern to create the DAL implementation

        /// specified from the configuration file

        /// </summary>

        public sealed class DataAccess

        {

            // Look up the DAL implementation we should be using

            private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];

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


            private DataAccess() { }

            public static NetShop.IDAL.ICategory CreateCategory()

            {

                string className = path + ".Category";

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

            }

        }

    }

7、添加相应的引用,并替换文件中的PetShop为NetShop

8、右击Web项目→“新建文件夹”,新建文件夹Controls

   右击Controls→“添加新项”, 添加用户控件NavigationControl.ascx

   NavigationControl.ascx中代码如下:

   <body>

    <asp:Repeater ID="repCategories" runat="server">

        <HeaderTemplate>

            <table cellspacing="0" border="0" style="border-collapse: collapse;">

        </HeaderTemplate>

        <ItemTemplate>

            <tr>

                <td>

                    <asp:HyperLink runat="server" ID="lnkCategory"><%# DataBinder.Eval(Container.DataItem,"Name") %></asp:HyperLink>

                </td>

            </tr>

        </ItemTemplate>

        <FooterTemplate>

            </table>

        </FooterTemplate>

    </asp:Repeater>

   </body>


   NavigationControl.ascx.cs中代码如下:

   namespace NetShop.Web

{

    public partial class NavigationControl : System.Web.UI.UserControl

    {


        protected void Page_Load(object sender, EventArgs e)

        {

            BindCategories();

        }

        // Bind categories

        private void BindCategories() {

            Category category = new Category();

            repCategories.DataSource = category.GetCategories();

            repCategories.DataBind();           

        }

    }

}

9、在Default.aspx中,拖入用户控件NavigationControl.ascx



10、复制连接字符串

<connectionStrings>
<add name="SQLProfileConnString" connectionString="server=(local)\sqlexpress;database=MSPetShop4Profile;integrated security=SSPI;min pool size=4;max pool size=4;" providerName="System.Data.SqlClient" />
<add name="SQLMembershipConnString" connectionString="server=(local)\sqlexpress;database=MSPetShop4Services;integrated security=SSPI;min pool size=4;max pool size=4;" providerName="System.Data.SqlClient" />
<add name="SQLConnString1" connectionString="server=(local)\sqlexpress;database=MSPetShop4;integrated security=SSPI;min pool size=4;max pool size=4;" providerName="System.Data.SqlClient" />
<add name="SQLConnString2" connectionString="server=(local)\sqlexpress;database=MSPetShop4;integrated security=SSPI;max pool size=4;min pool size=4;" providerName="System.Data.SqlClient" />
<add name="SQLConnString3" connectionString="server=(local)\sqlexpress;database=MSPetShop4Orders;integrated security=SSPI;min pool size=4;max pool size=4;" providerName="System.Data.SqlClient" />
</connectionStrings>


11、复制

<appSettings>
<add key="WebDAL" value="NetShop.SQLServerDAL"/>
<add key="OrdersDAL" value="NetShop.SQLServerDAL"/>
</appSettings>


12、右击“Default.aspx“→“在浏览器中查看”