.netCoreMVC添加数据仓储
在上一篇关于CodeFirst从零搭建ASP.NETCore2.0中搭建起了完整.netCoreMVC项目,在这一篇中将实现如何注册service服务和Repository数据仓储到web中实现数据的统一处理.
首先新建项目:DotNetCore20.Service:
右键解决方案>新建项目:DotNetCore20.Service

添加repository
原先计划自己实现一套repository(将在后续计划再写一篇参考:https://github.com/Arch/UnitOfWork实现Repository的文章),但是由于时间原因后来在nuget中找到了更好更全面的解决方案,暂且利用第三方组件实现repository.
nuget搜索:Microsoft.EntityFrameworkCore.UnitOfWork;源代码:https://github.com/Arch/UnitOfWork

安装到DotNetCore20.Service

在DotNetCore20.Service中添加DemoService.cs和对应的接口IDemoService.cs,目录结构如下:

DemoService.cs:
using DotNetCore20.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks; namespace DotNetCore20.Service
{
public class DemoService : IDemoService
{
private readonly IUnitOfWork _unitOfWork;
public DemoService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
} public async Task<UserExtend> Meth()
{
var repo = _unitOfWork.GetRepository<UserExtend>();
var value = await repo.FindAsync();
return value;
}
}
}
IDemoService:
using DotNetCore20.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks; namespace DotNetCore20.Service
{
public interface IDemoService
{
Task<UserExtend> Meth();
}
}
Startup.cs中注册相关服务:
在Startup中的ConfigureServices方法中添加以下行:
//Customer's services
services.AddUnitOfWork<DotNetCoreDbContext>();//注册数据仓储
services.AddScoped(typeof(IDemoService), typeof(DemoService));//注册service,为了避免都要在此声明,所以之后会统一注册DotNetCore20.Service下的所有service
完整Startup如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using DotNetCore20.Web.Data;
using DotNetCore20.Web.Models;
using DotNetCore20.Web.Services;
using DotNetCore20.DAL.DbContext;
using DotNetCore20.Service; namespace DotNetCore20.Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //自定义数据库连接字符串
services.AddDbContext<DotNetCoreDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DotNetCoreConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); // Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>(); //Customer's services
services.AddUnitOfWork<DotNetCoreDbContext>();
services.AddScoped(typeof(IDemoService), typeof(DemoService)); services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Startup
HomeController:
在Controller的HomeController中的构造函数中添加以下代码:
public class HomeController : Controller
{
private IDemoService _demoService { get; set; }
public HomeController(IDemoService demoService)
{
_demoService = demoService;
}
}
使用Service
public IActionResult Index()
{
_demoService.Meth(); return View();
}
在此大功告成,点击
即可
.netCoreMVC添加数据仓储的更多相关文章
- 从零开始,搭建博客系统MVC5+EF6搭建框架(2),测试添加数据、集成Autofac依赖注入
一.测试仓储层.业务层是否能实现对数据库表的操作 1.创建IsysUserInfoRepository接口来继承IBaseRepository父接口 namespace Wchl.WMBlog.IRe ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑
前言 从上篇30岁找份程序员的工作(伪程序员的独白),文章开始,我说过我要用我自学的技术,来搭建一个博客系统,也希望大家给点意见,另外我很感谢博客园的各位朋友们,对我那篇算是自我阶段总结文章 ...
- 【干货】利用MVC5+EF6搭建博客系统(二)测试添加数据、集成Autofac依赖注入
PS:如果图片模糊,鼠标右击复制图片网址,然后在浏览器中打开即可. 一.测试仓储层.业务层是否能实现对数据库表的操作 1.在52MVCBlog.IRepository程序集下创建IsysUserInf ...
- C#数据仓储类
https://ninesky.codeplex.com/SourceControl/latest /*============================== 版本:v0.1 创建:2016.2 ...
- C#向sql server数据表添加数据源代码
HoverTree解决方案 学习C#.NET,Sql Server,WinForm等的解决方案. 本文链接http://hovertree.com/h/bjaf/0jteg8cv.htm 使用的技术. ...
- js表单动态添加数据并提交
情景1:已经存在form对象了,动态为form增加对象并提交 function formAppendSubmit(){ var myform=$('#newArticleForm'); //得到for ...
- 使用C#类向数据库添加数据的例子源码
在上一篇中,增加了sql server数据库操作类SqlOperator,用于操作sql server数据库.还有一个SqlStringHelper类,用于处理sql语句的单引号.那么这两个类怎么使用 ...
- EF批量添加数据性能慢的问题的解决方案
//EF批量添加数据性能慢的问题的解决方案 public ActionResult BatchAdd() { using (var db = new ToneRoad.CEA.DbContext.Db ...
- mybatis+oracle添加一条数据并返回所添加数据的主键问题
最近做mybatis+oracle项目的时候解决添加一条数据并返回所添加数据的主键问题 controller层 @RequestMapping("/addplan") public ...
随机推荐
- Go 在 TiDB 的实践
https://blog.csdn.net/RA681t58CJxsgCkJ31/article/details/79215751 更多TiDB链接: https://my.oschina.net/z ...
- 转:log4net使用详解
说明:本程序演示如何利用log4net记录程序日志信息.log4net是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括M ...
- python永久添加第三方模块,PYTHONPATH的设置
今天用pip安装pymysql后遇到了一个问题,在PyCharm中import pymysql模块时,运行却提示我找不到pymysql mudule 我先考虑的是pymysql没有安装成功,但是cmd ...
- Mojave使用pyenv安装python-zlib错误
mojave使用pyenv编译python出现 zipimport.ZipImportError: can't decompress data; zlib not available错误 解决方案: ...
- js节流
function _throttling(fn,wait,mustRun){ var time=null; var startTime= new Date() return function(){ c ...
- C# ActiveX 网页打包验证自动升级
原文地址:http://www.cnblogs.com/yilin/p/csharp-activex.html 注意事项:Win10下需要设置兼容模式,F12仿真切换到IE6-8(版本参考——BT90 ...
- Spring 加载Controller逻辑的源码笔记
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#initHandlerMethods 进行加载Controll ...
- thinkphp5整合 gatewaywork实现聊天
1:将下载的gatewaywork下的\vendor 下的workman文件夹,整个复制到tp5下的vendor目录下 2:tp5\application\push 新键push文件夹,将下载的 ...
- docker swarm英文文档学习-5-在swarm模式中运行Docker引擎
Run Docker Engine in swarm mode在swarm模式中运行Docker引擎 当你第一次安装并开始使用Docker引擎时,默认情况下禁用swarm模式.在启用集群模式时,需要处 ...
- docker构建Java环境
FROM java:7 COPY . /usr/src/javaapp WORKDIR /usr/src/javaapp RUN javac HelloWorld.java CMD ["ja ...