autofac

MVC Web API not working with Autofac Integration

匿名 (未验证) 提交于 2019-12-03 00:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I used the MVC integration from autofac like this: ... var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); But now I want to recreate the solution with the new Web Api RTM. And I want to use the new AutofacWebApiDependencyResolver class. But if I do this with AutofacWebApiDependencyResolver i got this error: The type Autofac.Integration.WebApi.AutofacWebApiDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. I read that i have to do this

C# 读Autofac源码笔记(1)

匿名 (未验证) 提交于 2019-12-03 00:40:02
最近在看Autofac的源码。 Autofac据说是.net中最快的IOC框架,具体没有实验,于是看看Autofac具体是怎样实例化实体。 image.png 如上图所示,Autofac使用的是表达式树来创建实体。 Expression Tree创建实体的性能比Activator.CreateInstance要高。 网上有人做过测试,这里我将链接贴在此 Activator.CreateInstance与ExpressionTree创建实体性能对比 我们再来看看,autofac中构造函数注入是如何实现的。 看下面这张图片 image.png 这段代码是生成实体前,先根据实体类型获取构造函数参数,并尝试获取参数的值。那参数的值是怎么获取的呢。再看下面这张图: image.png 这个方法,是根据参数类型,到注册的组件集合中获取类型相同的实体。最终还是用上面的表达式树,将参数传入实体中。 未完待续... 原文:https://www.cnblogs.com/czly/p/9283403.html

How to organize MVP with an IoC container?

旧巷老猫 提交于 2019-12-03 00:35:39
I'm trying to get the IoC concept down with a winforms app. Say I have a presenter whose constructor takes its view and a service as constructor arguments. So in the form code I have something that amounts to this: mnPresenter = new Presenter(this, new AppService()); where, say AppService is an implementation of IAppService. It's registered in my [autofac] IoC container. What's the recommended way of getting the "new" out of this presenter construction? Isn't the whole point of using an IoC framework to lose these "new" calls like I'm making above? I could do something like mPresenter = new

JavaScript DI/IoC equivalents to standard DI patterns for statically typed languages

旧街凉风 提交于 2019-12-03 00:29:17
.NET and Java both have a slew of DI/IoC containers available to them and each have a number of patterns that I've found very useful at various points in working with them. I'm now at a point where I would like to do equivalent things in JavaScript. As JavaScript is a dynamic language, I don't expect DI/IoC containers to have direct equivalents to all the functionality provided by the containers found in statically typed languages so alternatives to these patterns are welcome. I also expect that the DI/IoC containers available in JavaScript will vary in their functionality, so references to

AutoFac 入门 简单使用

匿名 (未验证) 提交于 2019-12-03 00:27:02
第一步:添加引用 第二步:在运行程序下添加一个类,mvc应添加在App_Start文件夹中. 并创建一个静态方法,例: } 第三步:在BLL层,Dal层分别添加一个Module类 //BLL层 创建一个类继承自Module,并重写Load方法 protected override void Load(ContainerBuilder builder){ //此处调用Dal中Module } //DAL层 创建一个类继承自Module,并重写Load方法 } 第四步:修改项目中对bll,dal,db数据上下文对象的使用 //UI层,调用BLL层修改 private readonly IBLL<DTOSupplierInfo> _supplierInfoBll; _supplierInfoBll = supplierInfoBll; _supplierTypeBll = supplierTypeBll; } //BLL层,调用DAL层修改 //SupplierInfoBLL public SupplierInfoBLL(IDAL<SupplierInfo> supplierInfoDal){ } //SupplierTypeBLL public SupplierTypeBLL(IDAL<SupplierType> supplierTypeDal){ } //DAL层,调用DB对象修改

Autofac Multi-tenant IoC Container in an ASP.NET Web API Application

风流意气都作罢 提交于 2019-12-03 00:22:28
Autofac 3.0 will have a MultitenantIntegration support and its preview release is out now. To try it out, I created an ASP.NET Web API application with the following configuration: public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { var config = GlobalConfiguration.Configuration; config.Routes.MapHttpRoute("Default", "api/{controller}"); RegisterDependencies(config); } public void RegisterDependencies(HttpConfiguration config) { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //

ABPvnext源码分析 (二):Autofac整合及动态代理

匿名 (未验证) 提交于 2019-12-03 00:17:01
写在前面: 上一篇我们讲了Abp的核心模块,这一篇我们把DI 的serviceProvider替换成Autofac,另外添加动态代理拦截器功能。动态代理指从DI容器获取组件实例时组件实例不是原实例,而是代理实例。代理实例是对原实例进行了封装, 在实例方法前后添加逻辑处理,让获取的对象表现基于应有对象但又有自己的逻辑。举个例子,代理对象方法可以在原方法前后记录时间戳,来分析原方法的处理时长。Abp Core默认使用的是微软官方的DI实现, 那个功能较简单没有代理功能,为了添加动态代理功能,我们把DI实现替换为Autofac,并使用Autofac集成的代理实现方式Castle Core。 集成Autofac,我们需要添加Volo.Abp.Autofac包,这个包本身是Abp模块,其依赖AbpCastleCoreModule模块(Volo.Abp.CatleCore) namespace Volo.Abp.Autofac { [DependsOn(typeof(AbpCastleCoreModule))] public class AbpAutofacModule : AbpModule { } } 所以我们添加Volo.Abp.Autofac包并让我们的应用模块DependsOn(typeof(AbpAutofacModule))就自动的把AbpAutofacModule

Register RavenDb using Autofac?

∥☆過路亽.° 提交于 2019-12-03 00:12:22
Can anyone guide me on how I could register RavenDB using Autofac? builder.Register<DocumentStore>( .. what after that? Matt Johnson Here is a sample console program that illustrates not only how to wire up the document store, but also how to set it up so you can just inject your document session: using System.Threading.Tasks; using Autofac; using Raven.Client; using Raven.Client.Document; namespace ConsoleApplication1 { internal class Program { private static void Main() { var builder = new ContainerBuilder(); // Register the document store as single instance, // initializing it on first use.

ASP.NET MVC IOC 之 Autofac(二)

匿名 (未验证) 提交于 2019-12-02 23:38:02
在上一章节,我们已经知道了再控制器中如何注入以及使用了。这一章,我们重点讲解下,如何在服务层中使用。 我们新定义一个教师类,在服务层中,通过这个教师类服务层,获取学生的年龄。实现在教师类的服务层中调用了学生类的服务接口对象。 新建一个教师类接口:ITeacherService.cs namespace AutoFacTest.Service { public interface ITeacherService { int GetStudentAge(string name); } } 接着定义一个 教师实现类,TeacherService.cs public class TeacherService:ITeacherService { /// <summary> /// 定义学生接口服务类 /// </summary> private readonly IStudentService _stu; /// <summary> /// 实现构造函数注入 /// </summary> /// <param name="stu"></param> public TeacherService(IStudentService stu) { _stu = stu; } /// <summary> /// 调用学生类,获取学生信息 /// </summary> /// <param name=

asp.net core3.0 mvc 用 autofac

匿名 (未验证) 提交于 2019-12-02 22:09:29
好久没有写文章了,最近在用.net core3.0,一些开发中问题顺便记录; 1.首先nuget引入 Autofac Autofac.Extensions.DependencyInjection 2.修改Program.cs 添加.UseServiceProviderFactory(new AutofacServiceProviderFactory()) public class Program { public static void Main ( string [] args ) { CreateHostBuilder ( args ). Build (). Run (); } public static IHostBuilder CreateHostBuilder ( string [] args ) => Host . CreateDefaultBuilder ( args ) . UseServiceProviderFactory ( new AutofacServiceProviderFactory ()) . ConfigureWebHostDefaults ( webBuilder => { webBuilder . UseStartup < Startup >(); }); } 3.修改Startup.cs 添加 services