http://www.it165.net/pro/html/201407/17685.htmlhtml
我在網上看到了這篇文章,這邊文章主要說的方法就是經過讀取配置文件來解決依賴注入的問題。可是每次新建一個依賴注入的類,就要改一下配置文件,這顯然不符合設計模式中的開閉原則。先來看看通常的解法。web
//var container = new UnityContainer(); //container.RegisterType(typeof(IRegisterService), typeof(RegisterService)); //IRegisterService ins = (IRegisterService)container.Resolve(typeof(IRegisterService));
因而咱們想到,只要把其中的類替換成泛型類就好了,那這個怎麼寫呢?很簡單,新建一個公共靜態的方法,而後用的時候調用這個方法就好了。下面是公共方法我寫的,因而你們會想看看區別,我給 你們看看調用CONFIG文件和直接用泛型的區別,注意多個泛型類的寫法:設計模式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using Microsoft.Practices.Unity; using System.Configuration; using System.Xml; using Microsoft.Practices.Unity.Configuration; namespace MapleStory2.Common { public class UnityTool<IClass> where IClass:class { /// <summary> /// 設置依賴注入映射,經過web.config映射 /// </summary> public static IClass UnityConfiguration() { IUnityContainer container=new UnityContainer(); Configuration config = DBUtil.GetConfig(System.Web.HttpContext.Current.Server.MapPath("~/MapleStory2.Common/unity.config")); UnityConfigurationSection seciton = (UnityConfigurationSection)config.GetSection("unity"); container.LoadConfiguration(seciton, "FirstClass"); IClass classInfo= container.Resolve<IClass>("ec"); return classInfo; } } /// <summary> /// 不經過config文件映射 /// </summary> /// <typeparam name="IClass">接口類</typeparam> /// <typeparam name="cbClass">接口實現類</typeparam> public class UnityHelper<IClass, cbClass> where IClass : class where cbClass : class { public static IClass UnityUnitTest() { var container = new UnityContainer(); container.RegisterType(typeof(IClass), typeof(cbClass)); IClass ins = (IClass)container.Resolve(typeof(IClass)); return ins; } } }
好了,我只要在controller裏面調用就好了,app
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; using MapleStory2.IDAL.Account; using MapleStory2.Model; using MapleStory2.Model.Models; using MapleStory2.DAL.Account; using MapleStory2.Common; namespace MapleStory2.Controllers { public class RegisterController : Controller { [Dependency] public IRegisterService registerService { get; set; } // // GET: /Register/ public ActionResult Index() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Register(UserQueryData user) { //依賴注入,詳情: //https://msdn.microsoft.com/en-us/library/ff650319.aspx //var container = new UnityContainer(); //container.RegisterType(typeof(IRegisterService), typeof(RegisterService)); //IRegisterService ins = (IRegisterService)container.Resolve(typeof(IRegisterService)); //依賴注入公共類--依賴WEB.CONFIG文件 // registerService = UnityTool<IRegisterService>.UnityConfiguration(); //依賴注入公共類--不依靠WEB.CONFIG文件 registerService = UnityHelper<IRegisterService, RegisterService>.UnityUnitTest(); if (ModelState.IsValid) { bool flag = registerService.AddNormalUser(user); //添加普通用戶 //int count = 0; //using (maplestory2Context mc = new maplestory2Context()) //{ // string userId = Guid.NewGuid().ToString(); // //往用戶表裏插入數據 // mc.Users.Add(new User // { // ID = userId, // UserName = user.UserName, // Password = user.Password, // NickName = user.UserName, // RegDate = DateTime.Now, // SortCode = 0 // }); // //count = mc.SaveChanges(); // //往用戶詳情表裏插入數據 // mc.UserDetails.Add(new UserDetail() // { // DetailID = Guid.NewGuid().ToString(), // DetailType = 0, // BornLocation = user.BornLocation, // UserID = userId, // UserName = user.UserName // }); // count = mc.SaveChanges(); //} //if (count > 0) //{ // ModelState.Clear(); // user = null; // ViewBag.Message = "註冊成功"; //} if (flag) { ModelState.Clear(); user = null; ViewBag.Message = "註冊成功"; } } return View("Index"); } } }
就這麼簡單,可是難以琢磨,對了,請教你們一個問題,怎麼在找到一個文件在程序集中的物理地址呢?網站
好比這個unity.config文件,不在網站裏面而在code library 裏面,我沒法經過 Server.mappath或者Assembly 的方法去定位啊ui