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 ...
随机推荐
- 【Android 开发】: Android 消息处理机制之一: Handler 与 Message
最近几讲内容,我们学习了Android中关于多线程的一些知识,上一讲我们讲解了异步任务 AsyncTask 的操作,Android中还提供了其他的线程操作,如Handler Message Messa ...
- Intent和Intent Filters
什么是Intent Intent是android开发中的重要对象,它作为一个信息承载对象存在. 我们可以在使用其他一些组件的时候从Intent获取行为响应的准则(即应该做什么东西,如何 ...
- 各组对final发布产品的排名
结果 排名 组名 项目简称 组长 平均 方差 1 新蜂 俄罗斯 武志远 2 0.80 2 天天向上 连连看 王森 2.50 1.90 3 奋斗吧兄弟 食物链 黄兴 2.83 0.97 4 金洲勇士 考 ...
- Android Studio2.0 教程MAC版 -快捷键篇
本文转至 Android Studio2.0 教程从入门到精通MAC版 - 提高篇 ( OPEN 开发经验库) 第二篇我们开发了一个Hello World应用,并介绍Android Sutdio的界面 ...
- [转]oracle数据类型和对应的java类型
地址: http://otndnld.oracle.co.jp/document/products/oracle10g/102/doc_cd/java.102/B19275-03/datacc.htm ...
- Swift基础--可选绑定和守护绑定
Swift中的可选绑定和守护绑定 1.可选绑定 格式 // 通过url来创建request对象 if let tempUrl = url { // url为可选类型,当可选类型有值,才执行大括号里面的 ...
- 【UR #2】树上GCD
这道题是有根树点分治+烧脑的容斥+神奇的分块 因为是规定1为根,还要求LCA,所以我们不能像在无根树上那样随便浪了,必须规定父亲,并作特殊讨论 因为gcd并不好求,所以我们用容斥转化一下,求x为gcd ...
- 工具软件发现(编写chm 文件的工具)
编写chm 文件的工具 1.PrecisionHelper 安装之后,发现 编写的很不方便,直接在html 上编写-- 不好用 2.Winchm (推荐) 很好用,赞!至少对比了上面那个复杂的操作之后 ...
- C#-WinForm-如何获取文本框(TextBox)中鼠标,光标位置
文本框(TextBox)中的鼠标位置和光标位置是两个不同的概念,鼠标位置是要点击鼠标后(NouseDown)获取到,而光标位置却是实时就要获取到,也就是用户输入一个字符(KeyUp),这个位置就要改变 ...
- OPRNGL总结(一)OPENGL的理论原理
1.计算机图形学的发展——走向3D 1.纸带 2.荧光屏,打印出*等等 3.阴极射线管 4实时3D 2.实现“3D”的原理 1.其实现在看到的3D都是伪3D,并不是真正的三维图像,真正的三维图像 真3 ...