autofac

autofac笔记

寵の児 提交于 2020-02-11 21:22:34
/// <summary> /// 数据源操作接口 /// </summary> public interface IDataSource { /// <summary> /// 获取数据 /// </summary> /// <returns></returns> string GetData(); } public class Oracle : IDataSource { public string GetData() { return "通过Oracle获取数据"; } } class Sqlserver : IDataSource { public string GetData() { return "通过SQLSERVER获取数据"; } } /// <summary> /// 数据源管理类 /// </summary public class DataSourceManager { IDataSource _ds; string Name; /// <summary> /// 根据传入的类型动态创建对象 /// </summary> /// <param name="ds"></param> public DataSourceManager(string name, IDataSource ds) { _ds = ds; Name = name; } public

abp vnext2.0核心组件之模块加载组件源码解析

爱⌒轻易说出口 提交于 2020-02-08 20:23:34
老版Abp对Castle的严重依赖在vnext中已经得到了解决,vnext中DI容器可以任意更换,为了实现这个功能,底层架构相较于老版abp,可以说是进行了高度重构.当然这得益于.Net Core的DI容器组件本身的优势.接着 abp vnext2.0核心组件之模块加载组件源码解析 上文,上文中我跳过了DI切换这个流程,因为我觉得这是整个框架的亮点之一,所以单独写了这篇随笔. .Net Core2.2之后,切换DI的实现换了,改成实现如下接口 1、核心ServiceProviderFactory接口实现 核心原理非常的简单,看看vnext如何实现这个工厂约束接口,并返回指定provider. 大致的逻辑是传入ServiceCollection,遍历ServiceCollection使用autofac的containerbuilder进行注入.最后调用containerbuilder实例的builder的build方法,返回provider.一气呵成,很简单. 接着,重点来了,看看Populate方法,看看其如何将ServiceCollection中的类型注入autofac容器的. 先将原生DI的相关功能转换成Autofac的,接着进行类型注册. 第一步获取模块加载类型中所有的模块,模块信息如下: 第二步释出DI容器中的ServiceRegistrationActionList

XUnit Test different results under netcoreapp1.1 and net462

被刻印的时光 ゝ 提交于 2020-02-08 06:09:35
问题 I created a XUnit test projects in VS 2017, its target framework is netcoreapp1.1, and below code works correctly. using Xunit; using Xunit.Abstractions; using Xunit.Ioc.Autofac; namespace XUnitTestProject2 { [UseAutofacTestFramework] public class MyAwesomeTests { public MyAwesomeTests() { } public MyAwesomeTests(ITestOutputHelper outputHelper) { _outputHelper = outputHelper; } [Fact] public void AssertThatWeDoStuff() { _outputHelper.WriteLine("Hello"); } private readonly ITestOutputHelper

Asp.Net Core 3.1 使用Autofac Aop

此生再无相见时 提交于 2020-02-07 16:13:50
跟上一篇的文章有点类似,这篇文章是在AspNetCore 3.1中使用,我们定义一个特性 定义拦截器。。。 定义一个接口一个类。模拟Service层。Autofac提示,用于Aop拦截器的调用的方法需要时virtual虚方法 在项目中,增加一个WebModule类,继承Autofac的Module。在WebModule中,注册使用拦截器 Program改为下图,使用Autofac 服务提供工厂。 Startup类改为 ConfigureContainer类是Autofac自动调用。 在控制器中调用Service的SayHi方法。 运行就可以看到结果。。。 来源: https://www.cnblogs.com/dazen/p/12273018.html

DependencyResolver: Pass parameters

允我心安 提交于 2020-02-07 07:50:11
问题 I use autofac and can pass parameters to my resolve method. How can I do this using microsofts DependencyResolver interface? 回答1: The IDependencyResolver does not support passing parameters directly, as I'm sure you have noticed. However, since you have Autofac under the hood, you're able to resolve a factory delegate that enables you to pass on parameters to the underlying service: var factory = dependencyResolver.GetService<Func<int, string, IService>>(); var service = factory(5, "42");

DependencyResolver: Pass parameters

允我心安 提交于 2020-02-07 07:50:03
问题 I use autofac and can pass parameters to my resolve method. How can I do this using microsofts DependencyResolver interface? 回答1: The IDependencyResolver does not support passing parameters directly, as I'm sure you have noticed. However, since you have Autofac under the hood, you're able to resolve a factory delegate that enables you to pass on parameters to the underlying service: var factory = dependencyResolver.GetService<Func<int, string, IService>>(); var service = factory(5, "42");

autofac, ASP.NET integration, and Dispose

谁说胖子不能爱 提交于 2020-02-05 07:37:07
问题 Autofac newbie here, but I like what I see so far. I'm trying to take advantage of request-lifetime of my resolved objects and I'm having trouble confirming that a dispose is actually happening after a request is done. I have a disposable object that I get at the start of a page request and dispose at the end. I'm using autofac to get an instance of the object now and I wanted to see if autofac would do the disposing for me. i've instrumented the Dispose() method on the object in question,

How to set DefaultBinder ModelBinders.Binders.DefaultBinder in asp.net mvc 4 using autofac?

偶尔善良 提交于 2020-02-05 05:39:11
问题 I want to set ModelBinders.Binders.DefaultBinder=new SmartBinder() in asp.net mvc 4 using autofac, what is the right way to do that? 回答1: In your Global.asax.cs; protected void Application_Start() { System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new SmartBinder(); } Autofac is not needed to do that. 来源: https://stackoverflow.com/questions/18286948/how-to-set-defaultbinder-modelbinders-binders-defaultbinder-in-asp-net-mvc-4-usi

Would like Autofac to not register any interface that has more than one implementation

别说谁变了你拦得住时间么 提交于 2020-02-03 10:10:33
问题 I'm currently testing Autofac for our company. We'd like to have the following rules: If an interface has been implemented only once, then add it automatically using the builder.RegisterAssemblyTypes (see below). Otherwise, we need to make sure to manually write the rule that will decide which implementation is the 'default' implementation. I have the following code: var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly .Load("Lunch.Service")).As(t => t.GetInterfaces()

Would like Autofac to not register any interface that has more than one implementation

偶尔善良 提交于 2020-02-03 10:10:27
问题 I'm currently testing Autofac for our company. We'd like to have the following rules: If an interface has been implemented only once, then add it automatically using the builder.RegisterAssemblyTypes (see below). Otherwise, we need to make sure to manually write the rule that will decide which implementation is the 'default' implementation. I have the following code: var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly .Load("Lunch.Service")).As(t => t.GetInterfaces()