.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的精简版开发框架(附源码)的更多相关文章

  1. (干货)基于 veImageX 搭建海报生成平台 -- 附源码

    前言 618 年中促销即将来临,很多公司都会通过海报来宣传自己的促销方案,通常情况下海报由设计团队基于 PS.Sketch 等工具创作,后期若想替换海报文案.商品列表等内容则需打开原工程进行二次创作, ...

  2. 基于Redis缓存的Session共享(附源码)

    基于Redis缓存的Session共享(附源码) 在上一篇文章中我们研究了Redis的安装及一些基本的缓存操作,今天我们就利用Redis缓存实现一个Session共享,基于.NET平台的Seesion ...

  3. 手机端页面自适应解决方案—rem布局(进阶版,附源码示例)

    转自:https://segmentfault.com/a/1190000007350680 一年前笔者写了一篇 <手机端页面自适应解决方案—rem布局>,意外受到很多朋友的关注和喜欢.但 ...

  4. 基于jQuery左右滑动切换特效 附源码

    分享一款基于脚jQuery左右滑动切换特效.这是一款鼠标点击左右箭头按钮图片滚动切换,鼠标移到图片上显示透明边框特效.   效果图如下:   废话不多说,代码奉上!   html代码: <div ...

  5. 一文详解如何用 TensorFlow 实现基于 LSTM 的文本分类(附源码)

    雷锋网按:本文作者陆池,原文载于作者个人博客,雷锋网已获授权. 引言 学习一段时间的tensor flow之后,想找个项目试试手,然后想起了之前在看Theano教程中的一个文本分类的实例,这个星期就用 ...

  6. 一个基于jQuery写的弹窗效果(附源码)

    最近项目中频繁遇到需要弹出窗口的功能,一直使用浏览器默认的Alert和Confirm弹窗,感觉视觉效果不是那么好,而从网上下载的话又找不到合适的,找到的话有些也是十分臃肿,有时候感觉学习配置的功夫自己 ...

  7. 基于S2SH开发学生考勤管理系统 附源码

    开发环境: Windows操作系统开发工具:Eclipse+Jdk+Tomcat+mysql数据库 运行效果图 源码及原文链接:http://javadao.xyz/forum.php?mod=vie ...

  8. 基于SSH开发银行个人业务管理系统 附源码

    开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 运行效果图

  9. 基于nopcommerce b2c开源项目的精简版开发框架Nop.Framework

    http://www.17ky.net/soft/70612.html?v=1#0-sqq-1-39009-9737f6f9e09dfaf5d3fd14d775bfee85 项目详细介绍 该开源项目是 ...

随机推荐

  1. 安卓 ADB常见问题整理

    以下都是ADB连接问题,可以通过尝试如下步骤,由简单度排序 1. 插拔下USB连接线 2. 关闭USB模式再打开 3. 执行以下命令 adb kill-server adb start-server ...

  2. ios 视频/图片压缩

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  3. Java容器的各种总结

    Java容器指的是List,Set,Map这些类.由于翻译的问题,问到集合,Collection这些指的都是它们几个. List ArrayList 随机访问快 LinkedList 插入删除快 这个 ...

  4. 【树莓派】修改树莓派盒子MAC地址

    用树莓派盒子,在某些客户方实施过程中,不同客户的网络环境对树莓派盒子的要求不同,网络管理配置要求MAC地址和IP绑定. 一种情况下,查询盒子的MAC地址,添加到网络管理的路由规则中即可: 另一种情况下 ...

  5. MySQL最常用数值函数

    数值函数: 用来处理很多数值方面的运算,使用数值函数,可以免去很多繁杂的判断求值的过程,能够大大提高用户的工作效率. 1.ABS(x):返回 x 的绝对值 mysql> select abs(- ...

  6. (转载)Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换 时间:2011-04-08作者:woyoo分类:linux评论:0 我友分享: 新浪微博 腾讯微博 搜狐微博 网易微博 开心网 QQ空间 msn 如果你需要在 ...

  7. 深刻认识shift_ram IP core——图像处理学习笔记

    在进行图像处理算法中,往往需要生成图像像素矩阵,这对于C语言来说可以直接用数据表示,但是对于verilog来说便不是那么容易了,硬件描述语言不比软件,它的核心不再算法,而是在于设计思想,加速硬件.在进 ...

  8. ms_celeb_1m数据提取(MsCelebV1-Faces-Aligned.tsv)python脚本

    本文主要介绍了如何对MsCelebV1-Faces-Aligned.tsv文件进行提取 原创by南山南北秋悲 欢迎引用!请注明原地址 http://www.cnblogs.com/hwd9654/p/ ...

  9. Alsa 读取wave文件,并播放wave 文件

    对于一个wave文件,如果需要播放,涉及到几个方面 1.对于wave文件的解析 2.通过解析wave文件,将得到的参数(主要是sampfrequency, bitsperSample,channel) ...

  10. Guvav:Options使用和避免NULL

    JAVA中的null null是Java中一个很重要的概念.null设计初衷是为了表示一些不确定的东西.null是Java中的关键字,它是大小写敏感的,你不能将null写成Null或NULL,编译器将 ...