2022年nopCommerce的源代码结构和架构说明 .pdf
《2022年nopCommerce的源代码结构和架构说明 .pdf》由会员分享,可在线阅读,更多相关《2022年nopCommerce的源代码结构和架构说明 .pdf(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、nopCommerce的源代码结构和架构编写本文档是为了向程序员说明nopcommerce的解决方案结构,亦是程序员开发nopcommerce的居家必备良书。首先nopcommerce的源代码很容易拿到,它是开源的,所以你可以直接到网上下载。在你打开 VS 以后项目和文件夹都会完整列出来,我们建议你在看此文档的同时也打开你的VS 来浏览项目和文件。绝大多数的项目,目录和文件都顾名思义,你可以从名字就大概知道是做什么的。比如Nop.Plugin.Payments.PayPalStandard这个我都不用看项目代码就能猜到做什么的。LibrariesNop.CoreNop.Core 项目包含 no
2、pcommerce的一系列核心类如缓存,事件,辅助类和业务对象(如订单和客户实体类)LibrariesNop.DataNop.Data 项目包含一系列的数据访问类和方法以从数据库或其他数据媒介读取和保存数据。它也有助于把数据访问逻辑和你的业务对象分离。nopcommerce使用 Entity Framework(EF)Code-First方法,允许你在 nopcommerce代码中定义实体(所有的核心实体类都在Nop.Core 中定义),再让 EF 生成数据库,这就是为什么会叫Code-First。你接下来可以用LINQ 来查询对象,它自己会把查询转换为SQL 语句并在数据库里执行。nopco
3、mmerce拥有牛 B 的 API 让你完全定制持久映射,你可以在这儿和这儿找到Code-First的资料。LibrariesNop.Services此项目包含一系列的核心服务,业务逻辑,验证,如果有数据的话还有数据的计算方法,也就是传说中的业务访问层(BAL)名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 19 页 -Plugins 文件夹中的那些项目Plugins 是 VS 的解决方案文件夹,硬盘中它是在你解决方案的根目录下。由于项目在编译时的输入路径是”.”,这样插件的DLL 会自动地放到文件夹中,用来放置已部署插件。这样也能让插件包含静态文件比如CSS或 JS,就不用在项
4、目之间拷贝这些文件了。PresentationNop.Admin Nop.Admin是一 MVC 项目,如果你还从没用过ASP.NET MVC,请猛击 这儿 有更多信息。可能你已经猜到这是表示层中的管理后台,你可以在PresentationNop.WebAdministration文件夹中找到它,此项目不能运行。PresentationNop.Web Nop.Web 也是一 MVC 项目,前台网店的表示层,这个才是你真正要跑起来的项目,它也是整个应用程序的起始项目。PresentationNop.Web.Framework Nop.Web.Framework是一个表示层的类库项目,包括可以让后
5、台和前台使用的一些共用的展示功能。TestNop.Core.Tests Nop.Core.Tests是 Nop.Core 的测试项目TestNop.Data.Tests Nop.Data.Tests是 Nop.Data 的测试项目TestNop.Services.Tests Nop.Services.Tests是 Nop.Services的测试项目TestNop.Tests Nop.Tests是一个类库,包含其它测试项目中要用的一共有类和辅助方法,此项目不包含任何测试用例名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 19 页 -扩展现有实体-添加新的属性Updating an
6、existing entity.How to add a new property.扩展现有实体:如何添加一个新的属性?This tutorial covers how to add a property to the Affiliate entity that ships with the nopCommerce source code.本教程将为代理商实体Affiliate entity添加一个属性,附带nopCom 源码。The data model数据模型Entities will have two classes that are used to map records to a t
7、able.The first class defines the properties,fields,and methods consumed by the web application.实体将有两个类用于映射记录表:第一个类定义affiliate 的属性、字段和方法。File System Location文件位置:Project RootLibrariesNop.CoreDomainAffiliatesAffiliate.csAssembly程序 集:Nop.CoreSolution Location解决方案中的位置:Nop.Core.Domain.Affiliates.Affilate
8、.csThe second class is used to map the properties defined in the class above to their respective SQL columns.The mapping class is also responsible for mapping relationships between different SQL tables.第二个类是将各属性分别映射到对应的SQL 列,以及映射不同的SQL 表之间的关系。File System Location:Project RootLibrariesNop.DataMapping
9、AffiliatesAffiliateMap.cs Assembly:Nop.Data Solution Location:Nop.Data.Mapping.Affiliates.AffiliateMap.csAdd the following property to the Affiliate class.1 为 Affiliate 添加一个属性:名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 19 页 -/Instance members must be virtual on data table objects like Affiliate.cs/Virtual is re
10、quired by data access frameworks so that these frameworks/can implement more complex features like lazy loading.public virtual string AffiliateWebSite get;set;Add the following code to the constructor of the AffiliateMap class.2为 AffiliateMap添加一个构造函数:/This code maps a column in the database to the n
11、ew property we created above/This creates a nullablenvarchar with a length of 255 characters in the/Affiliate SQL tablethis.Property(m=m.AffiliateWebSite).HasMaxLength(255).IsOptional();Because I m all about results,at this point I would run the code,re-install the database,and verify that the colum
12、n was created appropriately.3修改数据库,为Affiliate 表添加列:AffiliateWebSite,允许为空,navrchar(255)。4 重新编译程序The presentation model视图模型The presentation model is used to transport information from a controller to the view(read more at have another purpose;defining requirements.表示模型用于传输控制器的信息视图(参考 configured our da
13、tabase to only store 255 characters for the AffiliateWebSite.If we try and save an AffiliateWebSite with 300 characters the application will break(or truncate the text).We want the application to protect users from failures the best we can,and our view models help enforce requirements like string le
14、ngth.我们在数据库中设定AffiliateWebSite长度为 255 个字符,如果尝试保存300 个字符的,程序将中断(或截断文本)。因此需要通过程序强制用户输入不超过255 个字符,尽可能地降低出错。File System Location:Project RootPresentationNop.WebAdministrationModelsAffiliatesAffiliateModel.cs 名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 19 页 -Assembly:Nop.Admin Solution Location:Nop.Admin.Models.Affil
15、iates.AffiliateModel.csThe validator class is used to validate the data stored inside of the model class(e.g.required fields,max length,and required ranges).验证输入格式File System Location:Project RootPresentationNop.WebAdministrationValidatorsAffiliatesAffiliateValidator.cs Assembly:Nop.Admin Solution L
16、ocation:Nop.Admin.Validators.Affiliates.AffiliateValidator.csAdd the property to our view model.5 添加视图模型需要的属性:/The NopResourceDisplayName provides the key used during localization/Keep an eye out for more about localization in future blogsNopResourceDisplayName(Admin.Affiliates.Fields.AffiliateWebSi
17、te)public string AffiliateWebSite get;set;The requirements code will be added in the constructor of the validator./I think this code can speak for itselfRuleFor(m=m.AffiliateWebSite).Length(0,255);The viewFile System Location:Project RootPresentationNop.WebAdministrationViewsAffiliates _CreateOrUpda
18、te.cshtml Assembly:Nop.Admin Solution Location:Nop.Admin.Views.Affiliates._CreateOrUpdate.cshtmlViews contain the html for displaying model data.Place this html under the active section.6 在视图中添加一行:Html.NopLabelFor(model=model.AffiliateWebSite):名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 19 页 -Html.EditorFor(mode
19、l=model.AffiliateWebSite)Html.ValidationMessageFor(model=model.Active)The controllerIn this case the controller is responsible for mapping the domain data model to our view model and vice versa.The reason I choose the affiliate model to update is because of the simplicity.I want this to be an introd
20、uction to the nopCommerce platform and I would like to keep it as simple as possible.在这种情况下,控制器负责域数据模型映射到视图模型,反之亦然。这里之所以选择“代理商”模型来更新是因为他比较简单。以便尽可能简单地为大家介绍如何扩展现有实体属性。File System Location:Project RootPresentationNop.WebAdministrationControllerssAffiliateController.cs Assembly:Nop.Admin Solution Locati
21、on:Nop.Admin.Controllers.AffiliateController.csWere going to make three updates to the AffiliateController class.Data Model-View ModelCreate View Model-Data ModelEdit View Model-Data ModelNormally I would write tests for the following code and verify that model mapping is working correctly,but Ill s
22、kip unit testing to keep it simple.我们将要进行三次更新AffiliateController类。数据模型-视图模型创建视图模型-数据模型编辑视图模型-数据模型通常情况下,我会写下面的代码测试和验证模型的映射正常工作,但我会跳过单元测试,以保持它的简单。In the method PrepareAffiliateModel add the following code below the model.Active=affiliate.Active:名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 19 页 -7找到 Private void Pre
23、pareAffiliateModel方法,在 model.Active=affiliate.Active后中添加代码:/Data Model-Ceate/Edit View Modelmodel.AffiliateWebSite=affiliate.AffiliateWebSite;In the public ActionResultCreate(AffiliateModel model,boolcontinueEditing)method add the following code below affiliate.Active=model.Active:8找到 public ActionR
24、esult Create(AffiliateModel model,boolcontinueEditing)方法,在 affiliate.Active=model.Active后添加代码:/Create View Model-Data Modelaffiliate.AffiliateWebSite=model.AffiliateWebSite;A similar change is required in public ActionResultEdit(AffiliateModel model,boolcontinueEditing):9 最后,在 public ActionResult Ed
25、it(AffiliateModel model,boolcontinueEditing)方法中添加以下代码:/Edit View Model-Data Modelaffiliate.AffiliateWebSite=model.AffiliateWebSite;TroubleshootingRecreate the database.Either your own custom SQL script or use the nopCommerce installer.Stop the development web server between schema changes.Post a det
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年nopCommerce的源代码结构和架构说明 2022 nopCommerce 源代码 结构 架构 说明
限制150内