前言 上一篇《 "一步一步創建ASP.NET MVC5程式\[Repository+Autofac+Automapper+SqlSugar\" ][1]》,我們完成了: 引用SqlSugar 使用SqlSugar對Repository類的改造 併成功使用PostRepository來查詢到了數據,今 ...
前言
上一篇《一步一步創建ASP.NET MVC5程式[Repository+Autofac+Automapper+SqlSugar](三)》,我們完成了:
* 引用SqlSugar
* 使用SqlSugar對Repository類的改造
併成功使用PostRepository來查詢到了數據,今天我們來創建一個新的服務層以及安裝配置依賴註入框架組件Autofac等。
本篇知識要點
* 創建服務層:TsBlog.Services
* 創建服務介面
* 實現服務介面
* 創建倉儲介面
* 安裝Autofac依賴註入組件
* 註冊配置Autofac 依賴註入
教程內容
創建服務層
選中解決方案中的解決方案文件夾[1.Libraries],右鍵單擊=>>添加=>>新項目,在彈出的對話框中添加一個.NET Framework 4.6.2的C#類庫項目,命名為:TsBlog.Services。項目創建成功後,刪除自動生成的Class1.cs文件。
由於服務層需要依賴於倉儲層,所以首先切換到倉儲層[TsBlog.Repositories]項目中,創建博文的倉儲介面類:IPostRepository,代碼如下:
using System.Collections.Generic;
using TsBlog.Domain.Entities;
namespace TsBlog.Repositories
{
public interface IPostRepository
{
/// <summary>
/// 根據ID查詢單條數據
/// </summary>
/// <param name="id">ID</param>
/// <returns></returns>
Post FindById(int id);
/// <summary>
/// 查詢所有數據(無分頁,大數量時請慎用)
/// </summary>
/// <returns></returns>
IEnumerable<Post> FindAll();
/// <summary>
/// 寫入實體數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
int Insert(Post entity);
/// <summary>
/// 更新實體數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
bool Update(Post entity);
/// <summary>
/// 根據實體刪除一條數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
bool Delete(Post entity);
/// <summary>
/// 刪除指定ID的數據
/// </summary>
/// <param name="id">主鍵ID</param>
/// <returns></returns>
bool DeleteById(object id);
/// <summary>
/// 刪除指定ID集合的數據(批量刪除)
/// </summary>
/// <param name="ids">主鍵ID集合</param>
/// <returns></returns>
bool DeleteByIds(object[] ids);
}
}
再切換到服務層,在剛纔創建的服務層項目中首先引用倉儲層,並分別創建以下服務介面和類文件:
IPostService.cs:
using System.Collections.Generic;
using TsBlog.Domain.Entities;
namespace TsBlog.Services
{
public interface IPostService
{
/// <summary>
/// 根據ID查詢單條數據
/// </summary>
/// <param name="id">ID</param>
/// <returns></returns>
Post FindById(int id);
/// <summary>
/// 查詢所有數據(無分頁,大數量時請慎用)
/// </summary>
/// <returns></returns>
IEnumerable<Post> FindAll();
/// <summary>
/// 寫入實體數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
int Insert(Post entity);
/// <summary>
/// 更新實體數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
bool Update(Post entity);
/// <summary>
/// 根據實體刪除一條數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
bool Delete(Post entity);
/// <summary>
/// 刪除指定ID的數據
/// </summary>
/// <param name="id">主鍵ID</param>
/// <returns></returns>
bool DeleteById(object id);
/// <summary>
/// 刪除指定ID集合的數據(批量刪除)
/// </summary>
/// <param name="ids">主鍵ID集合</param>
/// <returns></returns>
bool DeleteByIds(object[] ids);
}
}
PostService.cs
using System.Collections.Generic;
using TsBlog.Domain.Entities;
using TsBlog.Repositories;
namespace TsBlog.Services
{
public class PostService : IPostService
{
private readonly IPostRepository _postRepository;
public PostService(IPostRepository postRepository)
{
_postRepository = postRepository;
}
public bool Delete(Post entity)
{
return _postRepository.Delete(entity);
}
public bool DeleteById(object id)
{
return _postRepository.DeleteById(id);
}
public bool DeleteByIds(object[] ids)
{
return _postRepository.DeleteByIds(ids);
}
public IEnumerable<Post> FindAll()
{
return _postRepository.FindAll();
}
public Post FindById(int id)
{
return _postRepository.FindById(id);
}
public int Insert(Post entity)
{
return _postRepository.Insert(entity);
}
public bool Update(Post entity)
{
return _postRepository.Update(entity);
}
}
}
最後,我們再切換到倉儲層,在PostRepository文件中使用IPostRepository介面並使用SqlSugar實現該介面中的所有數據操作的方法,
PostRepository.cs
using System.Collections.Generic;
using TsBlog.Domain.Entities;
namespace TsBlog.Repositories
{
/// <summary>
/// POST表的資料庫操作類
/// </summary>
public class PostRepository : IPostRepository
{
/// <summary>
/// 根據ID查詢
/// </summary>
/// <param name="id">Post ID</param>
/// <returns></returns>
public Post FindById(int id)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var entity = db.Queryable<Post>().Single(x => x.Id == id);
return entity;
}
}
/// <summary>
/// 查詢所有數據(無分頁,大數量時請慎用)
/// </summary>
/// <returns></returns>
public IEnumerable<Post> FindAll()
{
#region SqlSugar讀取方式
using (var db = DbFactory.GetSqlSugarClient())
{
var list = db.Queryable<Post>().ToList();
return list;
}
#endregion
}
/// <summary>
/// 寫入實體數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
public int Insert(Post entity)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Insertable(entity).ExecuteReturnBigIdentity();
//返回的i是long類型,這裡你可以根據你的業務需要進行處理
return (int)i;
}
}
/// <summary>
/// 更新實體數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
public bool Update(Post entity)
{
using (var db = DbFactory.GetSqlSugarClient())
{
//這種方式會以主鍵為條件
var i = db.Updateable(entity).ExecuteCommand();
return i > 0;
}
}
/// <summary>
/// 根據實體刪除一條數據
/// </summary>
/// <param name="entity">博文實體類</param>
/// <returns></returns>
public bool Delete(Post entity)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Deleteable(entity).ExecuteCommand();
return i > 0;
}
}
/// <summary>
/// 刪除指定ID的數據
/// </summary>
/// <param name="id">主鍵ID</param>
/// <returns></returns>
public bool DeleteById(object id)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Deleteable<Post>(id).ExecuteCommand();
return i > 0;
}
}
/// <summary>
/// 刪除指定ID集合的數據(批量刪除)
/// </summary>
/// <param name="ids">主鍵ID集合</param>
/// <returns></returns>
public bool DeleteByIds(object[] ids)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Deleteable<Post>().In(ids).ExecuteCommand();
return i > 0;
}
}
}
}
到這裡,我們的倉儲和服務層準備工作就完成了,接下來安裝依賴註入組件:Autofac
安裝Autofac
選擇解決方案夾[2.Persentation]中的Web項目[TsBlog.Frontend],在"引用"("References")上單擊右鍵,調出Nuget程式包管理界面,搜索"autofac",如下:
Autofac的當前版本為:v4.6.2
同時,再搜索"Autofac.Mvc5",如下:
配置/註冊依賴選項
Autofac安裝完成之後,我們需要對依賴的介面對實現在Autofac中進行註冊,本示例的Autofac配置在Global.asax文件中(請確保TsBlog.Frontend項目中引用了:TsBlog.Domain,TsBlog.Repositories,TsBlog.Servcies這本個項目),如下:
Global.asax
using Autofac;
using Autofac.Integration.Mvc;
using System.Web.Mvc;
using System.Web.Routing;
using TsBlog.Repositories;
using TsBlog.Services;
namespace TsBlog.Frontend
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacRegister();
}
private void AutofacRegister()
{
var builder = new ContainerBuilder();
//註冊MvcApplication程式集中所有的控制器
builder.RegisterControllers(typeof(MvcApplication).Assembly);
//註冊倉儲層服務
builder.RegisterType<PostRepository>().As<IPostRepository>();
//註冊服務層服務
builder.RegisterType<PostService>().As<IPostService>();
//註冊過濾器
builder.RegisterFilterProvider();
var container = builder.Build();
//設置依賴註入解析器
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
然後,我們修改控制器文件夾中的HomeController,修改後的代碼如下:
HomeController.cs
using System.Web.Mvc;
using TsBlog.Services;
namespace TsBlog.Frontend.Controllers
{
public class HomeController : Controller
{
private readonly IPostService _postService;
public HomeController(IPostService postService)
{
_postService = postService;
}
public ActionResult Index()
{
return View();
}
public ActionResult Post()
{
//var postRepository = new PostRepository();
//var post = postRepository.FindById(1);
//return View(post);
var post = _postService.FindById(1);
return View(post);
}
}
}
再次按F5運行,打開頁面:http://localhost:54739/home/post,這次我們可以看到和前兩篇一樣的運行效果了:
本文的源碼托管地址:https://github.com/lampo1024/TsBlog/releases/tag/v1.4
本文學習到此結束,本系列未完待續......
如果你喜歡Rector的本系列文章,請為我點個大大的贊,以支持Rector在後續的寫作中更有基(激)情,哈哈。。。
本文同步發表至 圖享網 《一步一步創建ASP.NET MVC5程式[Repository+Autofac+Automapper+SqlSugar](四)》