Skip to content

Commit aaf8c38

Browse files
calin-lupas_dsamcapscalin-lupas_dsamcaps
authored andcommitted
Refactor AddDataStore for improved modularity
This commit refactors the `AddDataStore` method in `ServiceExtensions.cs` by removing inline logic for creating storage contexts and repositories. It introduces private methods for instantiation based on data store type, enhancing code readability and maintainability. The method now registers repositories directly as singletons, simplifying the service registration process.
1 parent 3e8016d commit aaf8c38

1 file changed

Lines changed: 50 additions & 48 deletions

File tree

src/DevExcelerateApi/Core/Extensions/ServiceExtensions.cs

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -120,62 +120,64 @@ internal static IServiceCollection AddCorsPolicy(this IServiceCollection service
120120
policy.WithOrigins(allowedOrigins)
121121
.WithMethods("POST", "GET", "PUT", "DELETE", "PATCH")
122122
.AllowAnyHeader();
123-
});
123+
});
124124
});
125-
125+
126126
return services;
127127
}
128-
128+
129129
internal static IServiceCollection AddDataStore(this IServiceCollection services)
130130
{
131-
services.AddSingleton(sp=>
132-
{
133-
IStorageContext<GitHubWebhookEntity> githubWebhookStorageContext;
134-
DataStoreOptions dataStoreOptions = sp.GetRequiredService<IOptions<DataStoreOptions>>().Value;
135-
switch (dataStoreOptions.Type)
136-
{
137-
case DataStoreOptions.DataStoreType.FileSystem:
138-
139-
if (dataStoreOptions.FileSystem == null)
140-
{
141-
throw new InvalidOperationException("DataStore:FileSystem is required when DataStore:Type is 'FileSystem'");
142-
}
143-
144-
var fullPath = Path.GetFullPath(dataStoreOptions.FileSystem.FilePath);
145-
var directory = Path.GetDirectoryName(fullPath) ?? string.Empty;
146-
147-
githubWebhookStorageContext = new FileSystemStorageContext<GitHubWebhookEntity>(
148-
new FileInfo(Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(fullPath)}_GitHubWebhooks{Path.GetExtension(fullPath)}")));
149-
break;
150-
case DataStoreOptions.DataStoreType.CosmosDb:
151-
152-
if (dataStoreOptions.CosmosDb == null)
153-
{
154-
throw new InvalidOperationException("DataStore:CosmosDb is required when DataStore:Type is 'CosmosDb'");
155-
}
156-
157-
githubWebhookStorageContext = new CosmosDbStorageContext<GitHubWebhookEntity>(
158-
dataStoreOptions.CosmosDb.ConnectionString,
159-
dataStoreOptions.CosmosDb.Database,
160-
dataStoreOptions.CosmosDb.GitHubWebhooksContainer);
161-
break;
162-
case DataStoreOptions.DataStoreType.SqlServer:
163-
if (dataStoreOptions.SqlDb == null)
164-
{
165-
throw new InvalidOperationException("DataStore:SqlDb is required when DataStore:Type is 'SqlServer'");
166-
}
167-
githubWebhookStorageContext = new SqlDbStorageContext<GitHubWebhookEntity>(dataStoreOptions.SqlDb!);
168-
break;
169-
default:
170-
{
171-
throw new InvalidOperationException("Invalid 'DataStore' setting 'dataStoreOptions.Type'.");
172-
}
173-
}
131+
// Register storage contexts as singletons first
132+
services.AddSingleton(CreateStorageContext<GitHubWebhookEntity>);
133+
services.AddSingleton(CreateStorageContext<IssueRequestEntity>);
174134

175-
return new GitHubWebhookRepository(githubWebhookStorageContext);
176-
});
135+
// Register repositories
136+
services.AddSingleton<GitHubWebhookRepository>();
137+
services.AddSingleton<IssueRequestRepository>();
177138

178139
return services;
179140
}
141+
142+
private static IStorageContext<T> CreateStorageContext<T>(IServiceProvider sp) where T : IStorageEntity
143+
{
144+
var dataStoreOptions = sp.GetRequiredService<IOptions<DataStoreOptions>>().Value;
145+
146+
return dataStoreOptions.Type switch
147+
{
148+
DataStoreOptions.DataStoreType.FileSystem => CreateFileSystemStorageContext<T>(dataStoreOptions.FileSystem),
149+
DataStoreOptions.DataStoreType.CosmosDb => CreateCosmosDbStorageContext<T>(dataStoreOptions.CosmosDb),
150+
DataStoreOptions.DataStoreType.SqlServer => CreateSqlServerStorageContext<T>(dataStoreOptions.SqlDb),
151+
_ => throw new InvalidOperationException($"Invalid 'DataStore' setting '{dataStoreOptions.Type}'.")
152+
};
153+
}
154+
155+
private static FileSystemStorageContext<T> CreateFileSystemStorageContext<T>(FileSystemOptions? options) where T : IStorageEntity
156+
{
157+
if (options == null)
158+
throw new InvalidOperationException("DataStore:FileSystem is required when DataStore:Type is 'FileSystem'");
159+
160+
var fullPath = Path.GetFullPath(options.FilePath);
161+
var directory = Path.GetDirectoryName(fullPath) ?? string.Empty;
162+
var fileName = $"{Path.GetFileNameWithoutExtension(fullPath)}_{typeof(T).Name}{Path.GetExtension(fullPath)}";
163+
164+
return new FileSystemStorageContext<T>(new FileInfo(Path.Combine(directory, fileName)));
165+
}
166+
167+
private static CosmosDbStorageContext<T> CreateCosmosDbStorageContext<T>(CosmosDbOptions? options) where T : IStorageEntity
168+
{
169+
if (options == null)
170+
throw new InvalidOperationException("DataStore:CosmosDb is required when DataStore:Type is 'CosmosDb'");
171+
172+
return new CosmosDbStorageContext<T>(options.ConnectionString, options.Database);
173+
}
174+
175+
private static SqlDbStorageContext<T> CreateSqlServerStorageContext<T>(SqlDbOptions? options) where T : IStorageEntity
176+
{
177+
if (options == null)
178+
throw new InvalidOperationException("DataStore:SqlDb is required when DataStore:Type is 'SqlServer'");
179+
180+
return new SqlDbStorageContext<T>(options);
181+
}
180182
}
181183
}

0 commit comments

Comments
 (0)