ASP.NET MVC IOC 之AutoFac攻略

2 篇文章 0 订阅
2 篇文章 0 订阅

三、ASP.NET MVC与AtuoFac

终于到了ASP.NET MVC与AtuoFac双剑合璧的时候了,下面就看看AtuoFac在MVC中的应用,其实很简单,大概就几个步骤搞定:

1、首先在函数Application_Start() 注册自己的控制器类,一定要引入Autofac.Integration.Mvc.dll

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Http;  
  6. using System.Web.Mvc;  
  7. using System.Web.Optimization;  
  8. using System.Web.Routing;  
  9. using Autofac;  
  10. using AtuoFacOfMVC4.Models;  
  11. using System.Reflection;  
  12. using Autofac.Integration.Mvc;  
  13.  
  14.  
  15. namespace AtuoFacOfMVC4  
  16. {  
  17.    public class MvcApplication : System.Web.HttpApplication  
  18.     {  
  19.         protected void Application_Start()  
  20.         {  
  21.             var builder = new ContainerBuilder();  
  22.             SetupResolveRules(builder);  
  23.             builder.RegisterControllers(Assembly.GetExecutingAssembly());  
  24.             var container = builder.Build();  
  25.             DependencyResolver.SetResolver(new AutofacDependencyResolver(container));  
  26.  
  27.             AreaRegistration.RegisterAllAreas();  
  28.             WebApiConfig.Register(GlobalConfiguration.Configuration);  
  29.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  30.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  31.             BundleConfig.RegisterBundles(BundleTable.Bundles);  
  32.             AuthConfig.RegisterAuth();  
  33.         }  
  34.         private void SetupResolveRules(ContainerBuilder builder)  
  35.         {  
  36.             builder.RegisterType<StudentRepository>().As<IStudentRepository>();  
  37.         }  
  38.     }  

2、现在在你的MVC程序中注入依赖代码就ok了

(1)首先声明一个Student学生类

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.  
  6. namespace AtuoFacOfMVC4.Models  
  7. {  
  8.     public class Student  
  9.     {  
  10.         public int Id { get; set; }  
  11.         public string Name { get; set; }  
  12.         public string Graduation { get; set; }  
  13.         public string School { get; set; }  
  14.         public string Major { get; set; }  
  15.     }  
  16. }  

(2)然后声明仓储接口及其实现

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace AtuoFacOfMVC4.Models  
  7. {  
  8.     public interface IStudentRepository  
  9.     {  
  10.         IEnumerable<Student> GetAll();  
  11.         Student Get(int id);  
  12.         Student Add(Student item);  
  13.         bool Update(Student item);  
  14.         bool Delete(int id);  
  15.     }  
  16. }  
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.  
  6. namespace AtuoFacOfMVC4.Models  
  7. {  
  8.     public class StudentRepository : IStudentRepository  
  9.     {  
  10.         private List<Student> Articles = new List<Student>();  
  11.  
  12.         public StudentRepository()  
  13.         {  
  14.             //添加演示数据  
  15.             Add(new Student { Id = 1, Name = "张三", Major = "软件工程", Graduation = "2013年", School = "西安工业大学" });  
  16.             Add(new Student { Id = 2, Name = "李四", Major = "计算机科学与技术", Graduation = "2013年", School = "西安工业大学" });  
  17.             Add(new Student { Id = 3, Name = "王五", Major = "自动化", Graduation = "2013年", School = "西安工业大学" });  
  18.         }  
  19.         /// <summary>  
  20.         /// 获取全部学生信息  
  21.         /// </summary>  
  22.         /// <returns></returns>  
  23.         public IEnumerable<Student> GetAll()  
  24.         {  
  25.             return Articles;  
  26.         }  
  27.         /// <summary>  
  28.         /// 通过ID获取学生信息  
  29.         /// </summary>  
  30.         /// <param name="id"></param>  
  31.         /// <returns></returns>  
  32.         public Student Get(int id)  
  33.         {  
  34.             return Articles.Find(p => p.Id == id);  
  35.         }  
  36.         /// <summary>  
  37.         /// 添加学生信息  
  38.         /// </summary>  
  39.         /// <param name="item"></param>  
  40.         /// <returns></returns>  
  41.         public Student Add(Student item)  
  42.         {  
  43.             if (item == null)  
  44.             {  
  45.                 throw new ArgumentNullException("item");  
  46.             }  
  47.             Articles.Add(item);  
  48.             return item;  
  49.         }  
  50.         /// <summary>  
  51.         /// 更新学生信息  
  52.         /// </summary>  
  53.         /// <param name="item"></param>  
  54.         /// <returns></returns>  
  55.         public bool Update(Student item)  
  56.         {  
  57.             if (item == null)  
  58.             {  
  59.                 throw new ArgumentNullException("item");  
  60.             }  
  61.  
  62.             int index = Articles.FindIndex(p => p.Id == item.Id);  
  63.             if (index == -1)  
  64.             {  
  65.                 return false;  
  66.             }  
  67.             Articles.RemoveAt(index);  
  68.             Articles.Add(item);  
  69.             return true;  
  70.         }  
  71.         /// <summary>  
  72.         /// 删除学生信息  
  73.         /// </summary>  
  74.         /// <param name="id"></param>  
  75.         /// <returns></returns>  
  76.         public bool Delete(int id)  
  77.         {  
  78.             Articles.RemoveAll(p => p.Id == id);  
  79.             return true;  
  80.         }  
  81.     }  

(3)最后添加控制器StudentController,并注入依赖代码

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using AtuoFacOfMVC4.Models;  
  7.  
  8. namespace AtuoFacOfMVC4.Controllers  
  9. {  
  10.     public class StudentController : Controller  
  11.     {  
  12.         readonly IStudentRepository repository;  
  13.         //构造器注入  
  14.         public StudentController(IStudentRepository repository)  
  15.         {  
  16.             this.repository = repository;  
  17.         }  
  18.  
  19.         public ActionResult Index()  
  20.         {  
  21.             var data = repository.GetAll();  
  22.             return View(data);  
  23.         }  
  24.  
  25.     }  

(4)最后为控制器StudentController的Index方法添加视图即可,这里不再详述,运行效果如下

 

原文链接:http://www.cnblogs.com/WeiGe/p/3871451.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值