干货——基于Nop的精简版开发框架(附源码)
.NET的开发人员应该都知道这个大名鼎鼎的高质量b2c开源项目-nopCommerce,基于EntityFramework和MVC开发,拥有透明且结构良好的解决方案,同时结合了开源和商业软件的最佳特性。官网地址:http://www.nopcommerce.com/,中文网:http://www.nopcn.com/。下载后前后端展示如下。如果你还未了解过该项目,建议从官网下载代码后在本地运行查看效果。
笔者使用该框架开发过不少项目,总的来说,方便简洁,集成了.NET开发许多常用的组件和功能。一直想将它分享出来,但忙于工作而没有达成,最近也是有时间来写这篇文章,本文将展示如何提取该源码的精简框架并附上源码(基于nopCommerce3.9版本)。如果你想了解框架结构,通过该框架来开发项目,那么看一遍该文章是有价值的。前排提示:本框架源码已上传到GitHub:https://github.com/dreling8/Nop.Framework,有兴趣的可以关注该项目,后续会将其它的一些通用模块添加进去,如用户管理(IWorkContext 工作上下文)、插件功能、任务模块(taskservice)、日志、缓存、本地化等。欢迎star给星星,你的支持是我的动力!


一、了解项目结构
从项目结构图中我们也可以看出Nop的层次划分非常清晰,先看我画的层次图


1. 展现层(Presentation)
也可称之为应用层,只关注前端的整合,不涉及任何领域逻辑实现。这一层只做展现,对我们框架来说是可有可无的,因此提取框架时会将该层删除。
2. 业务服务层(Nop.Services)
整个系统的服务层,提供了对每个领域的接口和实现。这一层非常重要,提供了程序内对展现层的接口服务,不论展现层使用mvc,还是使用winform,异或是给app调用的webapi接口,都需要该层服务。但该层的服务主要是电商的一些服务,对我们框架无用,因此在这个框架中会删除所有服务,只添加一个测试服务类和接口,应用到项目中你应该在该层添加接口和服务。
3. 数据层(Nop.Data)
nop在数据层的仓储实现中使用了ef和sqlserver数据库,如果你想扩展,也可以在该层使用其它的ORM映射库和数据库。这一层的大部分功能我们会在框架中将保留。
4. 基础设施层(Nop.Core)
包括缓存的实现、配置、领域模型等等。在框架中会保留一部分功能,并将Domain领域模型移出该层做单独项目,为什么要这样做,因为通常情况下,Domain层的调整会比较多,所以我一般将Domain做单独Project,当然你也可以不调整,但框架做了该调整。
二、删除与业务相关的代码
我们已经对Nop的整个代码层次结构有了了解,基于以下两点开始修改项目源码:1.框架足够精简,没有任何电商业务。2.核心功能保留。建议在开始前先copy一份源码保留。
1. Test项目:Tests文件夹下面是测试项目,不是必需的,将它全部移除,开发具体业务,可以再单独添加测试项目。由于是测试项目,删除后整个项目还能跑起来。

2. Presentation展现层:这里的三个项目,分别是前台,后端和两个项目共用的一些模块。和测试项目一样,这里我们也全部移除。

3. Plugin项目:插件项目,同1、2一样,插件也不是必需的,移除所有的插件项目。现在只剩下三个项目了(欢迎关注该项目的github,后续我会专门写篇文章介绍如何添加插件)。

Nop.Services:业务服务层,这一层是程序集内对外接口层,需要保留。删除所有相关的业务服务类,其中日志、帮助、任务等跟系统相关的都删除,目的是更好的展示整个系统的结构。添加一个测试类,暂时什么都不写。

Nop.Data:数据层项目。这层基本不做调整,只删除EF的Mapping映射相关类。
Nop.Core:基础设施层。删除电商业务相关的Domain,新建项目Nop.Domain。
报错了,IWorkContext(工作上下文,用于获取用户信息等数据)依赖Domain,删除它。这个过程可能要删除不少文件,直到项目不再报错。完成后我们的项目结构如下,注意我们将Nop.Core中的实体基类移到了Nop.Domain中,到这一步,我们的基础框架结构已经大致出来了。

三、添加数据库、数据实体、映射、业务层代码
1. 在本地Sqlserver中,新建数据库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; }
}
public class TestEntityMap : NopEntityTypeConfiguration<TestEntity>
{
public TestEntityMap()
{
this.ToTable("TestTable");
this.HasKey(t => t.Id);
this.Property(t => t.Name).IsRequired().HasMaxLength();
}
}
3. 添加业务层方法。
在Nop.Services项目里,在我们之前添加的接口和类下面添加几个常用的CURD方法,并实现它。这样我们就已经实现的业务层的代码了。
/// <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 == )
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项目
有了业务服务,现在可以添加表现层项目来测试了。为什么不直接写测试项目?因为测试项目使用Mock模拟数据,不能完整展示整个功能。
1. 添加mvc模板项目,通过nuget引入Autofac和Autofac.Mvc5。
2. 添加容器注册类DependencyRegistrar,实现IDependencyRegistrar接口,这一步非常关键,我们将要用的接口和实现类注入到容器中。
/// <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 ; }
}
}
3. 配置文件中添加数据库访问节点
<add name="test" connectionString="Data Source=.;Initial Catalog=MyProject;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=sa1234" providerName="System.Data.SqlClient" />
4. 应用启动时添加初始化引擎上下文
启动项目,这时NopEngine会报错,因为我们没有使用Nopconfig来配置项目,在RegisterDependencies方法中注释NopConfig的注入,同时在Initialize过程中将相关代码注释。这样就完成通过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,在构造函数中初始化。系统启动后会自动注入实例。通过断点我们看到,数据成功添加到了数据库。
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 = "测试数据2"
};
_testService.InsertTest(entity);
var tests = _testService.GetAllTests();
return View();
}
五、扩展到Webapi、Winform、WPF

现在再添加一个winform项目,同样的步骤添加相关的代码。在Winform中我们也能使用业务的服务了。
1. 通过Nuget安装autofac,entityframework, 添加项目Libraries下的引用。
2. 添加依赖注册类,因为是winform项目,DependencyRegistrar这里需要做些调整,建议定义一个空接口IRegistrarForm,需要注入的Form实现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 ; }
}
}
3. 在启动时添加 EngineContext.Initialize(false),启动项目,报错了,因为winform不能执行,对方法做些调整,添加一个参数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中测试,成功调用了业务层的方法,这里我们并没有实例化ITestService,而是交给依赖注入自动实现。
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的精简开发框架基本完成,如果你有兴趣,建议在github关注该项目 :https://github.com/dreling8/Nop.Framework,欢迎star给星星,你的支持是我的动力!
干货——基于Nop的精简版开发框架(附源码)的更多相关文章
- (干货)基于 veImageX 搭建海报生成平台 -- 附源码
前言 618 年中促销即将来临,很多公司都会通过海报来宣传自己的促销方案,通常情况下海报由设计团队基于 PS.Sketch 等工具创作,后期若想替换海报文案.商品列表等内容则需打开原工程进行二次创作, ...
- 基于Redis缓存的Session共享(附源码)
基于Redis缓存的Session共享(附源码) 在上一篇文章中我们研究了Redis的安装及一些基本的缓存操作,今天我们就利用Redis缓存实现一个Session共享,基于.NET平台的Seesion ...
- 手机端页面自适应解决方案—rem布局(进阶版,附源码示例)
转自:https://segmentfault.com/a/1190000007350680 一年前笔者写了一篇 <手机端页面自适应解决方案—rem布局>,意外受到很多朋友的关注和喜欢.但 ...
- 基于jQuery左右滑动切换特效 附源码
分享一款基于脚jQuery左右滑动切换特效.这是一款鼠标点击左右箭头按钮图片滚动切换,鼠标移到图片上显示透明边框特效. 效果图如下: 废话不多说,代码奉上! html代码: <div ...
- 一文详解如何用 TensorFlow 实现基于 LSTM 的文本分类(附源码)
雷锋网按:本文作者陆池,原文载于作者个人博客,雷锋网已获授权. 引言 学习一段时间的tensor flow之后,想找个项目试试手,然后想起了之前在看Theano教程中的一个文本分类的实例,这个星期就用 ...
- 一个基于jQuery写的弹窗效果(附源码)
最近项目中频繁遇到需要弹出窗口的功能,一直使用浏览器默认的Alert和Confirm弹窗,感觉视觉效果不是那么好,而从网上下载的话又找不到合适的,找到的话有些也是十分臃肿,有时候感觉学习配置的功夫自己 ...
- 基于S2SH开发学生考勤管理系统 附源码
开发环境: Windows操作系统开发工具:Eclipse+Jdk+Tomcat+mysql数据库 运行效果图 源码及原文链接:http://javadao.xyz/forum.php?mod=vie ...
- 基于SSH开发银行个人业务管理系统 附源码
开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 运行效果图
- 基于nopcommerce b2c开源项目的精简版开发框架Nop.Framework
http://www.17ky.net/soft/70612.html?v=1#0-sqq-1-39009-9737f6f9e09dfaf5d3fd14d775bfee85 项目详细介绍 该开源项目是 ...
随机推荐
- vertical-align 与 line-height 傻傻分不清??
要说吧,咱家是个菜鸟,以前遇见垂直居中的东东,也是现查现用,其中最长遇到的东西就是 vertical-align 和 line-height,似乎这俩个兄弟都可以实现居中对齐,不过窃以为二者还是有区别 ...
- GreenDao与Rx的完美搭配
作为Android开发者,一定不会对 GreenDao 和 ReactiveX 陌生. GreenDao 号称Android最快的关系型数据库 ReactiveX Rx是一个编程模型,目标是 ...
- SpringMVC文件上传下载
不多说,代码: Spring-config.xml<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件, 如果扫描到有Spring的相关注解的类,则把这些类注 ...
- redis的安装和测试
redis一直都是调用别人部署好的,近日想要自己从灵开始搭建一次.其中也生出不少枝节,与各位猿友共同分享,望少走些弯路! 1.提前准备的资源 redis安装包(本人上传到csdn不需积分即可下载): ...
- 跟着刚哥梳理java知识点——多线程(十六)
创建多线程第一种方式:① 继承:继承Thread.② 重写:重写Thread类的run()方法③ 创建:创建一个子类的对象④ 调用:调用线程的start()方法,启动此线程,调用run()方法 cla ...
- golang kafka
golang kafka – hello world https://github.com/Shopify/sarama https://shopify.github.io/sarama/ con ...
- IE和其他浏览器用JS新窗口打开的问题
Chrome中 window.open(pageURL,name,parameters) pageURL 为子窗口路径 name 为子窗口句柄 parameters 为窗口参数(各参数用逗号分隔) 例 ...
- 对数据缺失的处理(R)
在进行数据分析之前,我们往往需要对数据进行预处理,而最重要一部分就是怎么处理哪些缺失的数据. 通常的方法有四种: 删除这些缺失的数据. 用最高频数来补充缺失数据. 通过变量的相关关系来填充缺失值. 通 ...
- 初遇stm32
刚开始接触32,建一个工程都这么费劲,可能是keil安装时一些文件和库没有安装完整,真是坑啊. 回头可能还要从新安装,然后开始新的学习,争取十天之内入门32,在博客园这个强大的技术支持下, 想不入门都 ...
- ueditor .net设置步骤
1.官网http://ueditor.baidu.com,下载ueditor的.net版本 2.把下载后的文件夹放在项目content目录下 3.页面设置,Featrue为textArea的id 4. ...