autofac

Autofac的AOP面向切面编程研究

老子叫甜甜 提交于 2019-11-30 22:31:04
什么是AOP: 我的理解是 把系统性的编程工作封装起来 =》我给这个取个名字叫 “Aspect”,然后通过AOP技术把它切进我们的业务逻辑代码 =》 “业务“ 这样的好处: “Aspect” 和 “业务” 相互独立,既可以让“业务” 用到了 “Aspect” 又让2者互相独立不耦合,多个“业务”也能复用 同一份“Aspect” 举一个最实用的例子 Transaction 事物 我们经常会在业务代码上使用Transaction事物,比如使用TransactionScope: 下面的测试代码用到了我在维护的一个开源ORM框架 https://github.com/yuzd/AntData.ORM 示例业务代码: 上面的代码 就是 插入一个学校到db 然后拿到主键 赋值给 person 再insert到db。 在同一个事物里面 要么school 和person 同时插入db 要么都失败。 实际业务代码肯定比这个要复杂的多了,比如多个方法在一个事物里面等我这里就举最简单的例子。 那么如果每段业务逻辑都这么写的话 会造成重复性代码很多, 下面我们就尝试用AOP面向对切的思想去优化 思考怎样的方式才是我想要的 示例代码: 如上图: 我在需要用到事物的方法上面打了一个 EnableTransactionScope 标签 这样遇到业务方法需要用事物包裹的话 都可以打上这个标签 假如要实现这样的话

Is it possible to remove an existing registration from Autofac container builder?

懵懂的女人 提交于 2019-11-30 22:29:56
问题 Something along those lines: builder.RegisterType<MyType>().As<IType>(); builder.RegisterType<MyType2>().As<IType>(); builder.DeRegisterType<MyType>().As<IType>() var container = builder.Build(); var types = container.Resolve<IEnumerable<IType>>(); Assert.IsTrue(types.Count == 1); Assert.IsTrue(types[0].GetType == typeof(MyType2)); Scenario: I go through bunch of assemblies and as I go I register types but I want to make sure that I have only one implementation of a given type. I need to do

Using Autofac as a service locator

十年热恋 提交于 2019-11-30 22:12:50
问题 I'm using Autofac to handle dependency injection in my application. However, I have one component that does some reflection magic at runtime and I don't know at compile-time what dependencies it will need. Ordinarily, I would just have this component reference the Container directly and resolve whatever it wants. However, the class that is instantiating this class has no reference to the Container. Effectively, my component has a dependency on Autofac. I'd prefer looser coupling, but that

AutoFac控制反转

吃可爱长大的小学妹 提交于 2019-11-30 21:50:56
一、AutoFac介绍 Autofac是.NET里IOC(Inversion of Control,控制反转)容器的一种,同类的框架还有Spring.NET,Unity,Castle等。可以通过NuGet方式添加到项目中使用。 官方网站:http://autofac.org/ Autofac相对于其它的IoC框架优点: 它是C#语言联系很紧密,也就是说C#里的很多编程方式都可以为Autofac使用,例如可以用Lambda表达式注册组件。 较低的学习曲线,学习它非常的简单,只要你理解了IoC和DI的概念以及在何时需要使用它们。 XML配置支持。 自动装配。 与Asp.Net MVC集成。 微软的Orchad开源程序使用的就是Autofac,从该源码可以看出它的方便和强大。 二、什么是控制反转(IOC)、依赖注入(DI) 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。Michael Mattson在1996年首次提出IoC的概念,IoC把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,而传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类之间高耦合。 依赖注入(Dependency Injection,简称DI),早在2004年,Martin

How to inject dependency name as a constructor parameter

谁都会走 提交于 2019-11-30 21:34:35
Using Autofac, I can register a class to resolve against an interface using property injection, using the following code: builder.RegisterType<Log4NetAdapter>() .As<ILogger>() .PropertiesAutowired() .InstancePerDependency(); However, my Log4NetAdapter class has a constructor parameter that requires the name of the calling class. This way, I can log events based upon the name of the calling class. public class Log4NetAdapter : ILogger { private readonly ILog _logger; public Log4NetAdapter(string logName) { _logger = LogManager.GetLogger(logName); } ... } How can I inject the name (i.e. typeof

ASP.NET MVC 3, RavenDB, & Autofac Issue Plus 2 Other Autofac Questions

我怕爱的太早我们不能终老 提交于 2019-11-30 19:26:54
NOTE: There are 3 questions in here and I did not make separate questions since they are all somewhat related to the same code. I have the following code that registers the connection to my RavenDB in the Application_Start once per the application's life cycle: var store = new DocumentStore { Url = "http://localhost:8080" }; store.Initialize(); builder.RegisterInstance(store).SingleInstance(); Now this works fine and this is something that should be created only once per the application's life cycle. Now I wanted to add in the DocumentSession to Autofac so I tried to add in this in the

AutoFac: Inject NULL values

江枫思渺然 提交于 2019-11-30 19:15:47
I want to use AutoFac to inject the current principal in the objects that need it. Suppose I have an object ( AuthorizationValidator ) that is performing security checks. It looks something like this: public AuthorizationValidator : IAuthorizationValidator { public AuthorizationValidator(IDataAccess dataAccess, IPrincipal principal) { // Save injected objects ... } public bool CheckPermission(Guid objectId, Action action) { // Check if we are authorized at all if (this.principal == null) return false; // Check the permission in the database ... } } For my web application the

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

安稳与你 提交于 2019-11-30 18:33:34
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 about Owned<>, and put "using Autofac.Features.OwnedInstances" in every code file. Func<> has the great

Transitioning to AutofacWebApiDependencyResolver from MVC's DependencyResolver - Where is .Current?

大城市里の小女人 提交于 2019-11-30 18:15:34
问题 I had AutoFac working properly with MVC4. I'm trying to transition to Web API 2. Here's what I've got for setting up AutoFac: public class AutofacRegistrations { public static void RegisterAndSetResolver() { // Create the container builder. var containerBuilder = new ContainerBuilder(); // Register the Web API controllers. containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // Only generate one SessionFactory ever because it is expensive. containerBuilder.Register(x =>

How to inject dependency name as a constructor parameter

这一生的挚爱 提交于 2019-11-30 17:07:45
问题 Using Autofac, I can register a class to resolve against an interface using property injection, using the following code: builder.RegisterType<Log4NetAdapter>() .As<ILogger>() .PropertiesAutowired() .InstancePerDependency(); However, my Log4NetAdapter class has a constructor parameter that requires the name of the calling class. This way, I can log events based upon the name of the calling class. public class Log4NetAdapter : ILogger { private readonly ILog _logger; public Log4NetAdapter