@@ -4,11 +4,13 @@ namespace MobileConfiguration.Repository
44{
55 using Database ;
66 using Database . Entities ;
7+ using Microsoft . EntityFrameworkCore ;
78 using Models ;
89 using Newtonsoft . Json ;
910 using Shared . EntityFramework ;
1011 using Shared . Exceptions ;
11- using Microsoft . EntityFrameworkCore ;
12+ using Shared . General ;
13+ using Shared . Repositories ;
1214 using ApplicationCentreConfiguration = Database . Entities . ApplicationCentreConfiguration ;
1315
1416 public interface IConfigurationRepository
@@ -23,24 +25,23 @@ Task UpdateConfiguration(ConfigurationType configurationType,
2325
2426 public class ConfigurationRepository : IConfigurationRepository
2527 {
26- private readonly Shared . EntityFramework . IDbContextFactory < ConfigurationContext > ContextFactory ;
27-
28- public ConfigurationRepository ( Shared . EntityFramework . IDbContextFactory < ConfigurationContext > contextFactory ) {
29- this . ContextFactory = contextFactory ;
28+ private readonly IDbContextResolver < ConfigurationContext > Resolver ;
29+ private static readonly String ConfigDatabaseName = "ConfigurationDatabase" ;
30+ public ConfigurationRepository ( IDbContextResolver < ConfigurationContext > resolver ) {
31+ this . Resolver = resolver ;
3032 }
3133 public async Task < MobileConfiguration > GetConfiguration ( ConfigurationType configurationType ,
3234 String id ,
3335 CancellationToken cancellationToken ) {
34- ConfigurationContext context = await this . ContextFactory . GetContext ( Guid . NewGuid ( ) , "ConfigurationDatabase" , cancellationToken ) ;
35-
36- Configuration ? configuration = await context . Configurations . SingleOrDefaultAsync ( c => c . Id == id && c . ConfigType == ( Int32 ) configurationType , cancellationToken : cancellationToken ) ;
36+ using ResolvedDbContext < ConfigurationContext > ? resolvedContext = this . Resolver . Resolve ( ConfigDatabaseName ) ;
37+ Configuration ? configuration = await resolvedContext . Context . Configurations . SingleOrDefaultAsync ( c => c . Id == id && c . ConfigType == ( Int32 ) configurationType , cancellationToken : cancellationToken ) ;
3738
3839 if ( configuration == null ) {
3940 throw new NotFoundException ( $ "No config of type [{ configurationType } ] found for Id [{ id } ]") ;
4041 }
4142
4243 // TODO: create a factory
43- MobileConfiguration configurationModel = new MobileConfiguration {
44+ MobileConfiguration configurationModel = new ( ) {
4445 ClientId = configuration . ClientId ,
4546 ClientSecret = configuration . ClientSecret ,
4647 ConfigurationType = ( ConfigurationType ) configuration . ConfigType ,
@@ -67,14 +68,12 @@ public async Task<MobileConfiguration> GetConfiguration(ConfigurationType config
6768
6869 public async Task < Result > CreateConfiguration ( MobileConfiguration mobileConfiguration ,
6970 CancellationToken cancellationToken ) {
70- ConfigurationContext context = await this . ContextFactory . GetContext ( Guid . NewGuid ( ) , "ConfigurationDatabase" , cancellationToken ) ;
71-
7271 Configuration configurationEntity = Factories . Factory . ToEntityConfiguration ( mobileConfiguration ) ;
73-
74- await context . Configurations . AddAsync ( configurationEntity , cancellationToken ) ;
72+ using ResolvedDbContext < ConfigurationContext > ? resolvedContext = this . Resolver . Resolve ( ConfigDatabaseName ) ;
73+ await resolvedContext . Context . Configurations . AddAsync ( configurationEntity , cancellationToken ) ;
7574
7675 try {
77- await context . SaveChangesAsync ( cancellationToken ) ;
76+ await resolvedContext . Context . SaveChangesAsync ( cancellationToken ) ;
7877 return Result . Success ( "Configuration created successfully." ) ;
7978 }
8079 catch ( Exception ex ) {
@@ -86,9 +85,8 @@ public async Task UpdateConfiguration(ConfigurationType configurationType,
8685 String id ,
8786 MobileConfiguration mobileConfiguration ,
8887 CancellationToken cancellationToken ) {
89- ConfigurationContext context = await this . ContextFactory . GetContext ( Guid . NewGuid ( ) , "ConfigurationDatabase" , cancellationToken ) ;
90-
91- Configuration ? configuration = await context . Configurations . SingleOrDefaultAsync ( c => c . Id == id && c . ConfigType == ( Int32 ) configurationType , cancellationToken : cancellationToken ) ;
88+ using ResolvedDbContext < ConfigurationContext > ? resolvedContext = this . Resolver . Resolve ( ConfigDatabaseName ) ;
89+ Configuration ? configuration = await resolvedContext . Context . Configurations . SingleOrDefaultAsync ( c => c . Id == id && c . ConfigType == ( Int32 ) configurationType , cancellationToken : cancellationToken ) ;
9290
9391 if ( configuration == null )
9492 {
@@ -103,7 +101,7 @@ public async Task UpdateConfiguration(ConfigurationType configurationType,
103101 ConfigType = ( Int32 ) mobileConfiguration . ConfigurationType ,
104102 Id = mobileConfiguration . Id ,
105103 } ;
106- await context . Configurations . AddAsync ( configuration , cancellationToken ) ;
104+ await resolvedContext . Context . Configurations . AddAsync ( configuration , cancellationToken ) ;
107105 }
108106 else {
109107 configuration . ClientSecret = mobileConfiguration . ClientSecret ;
@@ -114,7 +112,7 @@ public async Task UpdateConfiguration(ConfigurationType configurationType,
114112 configuration . HostAddresses = JsonConvert . SerializeObject ( mobileConfiguration . HostAddresses ) ;
115113 }
116114
117- await context . SaveChangesAsync ( cancellationToken ) ;
115+ await resolvedContext . Context . SaveChangesAsync ( cancellationToken ) ;
118116 }
119117 }
120118}
0 commit comments