文章出處
文章列表
1.nuget安裝依賴
2.配置注入規則(Global.asax)
using Autofac; using Autofac.Integration.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace BestExpress { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); #region Autofac注入依賴 var builder = new ContainerBuilder(); SetupResolveRules(builder); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); #endregion } private static void SetupResolveRules(ContainerBuilder builder) { builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces();
//注冊泛型規則
builder.RegisterGeneric(typeof(EntRegCRUDService<>)).As(typeof(ICRUDService<>));
} } }
crud注入Controller
private ICRUDService<X6_EntryRegistration> _crud; public X6_EntryRegistrationController(ICRUDService<X6_EntryRegistration> crud) { _crud = crud; }
//基于SqlSugarOrm的依賴注入:
interface:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EntryRegistration.Models { public interface IDBService<T> : IDisposable { T GetDBInstance(); } }
//實現類
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EntryRegistration.Models { public class SqlServerService : IDBService<SqlSugarClient> { public SqlSugarClient _db; public SqlServerService() { string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); _db = new SqlSugarClient(ConnectionString);//獲SqlSugarClient對象 } public SqlSugarClient GetDBInstance() { return _db; } public void Dispose() { if (_db != null) { _db.Dispose(); } } } }
在控制器中的注入:
private SqlSugarClient _db; public ViewController(IDBService<SqlSugarClient> db) { _db = db.GetDBInstance(); }
CRUD:
//interface
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; namespace EntryRegistration.Models.CRUD { public interface ICRUDService<T> { /// <summary> /// 添加 /// </summary> /// <returns></returns> ActionResult Add(T obj); /// <summary> /// 刪除 /// </summary> /// <param name="obj"></param> /// <returns></returns> ActionResult Del(Guid VGUID); /// <summary> /// 編輯 /// </summary> /// <param name="obj">當前類型實例</param> /// <returns></returns> ActionResult Edit(T obj); /// <summary> /// 通過主表ID查列表 /// </summary> /// <param name="EntryRegistrationID"></param> /// <returns></returns> ActionResult Select(Guid EntryRegistrationID); } }
//實現類
using Best.Entities.Tables; using EntryRegistration.Models.CRUD; using EntryRegistration.Models.Entity; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace EntryRegistration.Models.CRUD { public class EntRegCRUDService<T> : ICRUDService<T> where T : class, new() { private SqlSugarClient _db; public EntRegCRUDService(IDBService<SqlSugarClient> db) { _db = db.GetDBInstance(); } /// <summary> /// 添加方法實現 /// </summary> /// <param name="obj"></param> /// <returns>Json結果</returns> public ActionResult Add(T obj) { ActionResultModel<string> result = new ActionResultModel<string>(); try { if (typeof(T) != typeof(X6_EntryRegistration)) { obj.GetType().GetProperty("VGUID").SetValue(obj, Guid.NewGuid()); } #region 數據驗證 //必填驗證 CheckResult checkResult = Models.Check.CheckRequire<T>(obj); if (checkResult.isSuccess != true) { return JsonHandle.GetResult(false, checkResult.msg); } #endregion //數據插入 var IsSuccess = _db.Insert(obj); if (IsSuccess.ToString() == "true") { return JsonHandle.GetResult(true, "數據插入成功"); } else { return JsonHandle.GetResult(false, "數據插入失敗"); } } catch (Exception ex) { result.isSuccess = false; result.responseInfo = ex.Message; } return JsonHandle.GetResult(result.isSuccess, result.responseInfo); } /// <summary> /// 刪除方法實現 /// </summary> /// <param name="VGUID"></param> /// <returns>Json結果</returns> public ActionResult Del(Guid VGUID) { ActionResultModel<string> result = new ActionResultModel<string>(); try { bool isSuccess = _db.Delete<T, Guid>(VGUID); if (isSuccess == true) { return JsonHandle.GetResult(true, "刪除成功"); } else { return JsonHandle.GetResult(false, "刪除失敗"); } } catch (Exception ex) { result.isSuccess = false; result.responseInfo = ex.Message; } return JsonHandle.GetResult(result.isSuccess, result.responseInfo); } /// <summary> /// 編輯方法實現 /// </summary> /// <param name="obj"></param> /// <returns>Json結果</returns> public ActionResult Edit(T obj) { ActionResultModel<string> result = new ActionResultModel<string>(); try { var VGUID = obj.GetType().GetProperty("VGUID").GetValue(obj, null); bool isSuccess = _db.Update<T, Guid>(obj, Guid.Parse(VGUID.ToString())); if (isSuccess == true) { return JsonHandle.GetResult(true, "更新成功"); } else { return JsonHandle.GetResult(false, "更新失敗"); } } catch (Exception ex) { result.isSuccess = false; result.responseInfo = ex.Message; } return JsonHandle.GetResult(result.isSuccess, result.responseInfo); } /// <summary> /// 查詢方法實現 /// </summary> /// <param name="EntryRegistrationID"></param> /// <returns>Json結果</returns> public ActionResult Select(Guid EntryRegistrationID) { var list = _db.SqlQuery<T>("select * from " + typeof(T).Name + " where EntryRegistrationID=@EntryRegistrationID", new { EntryRegistrationID = EntryRegistrationID }) .ToList(); return JsonHandle.GetResult(list); } } }
文章列表
全站熱搜