forked from danielgerlag/workflow-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceCollectionExtensions.cs
57 lines (53 loc) · 2.07 KB
/
ServiceCollectionExtensions.cs
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
using MongoDB.Driver;
using System;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Persistence.MongoDB.Services;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static WorkflowOptions UseMongoDB(
this WorkflowOptions options,
string mongoUrl,
string databaseName,
Action<MongoClientSettings> configureClient = default)
{
options.UsePersistence(sp =>
{
var mongoClientSettings = MongoClientSettings.FromConnectionString(mongoUrl);
configureClient?.Invoke(mongoClientSettings);
var client = new MongoClient(mongoClientSettings);
var db = client.GetDatabase(databaseName);
return new MongoPersistenceProvider(db);
});
options.Services.AddTransient<IWorkflowPurger>(sp =>
{
var mongoClientSettings = MongoClientSettings.FromConnectionString(mongoUrl);
configureClient?.Invoke(mongoClientSettings);
var client = new MongoClient(mongoClientSettings);
var db = client.GetDatabase(databaseName);
return new WorkflowPurger(db);
});
return options;
}
public static WorkflowOptions UseMongoDB(
this WorkflowOptions options,
Func<IServiceProvider, IMongoDatabase> createDatabase)
{
if (options == null) throw new ArgumentNullException(nameof(options));
if (createDatabase == null) throw new ArgumentNullException(nameof(createDatabase));
options.UsePersistence(sp =>
{
var db = createDatabase(sp);
return new MongoPersistenceProvider(db);
});
options.Services.AddTransient<IWorkflowPurger>(sp =>
{
var db = createDatabase(sp);
return new WorkflowPurger(db);
});
return options;
}
}
}