Autofac:注册异步工厂方法

编程入门 行业动态 更新时间:2024-10-27 06:20:22
本文介绍了Autofac:注册异步工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

TL; DR:Autofac是否支持类似AutoFixture的fixture.Get()机制?

TL;DR: Does Autofac support something like AutoFixture's fixture.Get() mechanism ?

我正在使用Autofac,需要调用如下所示的异步工厂方法:

I'm using Autofac and need to invoke async factory methods which look like this:

class AppModel { public static async Task<AppModel> CreateAsync(IDependency x, IDependency2 y) { ... } }

执行这样的方法并由Autofac提供参数的最简单方法是什么?即,我希望能够执行以下操作:

What is the simplest way for me to execute such a method and have the arguments be supplied by Autofac? i.e., I want to be able to do something like:

Task<AppModel> creationTask = <some autofaccery>(AppModel.CreateAsync); var appModel = await creationTask();

其中,<some autofaccery>表示与ContainerBuilder和/或IContainer进行交互的某种机制和/或某种形式的生成的委托或类似的委托,本质上是简洁的,使我不必显式地指定Factory方法的参数.也就是说,我想避免像atm那样显式地解析每个参数[和/或必须在依赖项更改时更新它们]

where <some autofaccery> represents some mechanism of interacting with ContainerBuilder and/or IContainer and/or some form of generated Delegates or similar which is succinct in nature and isolates me from specifying the arguments to the Factory Method explicitly. i.e., I want to avoid having to explicitly resolve each argument [and/or have to update them as the dependencies change] like I do atm:

var appModel = await AppModel.CreateAsync( container.Resolve<IDependency>(), container.Resolve<IDependency2>());

我在基础设施组件区域内,靠近合成根",可以潜在地以编程方式定义组件注册和/或做其他仅局限于此的事情.我不介意反射被涉及,因为它仅被调用一次.

I am in infrastructure components territory, close to the Composition Root and could potentially programmatically define Component Registrations and/or do other nastiness that should be confined to there. I don't mind reflection being involved as it's only being invoked once.

关键是我确实需要观察来自Task的任何Exception.

What is critical is that I do need any Exceptions emanating from the Task to be observed.

Task<T>在很大程度上是一条红色鲱鱼,但要点是,遵循定义同步工厂方法并通过Autofac正常工作的正常模式不会成功(至少不是直接实现),即不能只是将其更改为:

The Task<T> is a red herring to a large degree, but the point is that following the normal pattern of defining a synchronous factory method and having Autofac work through that won't fly (at least not directly), i.e. I can't just change it to:

public static AppModel CreateAsync(IDependency x, IDependency2 y) { ... }

我还想避免两阶段初始化-在对象初始化之前,我不需要对象.

I'd also like to avoid two-phase initialization - I don't need the object to be available until it's been initialized.

推荐答案

滥用 LazyTask<T> ,可以转换一个CreateAsync方法转换为Autofac 可以解决的问题:-一种类型[源自LazyTask<T>],看起来像这样:

Abusing LazyTask<T>, one can transform a CreateAsync method into something Autofac can resolve :- a Type [derived from LazyTask<T>], which looks like so:

class AppModel { public class AsyncFactory : LazyTask<AppModel> { public AsyncFactory(IDependency x, IDependency2 y) : base(async() => new AppModel( x.CalculateA(await y.CalculateB()))) {} } private AppModel(A a) { ... } }

这可以消耗如下:-

var appModel = await container.Resolve<AppModel.AsyncFactory>();

不接受这一点,因为我仍然觉得这有一个更清晰的机会-即,如果Autofac对Task<T> CreateAsync方法编写如下特殊处理:-

Not accepting this as I still feel there is an opportunity for this to be clearer - i.e. if Autofac was to apply a special treatment to Task<T> CreateAsync methods written as follows:-

class AppModel { public async Task<AppModel> CreateAsync(IDependency x, IDependency2 y) => new AppModel( x.CalculateA(await y.CalculateB())); }

自动将它们注册为类型Task<T>,允许一个人在不依赖我的Task包装器类型的情况下进行以下消费:-

automatically registering them as the type Task<T>, allowing one to consume as follows without relying on my Task wrapper type:-

var appModel = await container.Resolve<Task<AppModel>>();

更多推荐

Autofac:注册异步工厂方法

本文发布于:2023-11-05 21:15:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工厂   方法   Autofac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!