forked from eventflow/EventFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventFlowOptions.cs
More file actions
193 lines (174 loc) · 8.75 KB
/
EventFlowOptions.cs
File metadata and controls
193 lines (174 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// The MIT License (MIT)
//
// Copyright (c) 2015-2016 Rasmus Mikkelsen
// Copyright (c) 2015-2016 eBay Software Foundation
// https://github.com/rasmus/EventFlow
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using EventFlow.Aggregates;
using EventFlow.Commands;
using EventFlow.Configuration;
using EventFlow.Configuration.Bootstraps;
using EventFlow.Configuration.Registrations;
using EventFlow.Core;
using EventFlow.Core.RetryStrategies;
using EventFlow.EventStores;
using EventFlow.EventStores.InMemory;
using EventFlow.Extensions;
using EventFlow.Jobs;
using EventFlow.Logs;
using EventFlow.Provided;
using EventFlow.Queries;
using EventFlow.ReadStores;
using EventFlow.Subscribers;
namespace EventFlow
{
public class EventFlowOptions : IEventFlowOptions
{
private readonly List<Type> _aggregateEventTypes = new List<Type>();
private readonly List<Type> _commandTypes = new List<Type>();
private readonly EventFlowConfiguration _eventFlowConfiguration = new EventFlowConfiguration();
private readonly List<Type> _jobTypes = new List<Type>();
private Lazy<IServiceRegistration> _lazyRegistrationFactory = new Lazy<IServiceRegistration>(() => new AutofacServiceRegistration());
private EventFlowOptions()
{
UseServiceRegistration(new AutofacServiceRegistration());
ModuleRegistration = new ModuleRegistration(this);
ModuleRegistration.Register<ProvidedJobsModule>();
}
public static EventFlowOptions New => new EventFlowOptions();
public IModuleRegistration ModuleRegistration { get; }
public IEventFlowOptions ConfigureOptimisticConcurrentcyRetry(int retries, TimeSpan delayBeforeRetry)
{
_eventFlowConfiguration.NumberOfRetriesOnOptimisticConcurrencyExceptions = retries;
_eventFlowConfiguration.DelayBeforeRetryOnOptimisticConcurrencyExceptions = delayBeforeRetry;
return this;
}
public IEventFlowOptions Configure(Action<EventFlowConfiguration> configure)
{
configure(_eventFlowConfiguration);
return this;
}
public IEventFlowOptions AddEvents(IEnumerable<Type> aggregateEventTypes)
{
foreach (var aggregateEventType in aggregateEventTypes)
{
if (!typeof (IAggregateEvent).IsAssignableFrom(aggregateEventType))
{
throw new ArgumentException($"Type {aggregateEventType.PrettyPrint()} is not a {typeof (IAggregateEvent).PrettyPrint()}");
}
_aggregateEventTypes.Add(aggregateEventType);
}
return this;
}
public IEventFlowOptions AddCommands(IEnumerable<Type> commandTypes)
{
foreach (var commandType in commandTypes)
{
if (!typeof (ICommand).IsAssignableFrom(commandType))
{
throw new ArgumentException($"Type {commandType.PrettyPrint()} is not a {typeof (ICommand).PrettyPrint()}");
}
_commandTypes.Add(commandType);
}
return this;
}
public IEventFlowOptions AddJobs(IEnumerable<Type> jobTypes)
{
foreach (var jobType in jobTypes)
{
if (!typeof (IJob).IsAssignableFrom(jobType))
{
throw new ArgumentException($"Type {jobType.PrettyPrint()} is not a {typeof (IJob).PrettyPrint()}");
}
_jobTypes.Add(jobType);
}
return this;
}
public IEventFlowOptions RegisterServices(Action<IServiceRegistration> register)
{
register(_lazyRegistrationFactory.Value);
return this;
}
public IEventFlowOptions RegisterModule<TModule>()
where TModule : IModule, new()
{
ModuleRegistration.Register<TModule>();
return this;
}
public IEventFlowOptions RegisterModule<TModule>(TModule module)
where TModule : IModule
{
ModuleRegistration.Register(module);
return this;
}
public IEventFlowOptions UseServiceRegistration(IServiceRegistration serviceRegistration)
{
if (_lazyRegistrationFactory != null && _lazyRegistrationFactory.IsValueCreated)
{
throw new InvalidOperationException("Service registration is already in use");
}
RegisterDefaults(serviceRegistration);
_lazyRegistrationFactory = new Lazy<IServiceRegistration>(() => serviceRegistration);
return this;
}
private void RegisterDefaults(IServiceRegistration serviceRegistration)
{
// http://docs.autofac.org/en/latest/register/registration.html
// Maybe swap around and do after and and .PreserveExistingDefaults()
serviceRegistration.Register<ILog, ConsoleLog>();
serviceRegistration.Register<IEventStore, EventStoreBase>();
serviceRegistration.Register<IEventPersistence, InMemoryEventPersistence>(Lifetime.Singleton);
serviceRegistration.Register<ICommandBus, CommandBus>();
serviceRegistration.Register<IReadModelPopulator, ReadModelPopulator>();
serviceRegistration.Register<IEventJsonSerializer, EventJsonSerializer>();
serviceRegistration.Register<IEventDefinitionService, EventDefinitionService>(Lifetime.Singleton);
serviceRegistration.Register<IQueryProcessor, QueryProcessor>(Lifetime.Singleton);
serviceRegistration.Register<IJsonSerializer, JsonSerializer>();
serviceRegistration.Register<IJobScheduler, InstantJobScheduler>();
serviceRegistration.Register<IJobRunner, JobRunner>();
serviceRegistration.Register<IJobDefinitionService, JobDefinitionService>(Lifetime.Singleton);
serviceRegistration.Register<IOptimisticConcurrencyRetryStrategy, OptimisticConcurrencyRetryStrategy>();
serviceRegistration.Register<IEventUpgradeManager, EventUpgradeManager>(Lifetime.Singleton);
serviceRegistration.Register<IAggregateFactory, AggregateFactory>();
serviceRegistration.Register<IReadModelDomainEventApplier, ReadModelDomainEventApplier>();
serviceRegistration.Register<IDomainEventPublisher, DomainEventPublisher>();
serviceRegistration.Register<ISerializedCommandPublisher, SerializedCommandPublisher>();
serviceRegistration.Register<ICommandDefinitionService, CommandDefinitionService>(Lifetime.Singleton);
serviceRegistration.Register<IDispatchToEventSubscribers, DispatchToEventSubscribers>();
serviceRegistration.Register<IDomainEventFactory, DomainEventFactory>(Lifetime.Singleton);
serviceRegistration.Register<IEventFlowConfiguration>(_ => _eventFlowConfiguration);
serviceRegistration.RegisterGeneric(typeof(ITransientFaultHandler<>), typeof(TransientFaultHandler<>));
serviceRegistration.RegisterGeneric(typeof(IReadModelFactory<>), typeof(ReadModelFactory<>), Lifetime.Singleton);
serviceRegistration.Register<IBootstrap, DefinitionServicesInitilizer>();
serviceRegistration.Register(_ => ModuleRegistration, Lifetime.Singleton);
serviceRegistration.Register<ILoadedVersionedTypes>(r => new LoadedVersionedTypes(
_jobTypes,
_commandTypes,
_aggregateEventTypes),
Lifetime.Singleton);
}
public IRootResolver CreateResolver(bool validateRegistrations = true)
{
return _lazyRegistrationFactory.Value.CreateResolver(validateRegistrations);
}
}
}