当前位置:编程学习 > asp >>

为ASP.NET MVC创建一个基于Mini容器的ControllerFactory

背景
     随着Asp.net mvc的越来越流行,关于和Mvc整合的DI容器也非常的多,而Mini容器一致没有把整合的方案发布出来,以至于不时的收到有些网友询问这些事,于是便有了该文。[源代码从这里下载,Mini容器官方地址]
解决方案
      创建自定义工厂         
   public class NLiteControllerFactory : DefaultControllerFactory
    {
        private IServiceLocator ServiceLocator;
        public NLiteControllerFactory(IServiceLocator serviceLocator)
        {
            this.ServiceLocator = serviceLocator;
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return (IController)ServiceLocator.Get(controllerType);
        }
    }
复制代码
Demo 例子
     为了演示效果效果, 用Artech兄的为ASP.NET MVC创建一个基于Unity的ControllerFactory这篇博客的例子了. 要创建一个维护联系人的应用,我们通过具有如下定义的Contact类型表示联系人,而IContactRepository接口定义了一个从存储中获取所有联系人的GetAllContacts方法,DefaultContactRepository是对IContactRepository接口的实现
    public class Contact
    {
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }
    }
   
    [Contract]
    public inte易做图ce IContactRepository
    {
        IEnumerable<Contact> GetAllContacts();
    }

    public class DefaultContactRepository : IContactRepository
    {
        public IEnumerable<Contact> GetAllContacts()
        {
            yield return new Contact
            {
                Name = "Zhang San",
                Gender = "Male",
                Address = "#328, XingHu Street, Su Zhou, Jiang Su Province, PRC."
            };

            yield return new Contact
            {
                Name = "Li Si",
                Gender = "Female",
                Address = "#328, Jin Ji Hu Road, Su Zhou, Jiang Su Province, PRC."
            };
        }
    }
复制代码
 
     我们在Web应用的主页显示联系人列表,为此我创建了如下一个HomeController。在这里我们演示的是构造器注入,所以我们通过构造函数指定的IContactRepository对象来初始化Repository属性。在Action方法Index中调用IContactRepository的GetAllContacts方法为对应的View指定Model。 
 
    public class HomeController : Controller
    {
        public IContactRepository Repository { get; private set; }
        public HomeController(IContactRepository repository)
        {
            this.Repository = repository;
        }
        public ActionResult Index()
        {
            return View(this.Repository.GetAllContacts());
        }

    }
复制代码
   在Global 文件中注册Mvc 的自定义控制器工厂
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
           routes.IgnoreRoute("favicon.ico");
           routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //创建Mini容器
            NLiteEnvironment.Refresh();
           
            //设置组件的生命周期缺省是临时的
            LifestyleType.Default = LifestyleFlags.Transient;

            //注册联系人仓储组件
    &nb

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,