.netCoreMVC添加数据仓储
来源:互联网 发布:商为开店软件 编辑:程序博客网 时间:2024/05/06 05:51
在上一篇关于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?}"); }); } }}
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(); }
在此大功告成,点击即可
阅读全文
0 0
- .netCoreMVC添加数据仓储
- 数据仓储
- Centos构建Maven数据仓储服务
- 数据库与数据仓储的本质区别
- 关于oracle jdbc driver OracleDriver添加maven仓储问题
- 后台管理框架之五 :数据仓储设计
- 仓储服务
- 仓储类型
- 仓储模式
- 仓储模式
- 仓储系统
- [转 张晓辉]在实施数据仓储过程中应避免的11个错误
- WMS仓储管理系统实现"收发存日报表"数据查询功能Sql语句--Oracle
- 图书盘点机,图书条码数据采集器,高效图书仓储管理条码解决方案
- 在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现
- 聚焦数据可视化之一--沃尔马如何利用数据可视化根据实时社交数据调整采购和仓储计划?
- 数据添加
- 添加数据
- gcd多线程查看是否执行完毕dispatch_barrier_async
- 合并果子
- Spring7大模块
- spring+springMVC+mybatis整合示例(基于XML配置文件)
- 正确处理listview的position
- .netCoreMVC添加数据仓储
- Python 函数参数传递方式
- Android 控制屏幕唤醒常亮或熄灭
- RCF
- Codeforces 834C The Meaningless Game【思维】
- 父元素没设置高度子元素自适应高度
- Nginx反向代理以及缓存
- hdu1198 dfs
- 一些输入输出函数