在上一篇關於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(); }
在此大功告成,點擊即可