nopCommerce的源代码结构和架构说明.docx

上传人:b****2 文档编号:12885264 上传时间:2023-04-22 格式:DOCX 页数:22 大小:131.21KB
下载 相关 举报
nopCommerce的源代码结构和架构说明.docx_第1页
第1页 / 共22页
nopCommerce的源代码结构和架构说明.docx_第2页
第2页 / 共22页
nopCommerce的源代码结构和架构说明.docx_第3页
第3页 / 共22页
nopCommerce的源代码结构和架构说明.docx_第4页
第4页 / 共22页
nopCommerce的源代码结构和架构说明.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

nopCommerce的源代码结构和架构说明.docx

《nopCommerce的源代码结构和架构说明.docx》由会员分享,可在线阅读,更多相关《nopCommerce的源代码结构和架构说明.docx(22页珍藏版)》请在冰豆网上搜索。

nopCommerce的源代码结构和架构说明.docx

nopCommerce的源代码结构和架构说明

nopCommerce的源代码结构和架构

编写本文档是为了向程序员说明nopcommerce的解决方案结构,亦是程序员开发nopcommerce的居家必备良书。

首先nopcommerce的源代码很容易拿到,它是开源的,所以你可以直接到网上下载。

在你打开VS以后项目和文件夹都会完整列出来,我们建议你在看此文档的同时也打开你的VS来浏览项目和文件。

绝大多数的项目,目录和文件都顾名思义,你可以从名字就大概知道是做什么的。

比如Nop.Plugin.Payments.PayPalStandard这个我都不用看项目代码就能猜到做什么的。

\Libraries\Nop.Core

Nop.Core项目包含nopcommerce的一系列核心类如缓存,事件,辅助类和业务对象(如订单和客户实体类)

\Libraries\Nop.Data

Nop.Data项目包含一系列的数据访问类和方法以从数据库或其他数据媒介读取和保存数据。

它也有助于把数据访问逻辑和你的业务对象分离。

nopcommerce使用EntityFramework(EF)Code-First方法,允许你在nopcommerce代码中定义实体(所有的核心实体类都在Nop.Core中定义),再让EF生成数据库,这就是为什么会叫Code-First。

你接下来可以用LINQ来查询对象,它自己会把查询转换为SQL语句并在数据库里执行。

nopcommerce拥有牛B的API让你完全定制持久映射,你可以在这儿和这儿找到Code-First的资料。

\Libraries\Nop.Services

此项目包含一系列的核心服务,业务逻辑,验证,如果有数据的话还有数据的计算方法,也就是传说中的业务访问层(BAL)

\Plugins\文件夹中的那些项目

Plugins是VS的解决方案文件夹,硬盘中它是在你解决方案的根目录下。

由于项目在编译时的输入路径是”..\..\Presentation\Nop.Web\Plugins\{Group}.{Name}\”,这样插件的DLL会自动地放到\Presentation\Nop.Web\Plugins\文件夹中,用来放置已部署插件。

这样也能让插件包含静态文件比如CSS或JS,就不用在项目之间拷贝这些文件了。

\Presentation\Nop.Admin

Nop.Admin是一MVC项目,如果你还从没用过ASP.NETMVC,请猛击这儿有更多信息。

可能你已经猜到这是表示层中的管理后台,你可以在\Presentation\Nop.Web\Administration文件夹中找到它,此项目不能运行。

\Presentation\Nop.Web

Nop.Web也是一MVC项目,前台网店的表示层,这个才是你真正要跑起来的项目,它也是整个应用程序的起始项目。

\Presentation\Nop.Web.Framework

Nop.Web.Framework是一个表示层的类库项目,包括可以让后台和前台使用的一些共用的展示功能。

\Test\Nop.Core.Tests

Nop.Core.Tests是Nop.Core的测试项目

\Test\Nop.Data.Tests

Nop.Data.Tests是Nop.Data的测试项目

\Test\Nop.Services.Tests

Nop.Services.Tests是Nop.Services的测试项目

\Test\Nop.Tests

Nop.Tests是一个类库,包含其它测试项目中要用的一共有类和辅助方法,此项目不包含任何测试用例

扩展现有实体-添加新的属性

Updatinganexistingentity.Howtoaddanewproperty.

扩展现有实体:

如何添加一个新的属性?

ThistutorialcovershowtoaddapropertytotheAffiliateentitythatshipswiththenopCommercesourcecode.

本教程将为代理商实体Affiliateentity添加一个属性,附带nopCom源码。

Thedatamodel数据模型

Entitieswillhavetwoclassesthatareusedtomaprecordstoatable.Thefirstclassdefinestheproperties,fields,andmethodsconsumedbythewebapplication.

实体将有两个类用于映射记录表:

第一个类定义affiliate的属性、字段和方法。

FileSystemLocation文件位置:

[ProjectRoot]\Libraries\Nop.Core\Domain\Affiliates\Affiliate.cs

Assembly程序集:

Nop.Core

SolutionLocation解决方案中的位置:

Nop.Core.Domain.Affiliates.Affilate.cs

ThesecondclassisusedtomapthepropertiesdefinedintheclassabovetotheirrespectiveSQLcolumns.ThemappingclassisalsoresponsibleformappingrelationshipsbetweendifferentSQLtables.

第二个类是将各属性分别映射到对应的SQL列,以及映射不同的SQL表之间的关系。

FileSystemLocation:

[ProjectRoot]\Libraries\Nop.Data\Mapping\Affiliates\AffiliateMap.cs

Assembly:

Nop.Data

SolutionLocation:

Nop.Data.Mapping.Affiliates.AffiliateMap.cs

AddthefollowingpropertytotheAffiliateclass.

为Affiliate添加一个属性:

//InstancemembersmustbevirtualondatatableobjectslikeAffiliate.cs

//Virtualisrequiredbydataaccessframeworkssothattheseframeworks

//canimplementmorecomplexfeatureslikelazyloading.

publicvirtualstringAffiliateWebSite{get;set;}

 

AddthefollowingcodetotheconstructoroftheAffiliateMapclass.

为AffiliateMap添加一个构造函数:

//Thiscodemapsacolumninthedatabasetothenewpropertywecreatedabove

//Thiscreatesanullablenvarcharwithalengthof255charactersinthe

//AffiliateSQLtable

this.Property(m=>m.AffiliateWebSite).HasMaxLength(255).IsOptional();     

BecauseI’mallaboutresults,atthispointIwouldrunthecode,re-installthedatabase,andverifythatthecolumnwascreatedappropriately.

修改数据库,为Affiliate表添加列:

AffiliateWebSite,允许为空,navrchar(255)。

重新编译程序

Thepresentationmodel视图模型

Thepresentationmodelisusedtotransportinformationfromacontrollertotheview(readmoreatModelshaveanotherpurpose;definingrequirements.

表示模型用于传输控制器的信息视图(参考

Weconfiguredourdatabasetoonlystore255charactersfortheAffiliateWebSite.IfwetryandsaveanAffiliateWebSitewith300characterstheapplicationwillbreak(ortruncatethetext).Wewanttheapplicationtoprotectusersfromfailuresthebestwecan,andourviewmodelshelpenforcerequirementslikestringlength.

我们在数据库中设定AffiliateWebSite长度为255个字符,如果尝试保存300个字符的,程序将中断(或截断文本)。

因此需要通过程序强制用户输入不超过255个字符,尽可能地降低出错。

FileSystemLocation:

[ProjectRoot]\Presentation\Nop.Web\Administration\Models\Affiliates\AffiliateModel.cs

Assembly:

Nop.Admin

SolutionLocation:

Nop.Admin.Models.Affiliates.AffiliateModel.cs

Thevalidatorclassisusedtovalidatethedatastoredinsideofthemodelclass(e.g.requiredfields,maxlength,andrequiredranges).

验证输入格式

FileSystemLocation:

[ProjectRoot]\Presentation\Nop.Web\Administration\Validators\Affiliates\AffiliateValidator.cs

Assembly:

Nop.Admin

SolutionLocation:

Nop.Admin.Validators.Affiliates.AffiliateValidator.cs

Addthepropertytoourviewmodel.

添加视图模型需要的属性:

//TheNopResourceDisplayNameprovidesthe"key"usedduringlocalization

//Keepaneyeoutformoreaboutlocalizationinfutureblogs

[NopResourceDisplayName("Admin.Affiliates.Fields.AffiliateWebSite")]

publicstringAffiliateWebSite{get;set;}

Therequirementscodewillbeaddedintheconstructorofthevalidator.

//Ithinkthiscodecanspeakforitself

RuleFor(m=>m.AffiliateWebSite).Length(0,255);

Theview

FileSystemLocation:

[ProjectRoot]\Presentation\Nop.Web\Administration\Views\Affiliates\_CreateOrUpdate.cshtml

Assembly:

Nop.Admin

SolutionLocation:

Nop.Admin.Views.Affiliates._CreateOrUpdate.cshtml

Viewscontainthehtmlfordisplayingmodeldata.Placethishtmlunderthe"active"section.

在视图中添加一行:

   

       @Html.NopLabelFor(model=>model.AffiliateWebSite):

   

   

       @Html.EditorFor(model=>model.AffiliateWebSite)

       @Html.ValidationMessageFor(model=>model.Active)

   

Thecontroller

Inthiscasethecontrollerisresponsibleformappingthedomaindatamodeltoourviewmodelandviceversa.ThereasonIchoosetheaffiliatemodeltoupdateisbecauseofthesimplicity.IwantthistobeanintroductiontothenopCommerceplatformandIwouldliketokeepitassimpleaspossible.

在这种情况下,控制器负责域数据模型映射到视图模型,反之亦然。

这里之所以选择“代理商”模型来更新是因为他比较简单。

以便尽可能简单地为大家介绍如何扩展现有实体属性。

FileSystemLocation:

[ProjectRoot]\Presentation\Nop.Web\Administration\Controllerss\AffiliateController.cs

Assembly:

Nop.Admin

SolutionLocation:

Nop.Admin.Controllers.AffiliateController.cs

We'regoingtomakethreeupdatestotheAffiliateControllerclass.

∙DataModel->ViewModel

∙CreateViewModel->DataModel

∙EditViewModel->DataModel

NormallyIwouldwritetestsforthefollowingcodeandverifythatmodelmappingisworkingcorrectly,butI'llskipunittestingtokeepitsimple.

我们将要进行三次更新AffiliateController类。

数据模型-视图模型

创建视图模型->数据模型

编辑视图模型->数据模型

通常情况下,我会写下面的代码测试和验证模型的映射正常工作,但我会跳过单元测试,以保持它的简单。

InthemethodPrepareAffiliateModeladdthefollowingcodebelowthemodel.Active=affiliate.Active:

找到PrivatevoidPrepareAffiliateModel方法,在model.Active=affiliate.Active后中添加代码:

 

//DataModel->Ceate/EditViewModel

model.AffiliateWebSite=affiliate.AffiliateWebSite;

 

InthepublicActionResultCreate(AffiliateModelmodel,boolcontinueEditing)methodaddthefollowingcodebelowaffiliate.Active=model.Active:

找到publicActionResultCreate(AffiliateModelmodel,boolcontinueEditing)方法,在affiliate.Active=model.Active后添加代码:

//CreateViewModel->DataModel

affiliate.AffiliateWebSite=model.AffiliateWebSite;

AsimilarchangeisrequiredinpublicActionResultEdit(AffiliateModelmodel,boolcontinueEditing):

最后,在publicActionResultEdit(AffiliateModelmodel,boolcontinueEditing)方法中添加以下代码:

//EditViewModel->DataModel

affiliate.AffiliateWebSite=model.AffiliateWebSite;

Troubleshooting

∙Recreatethedatabase.EitheryourowncustomSQLscriptorusethenopCommerceinstaller.

∙Stopthedevelopmentwebserverbetweenschemachanges.

∙Postadetailedcommentonourforums.

如何编写nopCommerce插件

插件(Plug-in,又叫addin、add-in、addon或add-on)是一种电脑程序,通过和应用程序的互动,用来替应用程序增加一些所需要的特定的功能。

(Wikipedia)

插件用来扩展nopCommerce的功能,nopcommcer有多种类型的插件。

比如支付方式中的paypal,税率供应商,配送计算方式(UPS,USP,Fedex),小部件(livechat功能)等等。

nopCommerce本身也自带了很多不同的插件。

你可以在官网上搜索是否已经有人上传了满足你需要的插件。

如果没有,哥这就手把手带你编写一个出来。

插件结构,所用文件,所在位置

1.你第一件事就是要在解决方案中新建一个“类库”项目。

最好的办法是把插件都放在解决方案根目录(不过小心不要和Nop.Web下边的plugins目录搞混了,那儿是放已布置插件的),而且最好把插件也都放在解决方案目录的plugin目录中(关于更多解决方案文件夹的信息,请猛击此处)

最好以这种方法来命名:

”Nop.Plugin.{Group}.{Name}”。

{Group}是你插件的分类(比如支付),{Name}是你的插件名(比如”AuthorizeNet”),那么Authorize.NET的支付插件就会有这样的名字:

Nop.Plugin.Payments.AuthorizeNet。

2.一旦建立了插件项目,把输入路径改为”..\..\Presentation\Nop.Web\Plugins\{Group}.{Name}\”,比如Authorize.NET支付插件就会有这样的输入路径:

“..\..\Presentation\Nop.Web\Plugins\Payments.AuthorizeNet\”。

搞定以后,对应的插件DLL就会被拷贝到\Presentation\Nop.Web\Plugins\文件夹,nopCommerce内核会搜索此文件夹。

a.在项目菜单,点击属性

b.选择生成选项卡

c.点击输入路径旁边的浏览按钮选择一个输入目录

你要在debug和release模式下都要做此步骤。

3.下一步你就要为你的每一个插件建立一个Description.txt,此文件包含描述插件的信息。

你可以从其它插件目录中拷出来。

比如Authorize.NET支付插件的Description.txt就有如下内容:

Group:

Paymentmethods

FriendlyName:

CreditCard

SystemName:

Payments.AuthorizeNet

Version:

1.00

SupportedVersions:

2.30

Author:

nopCommerceteam

DisplayOrder:

1

FileName:

Nop.Plugin.Payments.AuthorizeNet.dll

其实所有的信息你都能看懂,不过有一些注意事项。

SystemName必须唯一。

Version字段是你插件的版本号,你可以将它设置为你喜欢的任何值。

SupportedVersions可以包含一个由逗号分隔的(确保nopCommerce当前版本包含在此列表中,否则此插件没戏)支持版本清单。

FileName是用这个格式:

Nop.Plugin.{Group}.{Name}.dll(是你插件的assembly文件名)。

要确保此文件的“拷贝到输入目录”属性是“Copyifnewer”

4.所需的最后一个步骤是创建一个类实现IPlugin接口(Nop.Core.Plugins命名空间)。

nopCommerce有BasePlugin类已经实现了一些IPlugin方法,这样你就不用苦逼地再写一遍。

nopCommerce还提供一些从IPlugin派生特定的接口。

例如,俺们有“IPaymentMethod”接口,用于创建新的付款插件,它包含了一些特定的用于付款的方法如ProcessPayment()或GetAdditionalHandlingFee()。

nopCommerce目前有以下特定的插件接口:

IExternalAuthenticationMethod.用来建立外部认证方法如Facebook,Twitter,OpenID,etc.

IWidgetPlugin.让你可以创建小部件,小部件在你网站的某些地方出现,如左边的Livechat框

IExchangeRateProvider.用于获得货币汇率.

IDiscountRequirementRule.允许你创建新的折扣规则比如”帐单寄到的国家必须是……“

ISMSP

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1