autofac 用法总结
autofac官网:
http://autofaccn.readthedocs.io/en/latest/getting-started/index.html
autofac作为一个热门ioc框架,还是有了解一下的必要的。这里用两个小案例来简单说说autofac在.net framework控制台应用程序和asp.net mvc5 项目中的使用。
先从控制台应用程序开始。
首先nuget安装autofac,新建IDemo接口
namespace AutuFacDemo
{
interface IDemo
{
string Hello();
}
}
新建Demo类实现IDemo接口:
namespace AutuFacDemo
{
public class Demo :IDemo
{
public string Hello()
{
return "hello";
}
}
}
Program.cs:
using Autofac;
using System; namespace AutuFacDemo
{
class Program
{
private static IContainer Container { set; get; } static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<Demo>().As<IDemo>();
Container = builder.Build(); using (var scope = Container.BeginLifetimeScope())
{
var demo = scope.Resolve<IDemo>();
Console.WriteLine(demo.Hello());
Console.ReadKey();
}
}
}
}
这样就完成了一个最简单的控制台Demo。
现在开始使用mvc5案例。
nuget安装Autofac.Mvc5
同一解决方案内新建IBLL和BLL类库,IBLL存放IDemo.cs:
namespace IBLL
{
public interface IDemo
{
string Hello();
}
}
BLL存放Demo.cs
using IBLL; namespace BLL
{
public class Demo :IDemo
{
public string Hello()
{
return "hello";
}
}
}
Global.asax.cs配置autofac:
using Autofac;
using Autofac.Integration.Mvc;
using BLL;
using IBLL;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing; namespace WebDemo
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(WebApiApplication).Assembly); builder.RegisterType<Demo>().As<IDemo>(); var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
控制器配置Demo方法:
using IBLL;
using System.Web.Mvc; namespace WebDemo.Controllers
{
public class HomeController : Controller
{ private IDemo _demo; public HomeController(IDemo demo)
{
this._demo = demo;
} public ActionResult Index()
{
ViewBag.Title = "Home Page"; return View();
} public ActionResult Demo()
{
string hello = _demo.Hello();
return Content(hello);
}
}
}
运行后访问Demo方法,即可看到效果。
autofac 用法总结的更多相关文章
- IoC容器Autofac - Autofac + Asp.net MVC + EF Code First(转载)
转载地址:http://www.cnblogs.com/JustRun1983/archive/2013/03/28/2981645.html 有修改 Autofac通过Controller默认构造 ...
- IOC容器 - Autofac概述
Autofac是比较出名的Ioc容器之一,熟悉Orchard的应该熟知.本文直接介绍autofac用法 一.开始 1.NuGet添加或者直接http://code.google.com/p/autof ...
- 【半小时大话.net依赖注入】(下)详解AutoFac+实战Mvc、Api以及.NET Core的依赖注入
系列目录 上|理论基础+实战控制台程序实现AutoFac注入 下|详解AutoFac+实战Mvc.Api以及.NET Core的依赖注入 前言 本来计划是五篇文章的,每章发个半小时随便翻翻就能懂,但是 ...
- Autofac高级用法之动态代理
前言 Autofac的DynamicProxy来自老牌的Castle项目.DynamicProxy(以下称为动态代理)起作用主要是为我们的类生成一个代理类,这个代理类可以在我们调用原本类的方法之前,调 ...
- 第二节:框架前期准备篇之AutoFac常见用法总结
一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势必会引起一点点小小的风波,我说我的好,他说他的好,非常容 ...
- 转载 AutoFac常见用法总结
第二节:框架前期准备篇之AutoFac常见用法总结 一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势 ...
- 以英雄联盟的方式建模,谈对依赖注入(DI)的理解以及Autofac的用法(一)
一.前言 近期在探索分层架构和架构设计,选择了领域驱动作为5年.Net开发后的新的方向,不可避免的接触了IoC/DI方面的技术.目前通过反射或其他方法都已实现,但只知其一,并没有考虑为什么要这么做,同 ...
- 【转】Autofac高级用法之动态代理
原文:http://www.cnblogs.com/stulzq/p/8547839.html 前言 Autofac的DynamicProxy来自老牌的Castle项目.DynamicProxy(以下 ...
- 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程
反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) 背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...
随机推荐
- C++从键盘读入数组并存储
C++从键盘读取任意长度的数组,现总结如下: //读取指定长度的数组 int main() { int n = 0; cin >> n; vector<int> p(n); f ...
- Uva:11401-Triangle Counting
Triangle Counting Time limit1000 ms Description You are given n rods of length 1, 2-, n. You have to ...
- Git add命令
git add -A和 git add . git add -u在功能上看似很相近,但还是存在一点差别 git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文 ...
- python os模块进程函数
Table of Contents 1. 系统进程 2. 举例 2.1. os.fork 2.2. os.exec 和 os.system 2.3. os.wait 3. 总结 系统进程 今天在看&l ...
- Maya
建立酒杯的方法(CV曲线) surface(曲面)-- creat cv curve tool-- control vertex(调整图形)[再次creat cv建立厚度,只需要建立酒杯的上口]--- ...
- 【Keepalived+MySQL】MySQL双主互备+高可用
一.基本信息说明 [DB1] IP: 192.168.102.144 hostname: LVS-Real1 [DB2] IP: 192.168.102.145 hostname: LVS-Real2 ...
- 免费生成https证书以及配置
http升级到https需要在nginx的配置中加入证书信息,查询资料后确定生成证书两种方案 第一种:自签名证书,然后开启 CloudFlare 的 CDN 服务 //确定是否安装openss ...
- leetcode 【 Merge Sorted Array 】python 实现
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...
- Python subprocess.Popen中communicate()和wait()区别
刚开始我是使用的wait(),但是当adb命令返回太多时,程序就会卡死,查询得知原因后,才使用了communicate(),communicate()返回一个元组:(stdoutdata, stder ...
- tinyipa make
参考:http://tinycorelinux.net/ Ironic Python Agent repo还提供了一组脚本,用于在imagebuild / tinyipa文件夹下构建一个基于Linux ...