autofac

Is it bad design to reference Autofac in my projects just for Owned<T>?

半城伤御伤魂 提交于 2019-11-30 03:20:10
问题 I've recently become a heavy user of Autofac's OwnedInstances feature. For example, I use it to provide a factory for creating a Unit of Work for my database, which means my classes which depend on the UnitOfWork factory are asking for objects of type : Func<Owned<IUnitOfWork>> This is incredibly useful--great for keeping IDisposable out of my interfaces--but it comes with a price: since Owned<> is part of the Autofac assembly, I have to reference Autofac in each of my projects that knows

Autofac - Lifetime and modules

余生长醉 提交于 2019-11-30 03:17:48
问题 Problem (abstract) Given a module which registers dependency X. The dependency X has a different lifetime in a MVC3 app (lifetime per HttpRequest) then in a console application (dependency per lifetimescope with a name). Where or how to specify the lifetime of dependency X? Case I've put all my database related code in a assembly with a module in it which registers all repositories. Now the ISession (Nhibernate) registration is also in the module. ISession is dependency X (in the given

How to configure fluent nHibernate with MySQL

别说谁变了你拦得住时间么 提交于 2019-11-30 03:16:00
I'm trying to configure nHibernate to use a MySql database. I found examples for mssql and sqlite but none for mysql. So, how do I change this so it uses mysql: Fluently.Configure().Database( MsSqlConfiguration.MsSql2005.ConnectionString( c => c.FromConnectionStringWithKey("ConnectionString") ) ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>()) .BuildSessionFactory()) Change MsSqlConfiguration.MsSql2005 , to MySqlConfiguration.Standard , it was the one thing I contributed to the project. Example: Fluently.Configure().Database( MySqlConfiguration.Standard.ConnectionString(

用Autofac替换.net core 内置容器

房东的猫 提交于 2019-11-30 02:49:41
官方建议使用内置容器,但有些功能并不支持,如下: 属性注入 基于名称的注入 子容器 自定义生存期管理 Func<T> 支持 所以可以使用其他第三方IOC容器,如Autofac,下面为学习使用记录 一、首先准备了一个接口和其实现类 public interface ITestService { string ShowMsg(); } public class TestService: ITestService { public string ShowMsg() { return "test123"; } } 二、安装Nuget 包 Autofac Autofac.Extensions.DependencyInjection 三、在 Startup.ConfigureServices 中配置容器 注:使用第三方容器,Startup.ConfigureServices 必须返回 IServiceProvider。    第一种方式,使用AutofacModule配置文件,原来代码修改为: public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //

Autofac - how to resolve Func for ISomething from Singleton where ISomething is InstancePerHttpRequest

亡梦爱人 提交于 2019-11-30 02:31:34
I'm trying to use Autofac to inject dependencies into FluentValidation in an MVC 4 app. I think I've got the strategy worked out, but I'm getting stuck with resolving my per-request ISomething from a singleton. Here's the scenario: I've got a validator that derives from FluentValidation's AbstractValidator. I've read that FluentValidation validators perform best as singletons, so my constructor expects a Func and stores that Factory for use later. When the validator is used, it should ask the stored factory for an IDataStore, get the instance created for that request and use it. That's the

Hangfire dependency injection lifetime scope

妖精的绣舞 提交于 2019-11-30 01:51:19
I'm rewriting this entire question because I realize the cause, but still need a solution: I have a recurring job in Hangfire that runs every minute and check the database, possibly updates some stuff, then exits. I inject my dbcontext into the class containing the job method. I register this dbcontext to get injected using the following builder.RegisterType<ApplicationDbContext>().As<ApplicationDbContext>().InstancePerLifetimeScope(); However, it seems that Hangfire does not create a seperate lifetime scope every time the job runs, because the constructor only gets called once, although the

.Net IOC框架入门之——Autofac

馋奶兔 提交于 2019-11-30 01:24:26
一、简介 Autofac是.NET领域最为流行的IOC框架之一,传说是速度最快的一个 目的 1.依赖注入的目的是为了解耦。 2.不依赖于具体类,而依赖抽象类或者接口,这叫依赖倒置。 3.控制反转即IoC (Inversion of Control),它把传统上由程序代码直接操控的对象的调用权交给容器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转移,从程序代码本身转移到了外部容器。 4. 微软的DependencyResolver如何创建controller 生命周期 1、InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例。这也是默认的创建实例的方式。官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.) 2、InstancePerLifetimeScope 在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个不同的生命周期域,实例是唯一的,不共享的。官方文档解释:Configure the component so that every dependent component or call to

Replace factory with AutoFac

最后都变了- 提交于 2019-11-29 23:10:37
I'm accustomed to creating my own factories as shown (this is simplified for illustration): public class ElementFactory { public IElement Create(IHtml dom) { switch (dom.ElementType) { case "table": return new TableElement(dom); case "div": return new DivElement(dom); case "span": return new SpanElement(dom); } return new PassthroughElement(dom); } } I'm finally getting around to using an IoC container (AutoFac) in my current project, and I'm wondering is there some magic way of achieving the same thing elegantly with AutoFac? Short answer: Yes. Longer answer: First, in simple cases where a

Managing NHibernate ISession with Autofac

谁说我不能喝 提交于 2019-11-29 22:29:27
Does anyone have any tips or best practices regarding how Autofac can help manage the NHibernate ISession Instance (in the case of an ASP.NET MVC application)? Peter Lillevold I'm not overly familiar with how NHibernate sessions should be handled. That said, Autofac have excellent instance lifetime handling ( scoping and deterministic disposal ). Some related resources are this article and this question . Since you're in ASP.Net MVC land make sure you also look into the MVC integration stuff . To illustrate the point, here's a quick sample on how you can use Autofac factory delegates and the

IOCAutofac与ORMEntityFramwork的联系--单例模式

我只是一个虾纸丫 提交于 2019-11-29 21:56:34
在你阅读之前默认你已经理解了IOC、DI、ORM以及autofac和EF的使用 在我最近写项目的时候我在单步调试时偶然发现的一个问题 先说明我的项目使用.NET MVC 三层架构,运用IOC Autofac工具和EntityFramework6 对于IOC Autofac不理解的可以去看我的博客: https://www.cnblogs.com/sandaman2019/p/11273366.html 对于EntityFramework6不理解的可以去看这个文章: https://www.cnblogs.com/wujingtao/category/816531.html 在使用IOC的时候进入单步调试项目时,IOC注册进容器时会获取实体类中的db上下文对象 可以看到我的_dbcontext是由一个dbcontext单例模式创建的! 那么为什么要用这个?在IOC执行时会扫描你对应的注册接口和实现类,实现类中的对于实体的操作,就需要dbcontext上下文对象 那么这个上下文对象怎么找,首先他会找到你的实体类,找到对应的实体模型类去检索上下文独享,确保有一个dbcontext对象可以去执行后续的操作 这就是对EF的请求操作,如果不通过工厂去实现就会造成同一次请求可能包含对数据库的不同操作,其他的EF对象内获取的数据可能已经是过时的, 如果不保证同一个请求中使用同一个EF上下文对象