1- using Microsoft . AspNetCore . Hosting ;
2- using Microsoft . Extensions . Configuration ;
3- using NetCoreStack . Data ;
4- using NetCoreStack . Data . Context ;
5- using NetCoreStack . WebSockets ;
1+ using NetCoreStack . WebSockets ;
62using System ;
7- using System . Collections . Generic ;
8- using System . IO ;
3+ using System . Linq ;
94using System . Threading ;
105using System . Threading . Tasks ;
116using static DataTransform . SharedLibrary . HostingConstants ;
@@ -14,49 +9,26 @@ namespace DataTransform.Api.Hosting
149{
1510 public class TransformManager
1611 {
17- private readonly IHostingEnvironment _hostingEnvironment ;
1812 private readonly IConnectionManager _connectionManager ;
19- private readonly ICollectionNameSelector _collectionNameSelector ;
13+ private readonly TransformTaskFactory _transformTaskFactory ;
2014
2115 public CancellationTokenSource CancellationToken { get ; }
2216
23- public TransformManager ( IHostingEnvironment hostingEnvironment ,
24- IConnectionManager connectionManager ,
25- ICollectionNameSelector collectionNameSelector )
17+ public TransformManager ( TransformTaskFactory transformTaskFactory , IConnectionManager connectionManager )
2618 {
27- _hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException ( nameof ( hostingEnvironment ) ) ;
19+ _transformTaskFactory = transformTaskFactory ?? throw new ArgumentNullException ( nameof ( transformTaskFactory ) ) ;
2820 _connectionManager = connectionManager ?? throw new ArgumentNullException ( nameof ( connectionManager ) ) ;
29- _collectionNameSelector = collectionNameSelector ;
3021 CancellationToken = new CancellationTokenSource ( ) ;
3122 }
3223
33- public async Task TransformAsync ( string filename )
34- {
35- var configFilePath = Path . Combine ( _hostingEnvironment . WebRootPath , "configs" , filename ) ;
36- if ( ! System . IO . File . Exists ( configFilePath ) )
37- {
38- throw new FileNotFoundException ( $ "{ configFilePath } not found.") ;
39- }
24+ public async Task TransformAsync ( string [ ] files )
25+ {
4026
4127 SharedSemaphoreSlim . Wait ( ) ;
4228 try
4329 {
44- var configuration = new ConfigurationBuilder ( ) . AddJsonFile ( configFilePath ) . Build ( ) ;
45- var options = new TransformOptions ( ) ;
46- configuration . Bind ( nameof ( TransformOptions ) , options ) ;
47-
48- List < DbTransformContext > transformContexts = options . CreateTransformContexts ( CancellationToken ) ;
49-
50- var dataContextConfigurationAccessor = new DefaultDataContextConfigurationAccessor ( options ) ;
51- var sqlDatabase = new SqlDatabase ( dataContextConfigurationAccessor ) ;
52- var mongoDbContext = new MongoDbContext ( dataContextConfigurationAccessor , _collectionNameSelector , null ) ;
53-
54- ITransformTask transformTask = new DbTransformTask ( sqlDatabase , mongoDbContext , _connectionManager ) ;
55-
56- foreach ( var context in transformContexts )
57- {
58- await transformTask . InvokeAsync ( context ) ;
59- }
30+ var tasks = _transformTaskFactory . Create ( files , CancellationToken ) ;
31+ await Task . WhenAll ( tasks . Select ( t => t . InvokeAsync ( ) ) ) ;
6032 }
6133 catch ( Exception ex )
6234 {
0 commit comments