基于nopCommerce的開發(fā)框架 附源碼
.NET的開發(fā)人員應該都知道這個大名鼎鼎的高質(zhì)量b2c開源項目-nopCommerce,基于EntityFramework和MVC開發(fā),擁有透明且結(jié)構(gòu)良好的解決方案,同時結(jié)合了開源和商業(yè)軟件的最佳特性。官網(wǎng)地址:http://www.nopcommerce.com/,中文網(wǎng):http://www.nopcn.com/。下載后前后端展示如下。如果你還未了解過該項目,建議從官網(wǎng)下載代碼后在本地運行查看效果。
筆者使用該框架開發(fā)過不少項目,總的來說,方便簡潔,集成了.NET開發(fā)許多常用的組件和功能。一直想將它分享出來,但忙于工作而沒有達成,最近也是有時間來寫這篇文章,本文將展示如何提取該源碼的精簡框架并附上源碼(基于nopCommerce3.9版本)。如果你想了解框架結(jié)構(gòu),通過該框架來開發(fā)項目,那么看一遍該文章是有價值的。前排提示:本框架源碼已上傳到GitHub:https://github.com/dreling8/Nop.Framework,有興趣的可以關(guān)注該項目,后續(xù)會將其它的一些通用模塊添加進去,如用戶管理(IWorkContext 工作上下文)、插件功能、任務模塊(taskservice)、日志、緩存、本地化等。歡迎star給星星,你的支持是我的動力!
一、了解項目結(jié)構(gòu)
從項目結(jié)構(gòu)圖中我們也可以看出Nop的層次劃分非常清晰,先看我畫的層次圖
1. 展現(xiàn)層(Presentation)
也可稱之為應用層,只關(guān)注前端的整合,不涉及任何領域邏輯實現(xiàn)。這一層只做展現(xiàn),對我們框架來說是可有可無的,因此提取框架時會將該層刪除。
2. 業(yè)務服務層(Nop.Services)
整個系統(tǒng)的服務層,提供了對每個領域的接口和實現(xiàn)。這一層非常重要,提供了程序內(nèi)對展現(xiàn)層的接口服務,不論展現(xiàn)層使用mvc,還是使用winform,異或是給app調(diào)用的webapi接口,都需要該層服務。但該層的服務主要是電商的一些服務,對我們框架無用,因此在這個框架中會刪除所有服務,只添加一個測試服務類和接口,應用到項目中你應該在該層添加接口和服務。
3. 數(shù)據(jù)層(Nop.Data)
nop在數(shù)據(jù)層的倉儲實現(xiàn)中使用了ef和sqlserver數(shù)據(jù)庫,如果你想擴展,也可以在該層使用其它的ORM映射庫和數(shù)據(jù)庫。這一層的大部分功能我們會在框架中將保留。
4. 基礎設施層(Nop.Core)
包括緩存的實現(xiàn)、配置、領域模型等等。在框架中會保留一部分功能,并將Domain領域模型移出該層做單獨項目,為什么要這樣做,因為通常情況下,Domain層的調(diào)整會比較多,所以我一般將Domain做單獨Project,當然你也可以不調(diào)整,但框架做了該調(diào)整。
二、刪除與業(yè)務相關(guān)的代碼
我們已經(jīng)對Nop的整個代碼層次結(jié)構(gòu)有了了解,基于以下兩點開始修改項目源碼:1.框架足夠精簡,沒有任何電商業(yè)務。2.核心功能保留。建議在開始前先copy一份源碼保留。
1. Test項目:Tests文件夾下面是測試項目,不是必需的,將它全部移除,開發(fā)具體業(yè)務,可以再單獨添加測試項目。由于是測試項目,刪除后整個項目還能跑起來。
2. Presentation展現(xiàn)層:這里的三個項目,分別是前臺,后端和兩個項目共用的一些模塊。和測試項目一樣,這里我們也全部移除。
3. Plugin項目:插件項目,同1、2一樣,插件也不是必需的,移除所有的插件項目?,F(xiàn)在只剩下三個項目了(歡迎關(guān)注該項目的github,后續(xù)我會專門寫篇文章介紹如何添加插件)。
Nop.Services:業(yè)務服務層,這一層是程序集內(nèi)對外接口層,需要保留。刪除所有相關(guān)的業(yè)務服務類,其中日志、幫助、任務等跟系統(tǒng)相關(guān)的都刪除,目的是更好的展示整個系統(tǒng)的結(jié)構(gòu)。添加一個測試類,暫時什么都不寫。
Nop.Data:數(shù)據(jù)層項目。這層基本不做調(diào)整,只刪除EF的Mapping映射相關(guān)類。
Nop.Core:基礎設施層。刪除電商業(yè)務相關(guān)的Domain,新建項目Nop.Domain。
報錯了,IWorkContext(工作上下文,用于獲取用戶信息等數(shù)據(jù))依賴Domain,刪除它。這個過程可能要刪除不少文件,直到項目不再報錯。完成后我們的項目結(jié)構(gòu)如下,注意我們將Nop.Core中的實體基類移到了Nop.Domain中,到這一步,我們的基礎框架結(jié)構(gòu)已經(jīng)大致出來了。
三、添加數(shù)據(jù)庫、數(shù)據(jù)實體、映射、業(yè)務層代碼
1. 在本地Sqlserver中,新建數(shù)據(jù)庫MyProject,添加表Test。
USE [MyProject] GO /****** Object: Table [dbo].[Test] Script Date: 05/24/2017 23:51:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Test]( [Id] [int] NOT NULL, [Name] [nvarchar](50) NOT NULL, [Description] [nvarchar](200) NULL, [CreateDate] [datetime] NULL, CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
2. 添加實體類和映射。在Domain項目下面新建Test目錄,添加TestEntity。Data項目Mapping下新建Test目錄,添加EF映射類。
public class TestEntity: BaseEntity { public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual DateTime? CreateDate { get; set; } }
3. 添加業(yè)務層方法。
在Nop.Services項目里,在我們之前添加的接口和類下面添加幾個常用的CURD方法,并實現(xiàn)它。這樣我們就已經(jīng)實現(xiàn)的業(yè)務層的代碼了。
/// <summary> /// Test service interface /// </summary> public partial interface ITestService { /// <summary> /// Gets all tests /// </summary> /// <returns>Tests</returns> IList<TestEntity> GetAllTests(); /// <summary> /// Gets a test /// </summary> /// <param name="testId">The test identifier</param> /// <returns>Test</returns> TestEntity GetTestById(int testId); /// <summary> /// Inserts a test /// </summary> /// <param name="test">Test</param> void InsertTest(TestEntity test); /// <summary> /// Updates the test /// </summary> /// <param name="test">Test</param> void UpdateTest(TestEntity test); /// <summary> /// Deletes a test /// </summary> /// <param name="test">Test</param> void DeleteTest(TestEntity test); }
/// <summary> /// Test service /// </summary> public partial class TestService : ITestService { #region Constants #endregion #region Fields private readonly IRepository<TestEntity> _testRepository; #endregion #region Ctor public TestService(IRepository<TestEntity> testRepository) { this._testRepository = testRepository; } #endregion #region Methods /// <summary> /// Gets all tests /// </summary> /// <returns>Tests</returns> public virtual IList<TestEntity> GetAllTests() { return _testRepository.Table.Where(p => p.Name != null).ToList(); } /// <summary> /// Gets a topic /// </summary> /// <param name="testId">The test identifier</param> /// <returns>Test</returns> public virtual TestEntity GetTestById(int testId) { if (testId == 0) return null; return _testRepository.GetById(testId); } /// <summary> /// Inserts a test /// </summary> /// <param name="test">Test</param> public virtual void InsertTest(TestEntity test) { if (test == null) throw new ArgumentNullException("test"); _testRepository.Insert(test); } /// <summary> /// Updates the test /// </summary> /// <param name="test">Test</param> public virtual void UpdateTest(TestEntity test) { if (test == null) throw new ArgumentNullException("test"); _testRepository.Update(test); } /// <summary> /// Deletes a test /// </summary> /// <param name="test">Test</param> public virtual void DeleteTest(TestEntity test) { if (test == null) throw new ArgumentNullException("test"); _testRepository.Delete(test); } #endregion }
四、添加Presentation項目
有了業(yè)務服務,現(xiàn)在可以添加表現(xiàn)層項目來測試了。為什么不直接寫測試項目?因為測試項目使用Mock模擬數(shù)據(jù),不能完整展示整個功能。
1. 添加mvc模板項目,通過nuget引入Autofac和Autofac.Mvc5。
2. 添加容器注冊類DependencyRegistrar,實現(xiàn)IDependencyRegistrar接口,這一步非常關(guān)鍵,我們將要用的接口和實現(xiàn)類注入到容器中。
/// <summary> /// Dependency registrar /// </summary> public class DependencyRegistrar : IDependencyRegistrar { /// <summary> /// Register services and interfaces /// </summary> /// <param name="builder">Container builder</param> /// <param name="typeFinder">Type finder</param> /// <param name="config">Config</param> public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config) { //注入ObjectContext builder.Register<IDbContext>(c => new NopObjectContext("test")).InstancePerLifetimeScope(); // 注入ef到倉儲 builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); // 注入Service及接口 builder.RegisterAssemblyTypes(typeof(TestService).Assembly) .AsImplementedInterfaces() .InstancePerLifetimeScope(); //注入controllers builder.RegisterControllers(typeFinder.GetAssemblies().ToArray()); } /// <summary> /// Order of this dependency registrar implementation /// </summary> public int Order { get { return 2; } } }
3. 配置文件中添加數(shù)據(jù)庫訪問節(jié)點
4. 應用啟動時添加初始化引擎上下文
啟動項目,這時NopEngine會報錯,因為我們沒有使用Nopconfig來配置項目,在RegisterDependencies方法中注釋NopConfig的注入,同時在Initialize過程中將相關(guān)代碼注釋。這樣就完成通過Autofac注入類到容器中。
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //引擎上下文初始化 EngineContext.Initialize(false); } }
//RegisterDependencies方法中注釋NopConfig的注入 //builder.RegisterInstance(config).As<NopConfig>().SingleInstance(); public void Initialize(NopConfig config) { //register dependencies RegisterDependencies(config); //沒有使用config,暫時注釋 //register mapper configurations //RegisterMapperConfiguration(config); //startup tasks 沒有啟用任務,注釋 //if (!config.IgnoreStartupTasks) //{ // RunStartupTasks(); //} }
5. 在controller添加測試代碼。將service添加到HomeController,在構(gòu)造函數(shù)中初始化。系統(tǒng)啟動后會自動注入實例。通過斷點我們看到,數(shù)據(jù)成功添加到了數(shù)據(jù)庫。
public class HomeController : Controller { public ITestService _testService; public HomeController( ITestService testService ) { _testService = testService; } public ActionResult Index() { var entity = new TestEntity() { CreateDate = DateTime.Now, Description = "描述2", Name = "測試數(shù)據(jù)2" }; _testService.InsertTest(entity); var tests = _testService.GetAllTests(); return View(); }
五、擴展到Webapi、Winform、WPF
現(xiàn)在再添加一個winform項目,同樣的步驟添加相關(guān)的代碼。在Winform中我們也能使用業(yè)務的服務了。
1. 通過Nuget安裝autofac,entityframework, 添加項目Libraries下的引用。
2. 添加依賴注冊類,因為是winform項目,DependencyRegistrar這里需要做些調(diào)整,建議定義一個空接口IRegistrarForm,需要注入的Form實現(xiàn)IRegistrarForm。
/// <summary> /// Dependency registrar /// </summary> public class DependencyRegistrar : IDependencyRegistrar { /// <summary> /// Register services and interfaces /// </summary> /// <param name="builder">Container builder</param> /// <param name="typeFinder">Type finder</param> /// <param name="config">Config</param> public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config) { //注入ObjectContext builder.Register<IDbContext>(c => new NopObjectContext("test")).InstancePerLifetimeScope(); // 注入ef到倉儲 builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); // 注入Service及接口 builder.RegisterAssemblyTypes(typeof(TestService).Assembly) .AsImplementedInterfaces() .InstancePerLifetimeScope(); //注入controllers //builder.RegisterControllers(typeFinder.GetAssemblies().ToArray()); //注入forms var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IRegistrarForm)))) .ToArray(); foreach (var formtype in types) { builder.RegisterAssemblyTypes(formtype.Assembly); } } /// <summary> /// Order of this dependency registrar implementation /// </summary> public int Order { get { return 2; } } }
3. 在啟動時添加 EngineContext.Initialize(false),啟動項目,報錯了,因為winform不能執(zhí)行,對方法做些調(diào)整,添加一個參數(shù)isForm表示是否是winform,默認為false。
/// <summary> /// Initializes a static instance of the Nop factory. /// </summary> /// <param name="forceRecreate">Creates a new factory instance even though the factory has been previously initialized.</param> /// <param name="isWinForm">是否客戶端程序</param> [MethodImpl(MethodImplOptions.Synchronized)] public static IEngine Initialize(bool forceRecreate,bool isWinForm = false) { if (Singleton<IEngine>.Instance == null || forceRecreate) { Singleton<IEngine>.Instance = new NopEngine(); NopConfig config = null; if (!isWinForm) { config = ConfigurationManager.GetSection("NopConfig") as NopConfig; } else { //如果使用winform,使用此代碼讀取配置初始化NopConfig var appSettings = ConfigurationManager.AppSettings; foreach (var key in appSettings.AllKeys) { } } Singleton<IEngine>.Instance.Initialize(config); } return Singleton<IEngine>.Instance; }
static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); //引擎上下文初始化 EngineContext.Initialize(false, true); Application.Run(EngineContext.Current.Resolve<Form1>()); } }
4. From1中測試,成功調(diào)用了業(yè)務層的方法,這里我們并沒有實例化ITestService,而是交給依賴注入自動實現(xiàn)。
public partial class Form1 : Form, IRegistrarForm { private ITestService _testService; public Form1( ITestService testService ) { InitializeComponent(); _testService = testService; //如果不注入form可以使用EngineContext.Current.Resolve<ITestService>(); 得到實例 } private void button1_Click(object sender, EventArgs e) { var tests = _testService.GetAllTests(); } }
至此,基于Nop的精簡開發(fā)框架基本完成,如果你有興趣,建議在github關(guān)注該項目 :https://github.com/dreling8/Nop.Framework,歡迎star給星星,你的支持是我的動力!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- NopCommerce架構(gòu)分析之(八)多語言支持
- NopCommerce架構(gòu)分析之(七)主題Theme皮膚管理器
- NopCommerce架構(gòu)分析之(六)自定義RazorViewEngine和WebViewPage
- NopCommerce架構(gòu)分析之(五)Model綁定Action參數(shù)
- NopCommerce架構(gòu)分析之(四)基于路由實現(xiàn)靈活的插件機制
- NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作
- NopCommerce架構(gòu)分析(一)Autofac依賴注入類生成容器
- 使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
相關(guān)文章
Asp.net core中實現(xiàn)自動更新的Option的方法示例
這篇文章主要介紹了Asp.net core中實現(xiàn)自動更新的Option的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03HttpWebRequest的常見錯誤使用TcpClient可避免
有時使用HttpWebRequest對象會出現(xiàn)錯誤有三種服務器提交了協(xié)議沖突/基礎連接已經(jīng)關(guān)閉:連接被意外關(guān)閉/無法發(fā)送具有此謂詞類型的內(nèi)容正文,感興趣的朋友可以參考下本文2013-02-02ASP.NET Core對不同類型的用戶進行區(qū)別限流詳解
這篇文章主要介紹了ASP.NET Core對不同類型的用戶進行區(qū)別限流的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
這幾天在看到一個網(wǎng)站的注冊的時候,就只允許輸入規(guī)定的字符和數(shù)字。我就好奇的寫了一個校驗的代碼。呵呵 不知道對大家有沒有用。如果有用的話可以保存。沒有用就當是看看以下了。2010-10-10基于Fiddler實現(xiàn)修改接口返回數(shù)據(jù)進行測試
這篇文章主要介紹了基于Fiddler實現(xiàn)修改接口返回數(shù)據(jù)進行測試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08