三、ASP.NET MVC与AtuoFac
终于到了ASP.NET MVC与AtuoFac双剑合璧的时候了,下面就看看AtuoFac在MVC中的应用,其实很简单,大概就几个步骤搞定:
1、首先在函数Application_Start() 注册自己的控制器类,一定要引入Autofac.Integration.Mvc.dll
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
- using Autofac;
- using AtuoFacOfMVC4.Models;
- using System.Reflection;
- using Autofac.Integration.Mvc;
- namespace AtuoFacOfMVC4
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- var builder = new ContainerBuilder();
- SetupResolveRules(builder);
- builder.RegisterControllers(Assembly.GetExecutingAssembly());
- var container = builder.Build();
- DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
- AreaRegistration.RegisterAllAreas();
- WebApiConfig.Register(GlobalConfiguration.Configuration);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- AuthConfig.RegisterAuth();
- }
- private void SetupResolveRules(ContainerBuilder builder)
- {
- builder.RegisterType<StudentRepository>().As<IStudentRepository>();
- }
- }
- }
2、现在在你的MVC程序中注入依赖代码就ok了
(1)首先声明一个Student学生类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace AtuoFacOfMVC4.Models
- {
- public class Student
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Graduation { get; set; }
- public string School { get; set; }
- public string Major { get; set; }
- }
- }
(2)然后声明仓储接口及其实现
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AtuoFacOfMVC4.Models
- {
- public interface IStudentRepository
- {
- IEnumerable<Student> GetAll();
- Student Get(int id);
- Student Add(Student item);
- bool Update(Student item);
- bool Delete(int id);
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace AtuoFacOfMVC4.Models
- {
- public class StudentRepository : IStudentRepository
- {
- private List<Student> Articles = new List<Student>();
- public StudentRepository()
- {
- //添加演示数据
- Add(new Student { Id = 1, Name = "张三", Major = "软件工程", Graduation = "2013年", School = "西安工业大学" });
- Add(new Student { Id = 2, Name = "李四", Major = "计算机科学与技术", Graduation = "2013年", School = "西安工业大学" });
- Add(new Student { Id = 3, Name = "王五", Major = "自动化", Graduation = "2013年", School = "西安工业大学" });
- }
- /// <summary>
- /// 获取全部学生信息
- /// </summary>
- /// <returns></returns>
- public IEnumerable<Student> GetAll()
- {
- return Articles;
- }
- /// <summary>
- /// 通过ID获取学生信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public Student Get(int id)
- {
- return Articles.Find(p => p.Id == id);
- }
- /// <summary>
- /// 添加学生信息
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- public Student Add(Student item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- Articles.Add(item);
- return item;
- }
- /// <summary>
- /// 更新学生信息
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- public bool Update(Student item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- int index = Articles.FindIndex(p => p.Id == item.Id);
- if (index == -1)
- {
- return false;
- }
- Articles.RemoveAt(index);
- Articles.Add(item);
- return true;
- }
- /// <summary>
- /// 删除学生信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public bool Delete(int id)
- {
- Articles.RemoveAll(p => p.Id == id);
- return true;
- }
- }
- }
(3)最后添加控制器StudentController,并注入依赖代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using AtuoFacOfMVC4.Models;
- namespace AtuoFacOfMVC4.Controllers
- {
- public class StudentController : Controller
- {
- readonly IStudentRepository repository;
- //构造器注入
- public StudentController(IStudentRepository repository)
- {
- this.repository = repository;
- }
- public ActionResult Index()
- {
- var data = repository.GetAll();
- return View(data);
- }
- }
- }
(4)最后为控制器StudentController的Index方法添加视图即可,这里不再详述,运行效果如下
原文链接:http://www.cnblogs.com/WeiGe/p/3871451.html