Skip to Content
C#并发编程经典实例(第2版)
book

C#并发编程经典实例(第2版)

by Stephen Cleary
November 2020
Intermediate to advanced
226 pages
5h 34m
Chinese
Posts & Telecom Press
Content preview from C#并发编程经典实例(第2版)
函数式面向对象编程
149
参阅
11.3
节探讨异步初始化模式,这是一种异步构造方式,能与控制容器的依赖项注入(或反
转)协同工作。
14.1
节探讨异步延迟初始化,如果实例在概念上是共享资源或共享服务,那么这不失为可
行的方案。
11.3
 异步构造
异步初始化模式
问题
假设正在编写一个类型,在其构造器中有一些异步工作需要处理,但不能使用
11.2
节介绍
的异步工厂模式,因为实例是通过反射创建的,例如控制库的依赖项注入(或反转)、数
据绑定、
Activator.CreateInstance
等。
解决方案
虽然通过应用常规的异步初始化模式,可以在一定程度上解决上述问题,但是必须返回未
初始化的实例。所有需要异步初始化的类型都应该定义一个属性,就像这样:
Task Initialization {
get
; }
我一般会在需要异步初始化的类型的标记接口中定义它:
/// <summary>
/// 将类型标记为需要异步初始化并提供该初始化的结果
/// </summary>
public interface
IAsyncInitialization
{
/// <summary>
/// 该实例异步初始化的结果
/// </summary>
Task Initialization {
get
; }
}
在实现这种模式时,应该在构造器中启动初始化,同时分配
Initialization
属性。异步初
始化的结果(包括所有异常)会通过该
Initialization
属性暴露。下面的代码示例使用了
异步初始化来实现简单的类型: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre

Publisher Resources

ISBN: 9787115550606