├── .gitignore ├── Eventual2PC.code-workspace ├── LICENSE ├── Makefile ├── README.md ├── doc └── images │ ├── 2pc-1.png │ ├── 2pc-2.png │ └── tcc-1.png ├── package.png └── src ├── Eventual2PC.sln └── Eventual2PC ├── Commands ├── ITransactionInitiatorAddCommittedParticipantCommand.cs ├── ITransactionInitiatorAddPreCommitFailedParticipantCommand.cs ├── ITransactionInitiatorAddPreCommitSucceedParticipantCommand.cs ├── ITransactionInitiatorAddRolledbackParticipantCommand.cs ├── ITransactionParticipantCommitCommand.cs ├── ITransactionParticipantPreCommitCommand.cs └── ITransactionParticipantRollbackCommand.cs ├── Events ├── ITransactionInitiatorAllParticipantPreCommitSucceed.cs ├── ITransactionInitiatorAnyParticipantPreCommitFailed.cs ├── ITransactionInitiatorCommittedParticipantAdded.cs ├── ITransactionInitiatorPreCommitFailedParticipantAdded.cs ├── ITransactionInitiatorPreCommitSucceedParticipantAdded.cs ├── ITransactionInitiatorRolledbackParticipantAdded.cs ├── ITransactionInitiatorTransactionCompleted.cs ├── ITransactionInitiatorTransactionStarted.cs ├── ITransactionParticipantCommitted.cs ├── ITransactionParticipantPreCommitFailed.cs ├── ITransactionParticipantPreCommitSucceed.cs └── ITransactionParticipantRolledback.cs ├── Eventual2PC.csproj ├── ITransactionInitiator.cs ├── ITransactionParticipant.cs ├── ITransactionPreparation.cs ├── TransactionParticipantInfo.cs ├── TransactionPreparationBase.cs └── TransactionPreparationInfo.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /Eventual2PC.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": "." 5 | } 6 | ], 7 | "settings": {} 8 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Jerry Bai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: pack 2 | 3 | pack: build 4 | mkdir -p `pwd`/packages 5 | dotnet pack -c Release `pwd`/src/Eventual2PC/ 6 | mv `pwd`/src/Eventual2PC/bin/Release/*.nupkg `pwd`/packages/ 7 | 8 | build: 9 | dotnet build -c Release `pwd`/src/Eventual2PC/ 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eventual2PC 2 | 3 | 最终一致性2PC范式,提供多聚合根之间交互事务的抽象接口。任何基于cqrs + eda 实现多聚合根最终一致性的框架,都可基于此接口进行实现,以达到提高开发效率的目的。 4 | 5 | 由于事务控制也在业务端,所以此处的 `2PC` 概念,可以等效于 `TCC` 概念;目前不支持 `Timeout` 隐式 `Cancel`。 6 | 7 | ## 安装 8 | 9 | ``` 10 | dotnet add package Eventual2PC 11 | ``` 12 | 13 | ## 实现案例 14 | 15 | ### 基于ENode的CQRS框架的实现 16 | 17 | [ENode.Eventual2PC](https://github.com/berkaroad/ENode.Eventual2PC) 18 | 19 | ## 文档 20 | 21 | ### 2PC定义 22 | 23 | 2pc(two-phase commit protocol)是非常经典的强一致性、中心化的原子提交协议。中心化是指协议中有两类节点:一个中心化协调者节点(coordinator)和N个参与者节点(participant、cohort)。 24 | 25 | 协议的每一次事务提交分为两个阶段: 26 | 27 | - 第一阶段,协调者询问所有的参与者是否可以提交事务(请参与者投票),所有参与者向协调者投票。 28 | 29 | - 第二阶段,协调者根据所有参与者的投票结果做出是否事务可以全局提交的决定,并通知所有的参与者执行该决定。 30 | 31 | 在一个两阶段提交流程中,参与者不能改变自己的投票结果。两阶段提交协议的可以全局提交的前提是所有的参与者都同意提交事务,只要有一个参与者投票选择放弃(abort)事务,则事务必须被放弃。 32 | 33 | ![2PC Propose/Vote](https://the-paper-trail.org/wp-content/uploads/2010/01/tpc-fault-free-phase-1.png) 34 | 35 | ![2PC Commit/Abort](https://the-paper-trail.org/wp-content/uploads/2010/01/tpc-fault-free-phase-2.png) 36 | 37 | 文献参考:[https://www.the-paper-trail.org/post/2008-11-27-consensus-protocols-two-phase-commit/](https://www.the-paper-trail.org/post/2008-11-27-consensus-protocols-two-phase-commit/) 38 | 39 | ### TCC定义 40 | 41 | TCC(Try-Confirm-Cancel)又称补偿事务。其核心思想是:"针对每个操作都要注册一个与其对应的确认和补偿(撤销操作)"。它分为三个操作: 42 | 43 | - Try阶段:主要是对业务系统做检测及资源预留。 44 | 45 | - Confirm阶段:确认执行业务操作。 46 | 47 | - Cancel阶段:取消执行业务操作。 48 | 49 | TCC事务的处理流程与2PC两阶段提交类似,不过2PC通常都是在跨库的DB层面,而TCC本质上就是一个应用层面的2PC,需要通过业务逻辑来实现。这种分布式事务的实现方式的优势在于,可以让应用自己定义数据库操作的粒度,使得降低锁冲突、提高吞吐量成为可能。 50 | 51 | 为了满足一致性的要求,confirm和cancel接口还必须实现幂等。 52 | 53 | ![TCC](https://www.enterpriseintegrationpatterns.com/img/TryConfirmCancelState.png) 54 | 55 | 文献参考: [https://www.enterpriseintegrationpatterns.com/patterns/conversation/TryConfirmCancel.html](https://www.enterpriseintegrationpatterns.com/patterns/conversation/TryConfirmCancel.html) 56 | 57 | ### 术语 58 | 59 | - `Initiator`: 事务发起方,它是聚合根,用于维护事务状态;如果本身也参与当前事务,那么在发起事务时的业务校验等效于`PreCommit`,在处理 `TransactionCompleted` 事件时修改自身状态等效于 `Commit` 60 | 61 | - `ProcessManager`: CQRS中的概念,作为事务相关消息路由的角色,用于协调 `Participant` 62 | 63 | - `Coordinator`: 事务协调者,是 `Initiator` + `ProcessManager` 的概念总和 64 | 65 | - `Participant`: 事务参与方(仅针对被修改的聚合根,因为新增不会产生业务失败问题;若需要新增的聚合根参与事务,可以在 `ProcessManager` 响应 `AllParticipantPreCommitSucceed` 事件时发送命令消息),它是聚合根,负责接受 `PreCommit`、`Commit`、`Rollback`以处理自身业务 66 | 67 | - `Preparation`: `Participant` 的事务准备,表示参与事务的业务修改行为,一个事务准备可以用于不同事务 68 | 69 | - `Transaction`: 2PC事务,可以只是一个标识ID(通常使用 `TransactionStarted` 事件的ID),也可以使用代表事务的聚合根(如银行转账的转账事务聚合根) 70 | 71 | #### Initiator 命令 72 | 73 | - `AddPreCommitSucceedParticipant`: 添加预提交成功的参与方 74 | 75 | - `AddPreCommitFailedParticipant`: 添加预提交失败的参与方 76 | 77 | - `AddCommittedParticipant`: 添加已提交的参与方 78 | 79 | - `AddRolledbackParticipant`: 添加已回滚的参与方 80 | 81 | #### Initiator 事件 82 | 83 | - `TransactionStarted`: 事务已发起事件 84 | 85 | - `PreCommitSucceedParticipantAdded`: 预提交成功的参与者已添加事件 86 | 87 | - `PreCommitFailedParticipantAdded`: 预提交失败的参与者已添加事件 88 | 89 | - `AllParticipantPreCommitSucceed`: 所有参与者预提交已成功事件 90 | 91 | - `AnyParticipantPreCommitFailed`: 任意一个参与者预提交已失败事件 92 | 93 | - `CommittedParticipantAdded`: 已提交的参与者已添加事件 94 | 95 | - `RolledbackParticipantAdded`: 已回滚的参与者已添加事件 96 | 97 | - `TransactionCompleted`: 事务已完成事件,并包含是否事务已提交的状态 98 | 99 | #### Participant 命令 100 | 101 | - `PreCommit`: 预提交 102 | 103 | - `Commit`: 提交 104 | 105 | - `Rollback`: 回滚 106 | 107 | #### Participant 事件 108 | 109 | - `PreCommitSucceed`: 预提交已成功事件 110 | 111 | - `PreCommitFailed`: 预提交已失败事件(或领域异常消息) 112 | 113 | - `Committed`: 已提交事件 114 | 115 | - `Rolledback`: 已回滚事件 116 | 117 | ### 规约 118 | 119 | - 一个聚合根,可以同时扮演 `Initiator` 和 `Transaction`的角色,如银行转账事务聚合根 120 | 121 | - 一个聚合根,可以同时扮演事务A中的 `Participant` 和事务B的 `Initiator` 122 | 123 | - `Initiator` 的聚合根实例,发起事务时,必须存在至少一个 `Participant`,且不能把自己作为 `Participant`;如果自己参与事务中,则通过发起事务都位置进行业务校验,在处理 `TransactionCompleted` 事件时提交业务修改 124 | 125 | - `Initiator` 的聚合根实例,如果处于事务A中,那么将不允许作为事务B的 `Participant`,直到事务A结束,才允许 126 | 127 | - `Initiator` 的聚合根实例,仅允许发起一个事务,只有事务完成后,才可以发起其他事务;此处的事务完成,以是否发布`TransactionCompleted`事件为准 128 | 129 | - `Participant` 参与事务的业务修改行为有多少个,对应定义多少个`Preparation`,与参与的事务数无关 130 | 131 | - `Participant` 的聚合根实例,允许同时参与多个不同的事务;也可以通过业务代码,在 `PreCommit` 时,判断是否存在其他类型的 `Preparation` 来阻止当前 `Preparation` 的 `PreCommit` 操作 132 | 133 | - `Initiator` 和 `Participant` 的命令处理都必须支持幂等 134 | 135 | ### 处理流程 136 | 137 | - `Initiator` 发布 `TransactionStarted` 事件 138 | 139 | - `ProcessManager` 响应 `TransactionStarted` 事件,并发送 `PreCommit` 命令 140 | 141 | - `Participant` 处理命令 `PreCommit` 142 | 143 | - 如果成功,则发布 `PreCommitSucceed` 事件; 144 | 145 | - 如果失败,则发布 `PreCommitFailed` 事件(或领域异常)。 146 | 147 | - `ProcessManager` 响应 `PreCommitSucceed`,并发送 `AddPreCommitSucceedParticipant` 命令 148 | 149 | - `ProcessManager` 响应 `PreCommitFailed`,并发送 `AddPreCommitFailedParticipant` 命令 150 | 151 | - `Initiator` 处理命令 `AddPreCommitSucceedParticipant`,发布 `PreCommitSucceedParticipantAdded` 事件 152 | 153 | - 如果所有 `Participant` 的 `PreCommit` 都已处理完成且都成功,则再发布 `AllParticipantPreCommitSucceed` 事件; 154 | 155 | - 如果已处理完成,但存在失败,则再发布 `AnyParticipantPreCommitFailed` 事件。 156 | 157 | - `Initiator` 处理命令 `AddPreCommitFailedParticipant`,发布 `PreCommitFailedParticipantAdded` 事件 158 | 159 | - 如果所有 `Participant` 的 `PreCommit` 都已处理完成,则发布 `AnyParticipantPreCommitFailed` 事件; 160 | 161 | - 如果已处理完成,且都失败,则再发布 `TransactionCompleted` 事件。 162 | 163 | - `ProcessManager` 响应 `AllParticipantPreCommitSucceed`,并发送 `Commit` 命令 164 | 165 | - `ProcessManager` 响应 `AnyParticipantPreCommitFailed`,并发送 `Rollback` 命令 166 | 167 | - `Participant` 处理命令 `Commit`,并发布 `Committed` 事件 168 | 169 | - `Participant` 处理命令 `Rollback`,并发布 `Rolledback` 事件 170 | 171 | - `ProcessManager` 响应 `Committed`,并发送 `AddCommittedParticipant` 命令 172 | 173 | - `ProcessManager` 响应 `Rolledback`,并发送 `AddRolledbackParticipant` 命令 174 | 175 | - `Initiator` 处理命令 `AddCommittedParticipant`,发布 `CommittedParticipantAdded` 事件 176 | 177 | - 如果所有 `Participant` 的 `Commit` 都已处理完成,则再发布 `TransactionCompleted` 事件。 178 | 179 | - `Initiator` 处理命令 `AddRolledbackParticipant`,发布 `RolledbackParticipantAdded` 事件 180 | 181 | - 如果所有 `Participant` 的 `Rolledback` 都已处理完成,则再发布 `TransactionCompleted` 事件。 182 | 183 | ## 发布历史 184 | 185 | ### 1.1.0(2020/04/30) 186 | 187 | - 1)`ITransactionInitiator`接口增加属性 `CurrentTransactionId`、`CurrentTransactionType` 188 | 189 | - 2)增加事务流转过程中产生的 `Command` 的接口定义 190 | 191 | ### 1.0.2(2020/4/28) 192 | 193 | - 移除异常类 `UnknownTransactionPreparationException` 194 | 195 | ### 1.0.1(2020/4/27) 196 | 197 | - 移除 `TransactionParticipantInfo` 的方法 `ValidateParticipantMustNotExists`,是否抛出异常,由使用方决定 198 | 199 | ### 1.0.0(2020/4/25) 200 | 201 | - 初始版本 202 | 203 | ## 鸣谢 204 | 205 | 非常感谢 ENode群,热心群友提出的各种异议,以及 [ENode](http://github.com/tangxuehua/enode) 的作者 汤雪华 阐述2PC概念、ENode设计理念,提供了宝贵的意见。 -------------------------------------------------------------------------------- /doc/images/2pc-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/berkaroad/Eventual2PC/c3ba31c71034a5b0157abf39663188e5334b387c/doc/images/2pc-1.png -------------------------------------------------------------------------------- /doc/images/2pc-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/berkaroad/Eventual2PC/c3ba31c71034a5b0157abf39663188e5334b387c/doc/images/2pc-2.png -------------------------------------------------------------------------------- /doc/images/tcc-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/berkaroad/Eventual2PC/c3ba31c71034a5b0157abf39663188e5334b387c/doc/images/tcc-1.png -------------------------------------------------------------------------------- /package.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/berkaroad/Eventual2PC/c3ba31c71034a5b0157abf39663188e5334b387c/package.png -------------------------------------------------------------------------------- /src/Eventual2PC.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26124.0 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Eventual2PC", "Eventual2PC\Eventual2PC.csproj", "{FB832C09-5CB6-4A1D-A11D-F7E10D94E333}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|x64 = Debug|x64 12 | Debug|x86 = Debug|x86 13 | Release|Any CPU = Release|Any CPU 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Debug|x64.ActiveCfg = Debug|Any CPU 24 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Debug|x64.Build.0 = Debug|Any CPU 25 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Debug|x86.ActiveCfg = Debug|Any CPU 26 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Debug|x86.Build.0 = Debug|Any CPU 27 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Release|Any CPU.Build.0 = Release|Any CPU 29 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Release|x64.ActiveCfg = Release|Any CPU 30 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Release|x64.Build.0 = Release|Any CPU 31 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Release|x86.ActiveCfg = Release|Any CPU 32 | {FB832C09-5CB6-4A1D-A11D-F7E10D94E333}.Release|x86.Build.0 = Release|Any CPU 33 | EndGlobalSection 34 | EndGlobal 35 | -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionInitiatorAddCommittedParticipantCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务发起方添加已提交的参与方 5 | /// 6 | public interface ITransactionInitiatorAddCommittedParticipantCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | 13 | /// 14 | /// 事务类型 15 | /// 16 | byte TransactionType { get; } 17 | 18 | /// 19 | /// 事务参与方ID 20 | /// 21 | string ParticipantId { get; } 22 | 23 | /// 24 | /// 事务参与方类型 25 | /// 26 | byte ParticipantType { get; } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionInitiatorAddPreCommitFailedParticipantCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务发起方添加预提交失败的参与方 5 | /// 6 | public interface ITransactionInitiatorAddPreCommitFailedParticipantCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | 13 | /// 14 | /// 事务类型 15 | /// 16 | byte TransactionType { get; } 17 | 18 | /// 19 | /// 事务参与方ID 20 | /// 21 | string ParticipantId { get; } 22 | 23 | /// 24 | /// 事务参与方类型 25 | /// 26 | byte ParticipantType { get; } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionInitiatorAddPreCommitSucceedParticipantCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务发起方添加预提交成功的参与方 5 | /// 6 | public interface ITransactionInitiatorAddPreCommitSucceedParticipantCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | 13 | /// 14 | /// 事务类型 15 | /// 16 | byte TransactionType { get; } 17 | 18 | /// 19 | /// 事务参与方ID 20 | /// 21 | string ParticipantId { get; } 22 | 23 | /// 24 | /// 事务参与方类型 25 | /// 26 | byte ParticipantType { get; } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionInitiatorAddRolledbackParticipantCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务发起方添加已回滚的参与方 5 | /// 6 | public interface ITransactionInitiatorAddRolledbackParticipantCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | 13 | /// 14 | /// 事务类型 15 | /// 16 | byte TransactionType { get; } 17 | 18 | /// 19 | /// 事务参与方ID 20 | /// 21 | string ParticipantId { get; } 22 | 23 | /// 24 | /// 事务参与方类型 25 | /// 26 | byte ParticipantType { get; } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionParticipantCommitCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务参与方提交命令 5 | /// 6 | public interface ITransactionParticipantCommitCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionParticipantPreCommitCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务参与方预提交命令 5 | /// 6 | public interface ITransactionParticipantPreCommitCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | 13 | /// 14 | /// 事务类型 15 | /// 16 | byte TransactionType { get; } 17 | 18 | /// 19 | /// 事务发起方ID 20 | /// 21 | string InitiatorId { get; } 22 | 23 | /// 24 | /// 事务发起方类型 25 | /// 26 | byte InitiatorType { get; } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Commands/ITransactionParticipantRollbackCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Commands 2 | { 3 | /// 4 | /// 事务参与方回滚命令 5 | /// 6 | public interface ITransactionParticipantRollbackCommand 7 | { 8 | /// 9 | /// 事务ID 10 | /// 11 | string TransactionId { get; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorAllParticipantPreCommitSucceed.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Eventual2PC.Events 4 | { 5 | /// 6 | /// 事务参与方所有预提交已成功事件接口 7 | /// 8 | public interface ITransactionInitiatorAllParticipantPreCommitSucceed 9 | where TInitiator : class, ITransactionInitiator 10 | { 11 | /// 12 | /// 事务ID 13 | /// 14 | string TransactionId { get; } 15 | 16 | /// 17 | /// 事务类型 18 | /// 19 | byte TransactionType { get; } 20 | 21 | /// 22 | /// 事务参与方信息 23 | /// 24 | IEnumerable TransactionParticipants { get; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorAnyParticipantPreCommitFailed.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Eventual2PC.Events 4 | { 5 | /// 6 | /// 事务参与方任意一个预提交已失败的事件接口 7 | /// 8 | public interface ITransactionInitiatorAnyParticipantPreCommitFailed 9 | where TInitiator : class, ITransactionInitiator 10 | { 11 | /// 12 | /// 事务ID 13 | /// 14 | string TransactionId { get; } 15 | 16 | /// 17 | /// 事务类型 18 | /// 19 | byte TransactionType { get; } 20 | 21 | /// 22 | /// 成功预提交的事务参与方信息 23 | /// 24 | IEnumerable PreCommitSucceedTransactionParticipants { get; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorCommittedParticipantAdded.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 已提交的事务参与方已添加事件接口 5 | /// 6 | public interface ITransactionInitiatorCommittedParticipantAdded 7 | where TInitiator : class, ITransactionInitiator 8 | { 9 | /// 10 | /// 事务ID 11 | /// 12 | string TransactionId { get; } 13 | 14 | /// 15 | /// 事务类型 16 | /// 17 | byte TransactionType { get; } 18 | 19 | /// 20 | /// 事务参与方信息 21 | /// 22 | TransactionParticipantInfo TransactionParticipant { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorPreCommitFailedParticipantAdded.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 预提交失败的事务参与方已添加事件接口 5 | /// 6 | public interface ITransactionInitiatorPreCommitFailedParticipantAdded 7 | where TInitiator : class, ITransactionInitiator 8 | { 9 | /// 10 | /// 事务ID 11 | /// 12 | string TransactionId { get; } 13 | 14 | /// 15 | /// 事务类型 16 | /// 17 | byte TransactionType { get; } 18 | 19 | /// 20 | /// 事务参与方信息 21 | /// 22 | TransactionParticipantInfo TransactionParticipant { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorPreCommitSucceedParticipantAdded.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 预提交成功的事务参与方已添加事件接口 5 | /// 6 | public interface ITransactionInitiatorPreCommitSucceedParticipantAdded 7 | where TInitiator : class, ITransactionInitiator 8 | { 9 | /// 10 | /// 事务ID 11 | /// 12 | string TransactionId { get; } 13 | 14 | /// 15 | /// 事务类型 16 | /// 17 | byte TransactionType { get; } 18 | 19 | /// 20 | /// 事务参与方信息 21 | /// 22 | TransactionParticipantInfo TransactionParticipant { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorRolledbackParticipantAdded.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 已回滚的事务参与方已添加事件接口 5 | /// 6 | public interface ITransactionInitiatorRolledbackParticipantAdded 7 | where TInitiator : class, ITransactionInitiator 8 | { 9 | /// 10 | /// 事务ID 11 | /// 12 | string TransactionId { get; } 13 | 14 | /// 15 | /// 事务类型 16 | /// 17 | byte TransactionType { get; } 18 | 19 | /// 20 | /// 事务参与方信息 21 | /// 22 | TransactionParticipantInfo TransactionParticipant { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorTransactionCompleted.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 事务已完成事件接口 5 | /// 6 | public interface ITransactionInitiatorTransactionCompleted 7 | where TInitiator : class, ITransactionInitiator 8 | { 9 | /// 10 | /// 事务ID 11 | /// 12 | string TransactionId { get; } 13 | 14 | /// 15 | /// 事务类型 16 | /// 17 | byte TransactionType { get; } 18 | 19 | /// 20 | /// 事务是否成功 21 | /// 22 | bool IsCommitSuccess { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionInitiatorTransactionStarted.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 事务已开始事件接口 5 | /// 6 | public interface ITransactionInitiatorTransactionStarted 7 | where TInitiator : class, ITransactionInitiator 8 | { 9 | /// 10 | /// 事务ID 11 | /// 12 | string TransactionId { get; } 13 | 14 | /// 15 | /// 事务类型 16 | /// 17 | byte TransactionType { get; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionParticipantCommitted.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 事务参与者已提交事件 5 | /// 6 | /// 7 | /// 8 | public interface ITransactionParticipantCommitted 9 | where TParticipant : class, ITransactionParticipant 10 | where TTransactionPreparation : class, ITransactionPreparation 11 | { 12 | /// 13 | /// 事务准备 14 | /// 15 | TTransactionPreparation TransactionPreparation { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionParticipantPreCommitFailed.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 事务参与者预提交失败事件(或领域异常) 5 | /// 6 | /// 7 | /// 8 | public interface ITransactionParticipantPreCommitFailed 9 | where TParticipant : class, ITransactionParticipant 10 | where TTransactionPreparation : class, ITransactionPreparation 11 | { 12 | /// 13 | /// 事务准备 14 | /// 15 | TTransactionPreparation TransactionPreparation { get; } 16 | } 17 | 18 | /// 19 | /// 事务参与者预提交失败事件(或领域异常) 20 | /// 21 | /// 22 | public interface ITransactionParticipantPreCommitFailed 23 | where TParticipant : class, ITransactionParticipant 24 | { 25 | /// 26 | /// 事务准备类型 27 | /// 28 | string TransactionPreparationType { get; } 29 | 30 | /// 31 | /// 事务准备 32 | /// 33 | TransactionPreparationInfo TransactionPreparation { get; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionParticipantPreCommitSucceed.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 事务参与者预提交成功事件 5 | /// 6 | /// 7 | /// 8 | public interface ITransactionParticipantPreCommitSucceed 9 | where TParticipant : class, ITransactionParticipant 10 | where TTransactionPreparation : class, ITransactionPreparation 11 | { 12 | /// 13 | /// 事务准备 14 | /// 15 | TTransactionPreparation TransactionPreparation { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Eventual2PC/Events/ITransactionParticipantRolledback.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC.Events 2 | { 3 | /// 4 | /// 事务参与者已回滚事件 5 | /// 6 | /// 7 | /// 8 | public interface ITransactionParticipantRolledback 9 | where TParticipant : class, ITransactionParticipant 10 | where TTransactionPreparation : class, ITransactionPreparation 11 | { 12 | /// 13 | /// 事务准备 14 | /// 15 | TTransactionPreparation TransactionPreparation { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Eventual2PC/Eventual2PC.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | true 6 | false 7 | MIT 8 | https://github.com/berkaroad/Eventual2PC 9 | https://github.com/berkaroad/Eventual2PC 10 | Jerry Bai 11 | Eventual2PC 12 | 1.1.0 13 | 1.1.0 14 | Eventual2PC 15 | _content\package.png 16 | true 17 | _content 18 | 最终一致性二阶段提交范式,简化多聚合根之间交互事务的实现。 19 | 最终一致性二阶段提交范式,简化多聚合根之间交互事务的实现。 20 | 2pc tcc eventual transaction processmanager saga eda cqrs 21 | 1)`ITransactionInitiator`接口增加属性 `CurrentTransactionId`、`CurrentTransactionType` 22 | 2)增加事务流转过程中产生的 `Command` 的接口定义 23 | 24 | 25 | 26 | 27 | _content\package.png 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/Eventual2PC/ITransactionInitiator.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC 2 | { 3 | /// 4 | /// 事务发起方 5 | /// 6 | public interface ITransactionInitiator 7 | { 8 | /// 9 | /// 添加预提交成功的参与者信息 10 | /// 11 | /// 事务ID 12 | /// 事务类型 13 | /// 事务参与方信息 14 | void AddPreCommitSuccessParticipant(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo); 15 | 16 | /// 17 | /// 添加预提交失败的参与者信息 18 | /// 19 | /// 事务ID 20 | /// 事务类型 21 | /// 事务参与方信息 22 | void AddPreCommitFailedParticipant(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo); 23 | 24 | /// 25 | /// 添加已提交的参与者信息 26 | /// 27 | /// 事务ID 28 | /// 事务类型 29 | /// 事务参与方信息 30 | void AddCommittedParticipant(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo); 31 | 32 | /// 33 | /// 添加已回滚的参与者信息 34 | /// 35 | /// 36 | /// 事务类型 37 | /// 事务参与方信息 38 | void AddRolledbackParticipant(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo); 39 | 40 | /// 41 | /// 是否事务在处理中 42 | /// 43 | bool IsTransactionProcessing { get; } 44 | 45 | /// 46 | /// 当前事务ID 47 | /// 48 | string CurrentTransactionId { get; } 49 | 50 | /// 51 | /// 当前事务类型 52 | /// 53 | byte CurrentTransactionType { get; } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Eventual2PC/ITransactionParticipant.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC 2 | { 3 | /// 4 | /// 事务参与方 5 | /// 6 | public interface ITransactionParticipant 7 | { 8 | /// 9 | /// 预提交(产生预提交成功的领域事件,或抛出领域异常) 10 | /// 11 | /// 12 | void PreCommit(ITransactionPreparation transactionPreparation); 13 | 14 | /// 15 | /// 提交(预提交反馈成功后,才能提交) 16 | /// 17 | /// 18 | void Commit(string transactionId); 19 | 20 | /// 21 | /// 回滚(由事务发起方进行回滚) 22 | /// 23 | /// 24 | void Rollback(string transactionId); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Eventual2PC/ITransactionPreparation.cs: -------------------------------------------------------------------------------- 1 | namespace Eventual2PC 2 | { 3 | /// 4 | /// 事务准备 5 | /// 6 | public interface ITransactionPreparation 7 | { 8 | /// 9 | /// 事务参与方ID 10 | /// 11 | string ParticipantId { get; } 12 | 13 | /// 14 | /// 事务参与方类型 15 | /// 16 | byte ParticipantType { get; } 17 | 18 | /// 19 | /// 事务ID 20 | /// 21 | string TransactionId { get; } 22 | 23 | /// 24 | /// 事务类型 25 | /// 26 | byte TransactionType { get; } 27 | 28 | /// 29 | /// 事务发起方ID 30 | /// 31 | string InitiatorId { get; } 32 | 33 | /// 34 | /// 事务发起方类型 35 | /// 36 | byte InitiatorType { get; } 37 | 38 | /// 39 | /// 获取准备信息 40 | /// 41 | /// 42 | TransactionPreparationInfo GetTransactionPreparationInfo(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Eventual2PC/TransactionParticipantInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Eventual2PC 6 | { 7 | /// 8 | /// 事务参与方信息 9 | /// 10 | [Serializable] 11 | public sealed class TransactionParticipantInfo 12 | { 13 | /// 14 | /// 事务准备 15 | /// 16 | /// 事务参与方ID 17 | /// 事务参与方类型 18 | public TransactionParticipantInfo(string participantId, byte participantType) 19 | { 20 | ParticipantId = participantId; 21 | ParticipantType = participantType; 22 | } 23 | 24 | /// 25 | /// 事务参与方ID 26 | /// 27 | public string ParticipantId { get; private set; } 28 | 29 | /// 30 | /// 事务参与方类型 31 | /// 32 | public byte ParticipantType { get; private set; } 33 | 34 | /// 35 | /// 参与者是否已存在 36 | /// 37 | /// 参与者信息列表 38 | /// 39 | public bool IsParticipantAlreadyExists(IEnumerable participants) 40 | { 41 | return participants != null && participants.Any(a => a.ParticipantId == ParticipantId); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Eventual2PC/TransactionPreparationBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Eventual2PC 4 | { 5 | /// 6 | /// 事务准备 7 | /// 8 | [Serializable] 9 | public abstract class TransactionPreparationBase : ITransactionPreparation 10 | { 11 | /// 12 | /// 事务准备 13 | /// 14 | protected TransactionPreparationBase() {} 15 | 16 | /// 17 | /// 事务准备 18 | /// 19 | /// 事务参与方ID 20 | /// 事务参与方类型 21 | /// 事务ID 22 | /// 事务类型 23 | /// 事务发起方ID 24 | /// 事务发起方类型 25 | protected TransactionPreparationBase(string participantId, byte participantType, string transactionId, byte transactionType, string initiatorId, byte initiatorType) 26 | { 27 | ParticipantId = participantId; 28 | ParticipantType = participantType; 29 | TransactionId = transactionId; 30 | TransactionType = transactionType; 31 | InitiatorId = initiatorId; 32 | InitiatorType = initiatorType; 33 | } 34 | 35 | /// 36 | /// 事务参与方ID 37 | /// 38 | public string ParticipantId { get; protected set; } 39 | 40 | /// 41 | /// 事务参与方类型 42 | /// 43 | public byte ParticipantType { get; protected set; } 44 | 45 | /// 46 | /// 事务ID 47 | /// 48 | public string TransactionId { get; protected set; } 49 | 50 | /// 51 | /// 事务类型 52 | /// 53 | public byte TransactionType { get; protected set; } 54 | 55 | /// 56 | /// 事务发起方ID 57 | /// 58 | public string InitiatorId { get; protected set; } 59 | 60 | /// 61 | /// 事务发起方类型 62 | /// 63 | public byte InitiatorType { get; protected set; } 64 | 65 | /// 66 | /// 获取准备信息 67 | /// 68 | /// 69 | public TransactionPreparationInfo GetTransactionPreparationInfo() 70 | { 71 | return new TransactionPreparationInfo(ParticipantId, ParticipantType, TransactionId, TransactionType, InitiatorId, InitiatorType); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Eventual2PC/TransactionPreparationInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Eventual2PC 4 | { 5 | /// 6 | /// 事务准备信息 7 | /// 8 | [Serializable] 9 | public sealed class TransactionPreparationInfo 10 | { 11 | /// 12 | /// 事务准备信息 13 | /// 14 | public TransactionPreparationInfo() { } 15 | 16 | /// 17 | /// 事务准备 18 | /// 19 | /// 事务参与方ID 20 | /// 事务参与方类型 21 | /// 事务ID 22 | /// 事务类型 23 | /// 事务发起方ID 24 | /// 事务发起方类型 25 | public TransactionPreparationInfo(string participantId, byte participantType, string transactionId, byte transactionType, string initiatorId, byte initiatorType) 26 | { 27 | ParticipantId = participantId; 28 | ParticipantType = participantType; 29 | TransactionId = transactionId; 30 | TransactionType = transactionType; 31 | InitiatorId = initiatorId; 32 | InitiatorType = initiatorType; 33 | } 34 | 35 | /// 36 | /// 事务参与方ID 37 | /// 38 | public string ParticipantId { get; private set; } 39 | 40 | /// 41 | /// 事务参与方类型 42 | /// 43 | public byte ParticipantType { get; private set; } 44 | 45 | /// 46 | /// 事务ID 47 | /// 48 | public string TransactionId { get; private set; } 49 | 50 | /// 51 | /// 事务类型 52 | /// 53 | public byte TransactionType { get; private set; } 54 | 55 | /// 56 | /// 事务发起方ID 57 | /// 58 | public string InitiatorId { get; private set; } 59 | 60 | /// 61 | /// 事务发起方类型 62 | /// 63 | public byte InitiatorType { get; private set; } 64 | } 65 | } 66 | --------------------------------------------------------------------------------