112

基于EF Core的Code First模式的DotNetCore快速开发框架 - 一只卖蜂蜜的码农

 6 years ago
source link: http://www.cnblogs.com/zengxw/p/7673952.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基于EF Core的Code First模式的DotNetCore快速开发框架

最近接了几个小单子,因为是小单子,项目规模都比较小,业务相对来说,也比较简单。所以在选择架构的时候,考虑到效率方面的因素,就采取了asp.net+entity framework中的code first模式,从而可以进行快速开发。几个单子做完下来,顺便总结整理一下,近些时候也一直在学习dotnetcore,索性将项目都升级了,于是便有了这一套“基于EF Core的Code First模式的DotNetCore快速开发框架”。至于code first模式的优劣,此文将不再赘述。至于本文的目的,一是为了总结和整理工作这几年所学的一些知识,方便以后能够快速高效地接入项目中。再是分享出来,跟大家一起探讨学习,一起进步。欢迎各路大佬指正和建议^_^

GitHub

dotnetcore版本:https://github.com/VictorTzeng/Zxw.Framework.NetCore

.net framework版本:https://github.com/VictorTzeng/Zxw.Framework.Nfx

码云:

dotnetcore版本:https://gitee.com/ceo_bitch/Zxw.Framework.NetCore

.net framework版本:https://gitee.com/ceo_bitch/Zxw.Framework.Nfx

此项目主要分成两部分:Zxw.Framework.NetCore (核心类库)和 NetCore.Sample (示例)两部分。如图所示:

1113613-20171015191123449-194835257.png

Zxw.Framework.NetCore 项目说明:

  • Attributes —— 一些常用的属性
  • CodeGenerator —— 代码生成器,用于生成Repository和Service层的代码
  • CodeTemplate —— Repository和Service层代码模板
  • EfDbContext —— EF上下文
  • Extensions —— 一些常用的扩展方法
  • Filters —— 一些常用的拦截器
  • Helpers —— 一些常用的帮助类
  • IoC —— IoC容器封装类,Autofac
  • IRepositories —— Repository接口类
  • IServices —— Service接口类
  • Middlewares —— 中间件
  • Models —— 实体接口类,IBaseModel<TKey>
  • Options —— 一些常用的配置类
  • Repositories —— Repository层的父类
  • Services —— Service层的父类

NetCore.Sample 所示,按照此项目结构创建好:

  • Zxw.Framework.Website —— 网站
  • Zxw.Framework.Website.Controllers —— 控制器
  • Zxw.Framework.Website.IRepositories —— 仓储接口
  • Zxw.Framework.Website.IServices —— Service接口
  • Zxw.Framework.Website.Models —— 实体
  • Zxw.Framework.Website.Repositories —— 仓储
  • Zxw.Framework.Website.Services —— Services
  • Zxw.Framework.Website.ViewModels —— ViewModels

安装nuget package:

Install-Package Zxw.Framework.NetCore -Version 1.0.1

需要注意以下几点:

  1. 所有实体都需实现IBaseModel<TKey>接口(TKey是主键类型),如果需要在数据库中生成对应的数据表
  2. 如果IRepositories、IServices、Repositories、Services这四个项目没有单独建立,调用代码生成器生成的代码将存在于调用项目的目录下
  3. 利用代码生成器生成的代码文件需要手动添加到项目中

实体示例:

ContractedBlock.gifExpandedBlockStart.gif

View Code

在Startup.cs文件中使用:

ContractedBlock.gifExpandedBlockStart.gif

View Code

使用代码生成器:

ContractedBlock.gifExpandedBlockStart.gif

View Code

写博客真的很费力,希望自己能够坚持下去。

最后,欢迎各路大佬建议和拍砖~~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK