-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathServicesCollectionExtensions.cs
More file actions
49 lines (41 loc) · 1.9 KB
/
Copy pathServicesCollectionExtensions.cs
File metadata and controls
49 lines (41 loc) · 1.9 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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SharpRepository.Ioc.StructureMap;
using SharpRepository.Repository;
using SharpRepository.Repository.Configuration;
using SharpRepository.Repository.Ioc;
using StructureMap;
using System;
namespace SharpRepository.Ioc.Microsoft.DependencyInjection
{
public static class ServicesCollectionExtensions
{
public static IServiceProvider UseSharpRepository(this IServiceCollection services, IConfigurationSection configurationSection, string repositoryName = null)
{
if (configurationSection == null)
throw new ConfigurationErrorsException("Configuration section not found.");
var sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection);
return services.UseSharpRepository(sharpRepoConfig, repositoryName);
}
public static IServiceProvider UseSharpRepository(this IServiceCollection services, ISharpRepositoryConfiguration sharpRepoConfig, string repositoryName = null)
{
var container = new Container();
container.Configure(config =>
{
config.Scan(x =>
{
x.TheCallingAssembly();
x.AssembliesAndExecutablesFromApplicationBaseDirectory();
x.LookForRegistries();
});
config.ForRepositoriesUseSharpRepository(sharpRepoConfig, repositoryName);
config.Populate(services);
});
var resolver = container.GetInstance<IServiceProvider>();
RepositoryDependencyResolver.SetDependencyResolver(resolver);
// Finally, make sure we return an IServiceProvider. This makes
// ASP.NET use the StructureMap container to resolve its services.
return resolver;
}
}
}