标签:
http://www.it165.net/pro/html/201407/17685.html
我在网上看到了这篇文章,这边文章主要说的方法就是通过读取配置文件来解决依赖注入的问题。但是每次新建一个依赖注入的类,就要改一下配置文件,这显然不符合设计模式中的开闭原则。先来看看一般的解法。
//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里面调用就行了,
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 的方法去定位啊
标签:
原文地址:http://www.cnblogs.com/kmsfan/p/4292119.html