MVC Autofac构造函数注入
建立 空的 MVC4项目
首先引用 NuGet 里 autofac 和 autofac .integration. mvc
然后 建立Model
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
public interface IPersonRepository
{
IEnumerable GetAll();
Person Get(int id);
Person Add(Person item);
bool Update(Person item);
bool Delete(int id);
}
public class PersonRepository : IPersonRepository
{
List person = new List();
public PersonRepository()
{
Add(new Person { Id = 1, Name = "joye.net1", Age = 18, Address = "中国上海" });
Add(new Person { Id = 2, Name = "joye.net2", Age = 18, Address = "中国上海" });
Add(new Person { Id = 3, Name = "joye.net3", Age = 18, Address = "中国上海" });
}
public IEnumerable GetAll()
{
return person;
}
public Person Get(int id)
{
return person.Find(p => p.Id == id);
}
public Person Add(Person item)
{
if (item == null) { throw new ArgumentNullException("item"); }
person.Add(item);
return item;
}
public bool Update(Person item)
{
if (item == null) { throw new ArgumentNullException("item"); }
int index = person.FindIndex(p => p.Id == item.Id);
if (index == -1)
{
return false;
}
person.RemoveAt(index);
person.Add(item); return true;
}
public bool Delete(int id)
{
person.RemoveAll(p => p.Id == id);
return true;
}
}
再新建 HomeController 与相应的 Index.cshtml 在HomeController 声明 属性及构造函数
public class HomeController : Controller
{
public IPersonRepository _personRepository { get; set; }
public HomeController(IPersonRepository personRepository)
{
_personRepository = personRepository;
}
public ActionResult Index()
{
var item = _personRepository.Get(1);
return View();
}
}
最后在global.asax.cs 的Application_Start() 里实现注入
using Autofac;
using Autofac.Integration.Mvc;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterType<PersonRepository>().As<IPersonRepository>(); //注册接口服务
builder.RegisterControllers(Assembly.GetExecutingAssembly());//构造函数注入
//builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();//属性注入
var container = builder.Build();//构建
System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));//开始注入
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
MVC Autofac构造函数注入的更多相关文章
- ASP.NET MVC Autofac自动注入
依赖注入容器有很多插件,我用过Unity和Autofac,这两个插件给我最明显的感觉就是Autofac很快,非常的快,毕竟是第三方开发的,而Unity相对而言性能比较稳定 下面附上Autofac自动注 ...
- Mvc Autofac构造器注入
新建MVC项目,添加程序集引用 定义接口ILog public interface ILog { string Save(string message); } 类TxtLog实现接口ILog publ ...
- ASP.NET MVC Autofac依赖注入的一点小心得(包含特性注入)
前言 IOC的重要性 大家都清楚..便利也都知道..新的ASP.NET Core也大量使用了这种手法.. 一直憋着没写ASP.NET Core的文章..还是怕误导大家.. 今天这篇也不是讲Core的 ...
- MVC Autofac依赖注入
通过Dll实现全部类的属性注入,该演示实例主要通过多层架构中单一的对象方式来演示,没有采取接口的方式, 新建AutoFacHelper类,如下代码: public class AutoFacHelpe ...
- MVC autofac 属性注入
Global文件 public class MvcApplication : System.Web.HttpApplication { private static IContainer Contai ...
- Autofac 的构造函数注入方式
介绍 该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入.注入方式通过构造函数. 在编写 aufofac 的依赖注入 ...
- ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中应用
上一章主要介绍了Autofac在MVC当中的具体应用,本章将继续简单的介绍下Autofac在普通的WebForm当中的使用. PS:目前本人还不知道WebForm页面的构造函数要如何注入,以下在Web ...
- ASP.NET MVC IOC依赖注入之Autofac系列(一)- MVC当中应用
话不多说,直入主题看我们的解决方案结构: 分别对上面的工程进行简单的说明: 1.TianYa.DotNetShare.Model:为demo的实体层 2.TianYa.DotNetShare.Repo ...
- Autofac Getting Started(默认的构造函数注入)
https://autofaccn.readthedocs.io/en/latest/getting-started/index.html The basic pattern for integrat ...
随机推荐
- PHP自学链接收藏
PHP之道 laravist Sublime Text 3
- Extjs 使用图标字体来美化按钮)
1. 使用Font Awesome,下载地址http://www.bootcss.com/p/font-awesome/#icons-new 2. 把font和css目录放到 Ext的app目录下面 ...
- [转]js中的时间与毫秒数互相转换
原文地址:http://blog.sina.com.cn/s/blog_77cb836301015icr.html [1]js毫秒时间转换成日期时间 var oldTime = (new Date ...
- Swift开发小技巧--自定义转场动画
自定义转场动画 个人理解为重写了被弹出控制器的modal样式,根据自己的样式来显示modal出来的控制器 例:presentViewController(aVC, animated: true, co ...
- Struts2总结
1.构建Struts2的开发环境? 1.1:导入相应jar包(最简化的,后期可以直接复制). 1.2:编写struts.xml配置文件.(从实例中提供参考). <package name=&qu ...
- git 冲突解决
冲突文件的组成 "<<< HEAD"和 "====="之间的为主干内容 "=====" 和 ">>& ...
- C#-WinForm-菜单和工具栏
通用属性: Enabled - 指示是否启用该控件. Visiable - 确定该控件是启用还是隐藏的. Checked - 指示组件是否处于选中状态. 点击事件. 工具箱→菜单和工具栏 1.Cont ...
- vim快捷键总结
直接上图 原图地址:vim快捷键
- 【BZOJ-3712】Fiolki LCA + 倍增 (idea题)
3712: [PA2014]Fiolki Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 303 Solved: 67[Submit][Status] ...
- 【bzoj2104】 K-th Number
http://poj.org/problem?id=2104 (题目链接) 题意 求区间第k大数. Solution1 主席树裸题. 主席树当时我学是学的要死,那个时候不晓得百度出什么bug了,搜个主 ...