Skip to content

Commit 33a5ce0

Browse files
author
채승현
committed
refactor: remove interfaces and use direct dependency injection
- Remove all service interfaces (IFileService, ITemplateService, IFlatBufferCompilerService, ICodeGenerationService) - Replace ServiceContainer with direct class instantiation - Update all services to use concrete types instead of interfaces - Add language parameter to RenderNullableTemplateAsync for proper namespace mapping - Fix nullable template to use mapped_kwd for correct namespace conversion (internal -> _internal)
1 parent 8046d3f commit 33a5ce0

11 files changed

+110
-239
lines changed

Program.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ static async Task<int> Main(string[] args)
3333
return 1;
3434
}
3535

36-
var services = CreateServices();
37-
var processor = new FlatBufferProcessor(services);
36+
var (fileService, templateService, compilerService, codeGenerationService) = CreateServices();
37+
var processor = new FlatBufferProcessor(fileService, templateService, compilerService, codeGenerationService);
3838

3939
await processor.ProcessAsync(config);
4040

@@ -100,15 +100,14 @@ private static AppConfiguration ParseCommandLineArguments(string[] args)
100100
/// <summary>
101101
/// Creates and configures service dependencies
102102
/// </summary>
103-
/// <returns>Service container</returns>
104-
private static ServiceContainer CreateServices()
103+
/// <returns>Tuple containing all service instances</returns>
104+
private static (FileService fileService, TemplateService templateService, FlatBufferCompilerService compilerService, CodeGenerationService codeGenerationService) CreateServices()
105105
{
106-
var services = new ServiceContainer();
107-
services.RegisterSingleton<IFileService, FileService>();
108-
services.RegisterSingleton<ITemplateService, TemplateService>();
109-
services.RegisterSingleton<IFlatBufferCompilerService, FlatBufferCompilerService>();
110-
services.RegisterSingleton<ICodeGenerationService, CodeGenerationService>();
111-
return services;
106+
var fileService = new FileService();
107+
var templateService = new TemplateService(fileService);
108+
var compilerService = new FlatBufferCompilerService(fileService);
109+
var codeGenerationService = new CodeGenerationService(fileService, templateService);
110+
return (fileService, templateService, compilerService, codeGenerationService);
112111
}
113112

114113
/// <summary>

Services/CodeGenerationService.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ namespace FlatBufferEx.Services
55
/// <summary>
66
/// Implementation of code generation operations
77
/// </summary>
8-
public class CodeGenerationService : ICodeGenerationService
8+
public class CodeGenerationService
99
{
10-
private readonly IFileService _fileService;
11-
private readonly ITemplateService _templateService;
10+
private readonly FileService _fileService;
11+
private readonly TemplateService _templateService;
1212

13-
public CodeGenerationService(IFileService fileService, ITemplateService templateService)
13+
public CodeGenerationService(FileService fileService, TemplateService templateService)
1414
{
1515
_fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
1616
_templateService = templateService ?? throw new ArgumentNullException(nameof(templateService));
1717
}
1818

19-
/// <inheritdoc />
19+
/// <summary>
20+
/// Generates raw FlatBuffer files from context
21+
/// </summary>
2022
public async Task<IEnumerable<string>> GenerateRawFlatBufferFilesAsync(Context context, string outputPath, string language)
2123
{
2224
var generatedFiles = new List<string>();
@@ -53,7 +55,7 @@ public async Task<IEnumerable<string>> GenerateRawFlatBufferFilesAsync(Context c
5355
// Generate .fbs files for nullable fields
5456
foreach (var nullableField in context.NullableFields)
5557
{
56-
var contents = await _templateService.RenderNullableTemplateAsync(nullableField);
58+
var contents = await _templateService.RenderNullableTemplateAsync(nullableField, language);
5759
var fileName = $"nullable_{string.Join('_', nullableField.FixedNamespace.Concat(new[] { nullableField.Type }))}.fbs".ToLower();
5860
var filePath = _fileService.CombinePath(outputPath, fileName);
5961

@@ -64,7 +66,9 @@ public async Task<IEnumerable<string>> GenerateRawFlatBufferFilesAsync(Context c
6466
return generatedFiles;
6567
}
6668

67-
/// <inheritdoc />
69+
/// <summary>
70+
/// Generates final code file for the target language
71+
/// </summary>
6872
public async Task GenerateLanguageCodeAsync(Context context, string language, string outputPath, string includePath)
6973
{
7074
var content = await _templateService.RenderLanguageTemplateAsync(context, language, includePath);

Services/FileService.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,49 @@ namespace FlatBufferEx.Services
33
/// <summary>
44
/// Implementation of file system operations
55
/// </summary>
6-
public class FileService : IFileService
6+
public class FileService
77
{
8-
/// <inheritdoc />
8+
/// <summary>
9+
/// Reads all text from a file asynchronously
10+
/// </summary>
911
public async Task<string> ReadAllTextAsync(string path)
1012
{
1113
return await File.ReadAllTextAsync(path);
1214
}
1315

14-
/// <inheritdoc />
16+
/// <summary>
17+
/// Writes all text to a file asynchronously
18+
/// </summary>
1519
public async Task WriteAllTextAsync(string path, string content)
1620
{
1721
var directory = GetDirectoryName(path);
1822
if (!string.IsNullOrEmpty(directory) && !DirectoryExists(directory))
1923
{
2024
CreateDirectory(directory);
2125
}
22-
26+
2327
await File.WriteAllTextAsync(path, content);
2428
}
2529

26-
/// <inheritdoc />
30+
/// <summary>
31+
/// Checks if a directory exists
32+
/// </summary>
2733
public bool DirectoryExists(string path)
2834
{
2935
return Directory.Exists(path);
3036
}
3137

32-
/// <inheritdoc />
38+
/// <summary>
39+
/// Creates a directory
40+
/// </summary>
3341
public void CreateDirectory(string path)
3442
{
3543
Directory.CreateDirectory(path);
3644
}
3745

38-
/// <inheritdoc />
46+
/// <summary>
47+
/// Deletes a directory and all its contents
48+
/// </summary>
3949
public void DeleteDirectory(string path)
4050
{
4151
if (DirectoryExists(path))
@@ -44,28 +54,36 @@ public void DeleteDirectory(string path)
4454
}
4555
}
4656

47-
/// <inheritdoc />
57+
/// <summary>
58+
/// Gets files matching a search pattern
59+
/// </summary>
4860
public string[] GetFiles(string path, string searchPattern)
4961
{
5062
return Directory.GetFiles(path, searchPattern);
5163
}
5264

53-
/// <inheritdoc />
65+
/// <summary>
66+
/// Combines path components
67+
/// </summary>
5468
public string CombinePath(params string[] paths)
5569
{
5670
return Path.Combine(paths);
5771
}
5872

59-
/// <inheritdoc />
73+
/// <summary>
74+
/// Gets the full path for a relative path
75+
/// </summary>
6076
public string GetFullPath(string path)
6177
{
6278
return Path.GetFullPath(path);
6379
}
6480

65-
/// <inheritdoc />
81+
/// <summary>
82+
/// Gets the directory name from a file path
83+
/// </summary>
6684
public string GetDirectoryName(string path)
6785
{
6886
return Path.GetDirectoryName(path) ?? string.Empty;
6987
}
7088
}
71-
}
89+
}

Services/FlatBufferCompilerService.cs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,32 @@ namespace FlatBufferEx.Services
88
/// <summary>
99
/// Implementation of FlatBuffer compiler operations
1010
/// </summary>
11-
public class FlatBufferCompilerService : IFlatBufferCompilerService
11+
public class FlatBufferCompilerService
1212
{
13-
private readonly IFileService _fileService;
13+
private readonly FileService _fileService;
1414

15-
public FlatBufferCompilerService(IFileService fileService)
15+
public FlatBufferCompilerService(FileService fileService)
1616
{
1717
_fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
1818
}
1919

20-
/// <inheritdoc />
20+
/// <summary>
21+
/// Sets up the FlatBuffer compiler by downloading and extracting it
22+
/// </summary>
2123
public async Task SetupCompilerAsync(string downloadUrl, string extractPath)
2224
{
2325
#if !DEBUG
2426
const string zipFileName = "flatbuffer.zip";
25-
27+
2628
// Download the compiler
2729
await Http.DownloadFile(downloadUrl, zipFileName);
28-
30+
2931
// Clean up existing directory
3032
_fileService.DeleteDirectory(extractPath);
31-
33+
3234
// Extract the compiler
3335
ZipFile.ExtractToDirectory(zipFileName, extractPath);
34-
36+
3537
// Clean up zip file
3638
if (File.Exists(zipFileName))
3739
{
@@ -43,12 +45,14 @@ public async Task SetupCompilerAsync(string downloadUrl, string extractPath)
4345
#endif
4446
}
4547

46-
/// <inheritdoc />
48+
/// <summary>
49+
/// Compiles FlatBuffer schema files for the target language
50+
/// </summary>
4751
public async Task<bool> CompileAsync(string language, IEnumerable<string> inputFiles, string outputPath, string includePath, string compilerPath)
4852
{
4953
var environment = GetCompilerEnvironment(language);
5054
var inputFileList = inputFiles.ToList();
51-
55+
5256
if (!inputFileList.Any())
5357
{
5458
throw new ArgumentException("No input files specified", nameof(inputFiles));
@@ -61,17 +65,17 @@ public async Task<bool> CompileAsync(string language, IEnumerable<string> inputF
6165
// Split files into batches to avoid Windows command line length limit (~8,191 characters)
6266
const int maxCommandLineLength = 7000; // Leave some margin for safety
6367
var batches = SplitIntoBatches(inputFileList, outputPath, includePath, environment, maxCommandLineLength);
64-
68+
6569
Console.WriteLine($" Compiling {inputFileList.Count} files in {batches.Count} batch(es)...");
66-
70+
6771
// Compile each batch
6872
for (int i = 0; i < batches.Count; i++)
6973
{
7074
var batch = batches[i];
7175
Console.WriteLine($" Batch {i + 1}/{batches.Count}: {batch.Count} files");
72-
76+
7377
var arguments = BuildCompilerArguments(environment, batch, outputPath, includePath);
74-
78+
7579
// Execute compiler
7680
var process = new Process
7781
{
@@ -144,26 +148,26 @@ private static List<List<string>> SplitIntoBatches(IList<string> inputFiles, str
144148
var batches = new List<List<string>>();
145149
var currentBatch = new List<string>();
146150
var baseArgsLength = CalculateBaseArgsLength(environment, outputPath, includePath);
147-
151+
148152
foreach (var file in inputFiles)
149153
{
150154
var fileArgLength = $"\"{file}\"".Length + 1; // +1 for space
151155
var currentBatchLength = baseArgsLength + currentBatch.Sum(f => $"\"{f}\"".Length + 1);
152-
156+
153157
if (currentBatchLength + fileArgLength > maxLength && currentBatch.Count > 0)
154158
{
155159
batches.Add(currentBatch);
156160
currentBatch = new List<string>();
157161
}
158-
162+
159163
currentBatch.Add(file);
160164
}
161-
165+
162166
if (currentBatch.Count > 0)
163167
{
164168
batches.Add(currentBatch);
165169
}
166-
170+
167171
return batches;
168172
}
169173

@@ -177,12 +181,12 @@ private static List<List<string>> SplitIntoBatches(IList<string> inputFiles, str
177181
private static int CalculateBaseArgsLength(string environment, string outputPath, string includePath)
178182
{
179183
var length = $"--{environment} -o \"{outputPath}\"".Length + 1; // +1 for space before file args
180-
184+
181185
if (!string.IsNullOrWhiteSpace(includePath))
182186
{
183187
length += $" -I \"{includePath}\"".Length;
184188
}
185-
189+
186190
return length;
187191
}
188192

@@ -213,4 +217,4 @@ private static string BuildCompilerArguments(string environment, IList<string> i
213217
return string.Join(" ", args);
214218
}
215219
}
216-
}
220+
}

Services/FlatBufferProcessor.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ namespace FlatBufferEx.Services
77
/// </summary>
88
public class FlatBufferProcessor
99
{
10-
private readonly ServiceContainer _services;
11-
private readonly IFileService _fileService;
12-
private readonly ITemplateService _templateService;
13-
private readonly IFlatBufferCompilerService _compilerService;
14-
private readonly ICodeGenerationService _codeGenerationService;
10+
private readonly FileService _fileService;
11+
private readonly TemplateService _templateService;
12+
private readonly FlatBufferCompilerService _compilerService;
13+
private readonly CodeGenerationService _codeGenerationService;
1514

16-
public FlatBufferProcessor(ServiceContainer services)
15+
public FlatBufferProcessor(FileService fileService, TemplateService templateService, FlatBufferCompilerService compilerService, CodeGenerationService codeGenerationService)
1716
{
18-
_services = services ?? throw new ArgumentNullException(nameof(services));
19-
_fileService = _services.GetService<IFileService>();
20-
_templateService = _services.GetService<ITemplateService>();
21-
_compilerService = _services.GetService<IFlatBufferCompilerService>();
22-
_codeGenerationService = _services.GetService<ICodeGenerationService>();
17+
_fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
18+
_templateService = templateService ?? throw new ArgumentNullException(nameof(templateService));
19+
_compilerService = compilerService ?? throw new ArgumentNullException(nameof(compilerService));
20+
_codeGenerationService = codeGenerationService ?? throw new ArgumentNullException(nameof(codeGenerationService));
2321
}
2422

2523
/// <summary>
@@ -117,7 +115,7 @@ private void CleanupTemporaryFiles(AppConfiguration config)
117115
#if !DEBUG
118116
// Clean up compiler directory
119117
_fileService.DeleteDirectory(config.CompilerDirectory);
120-
118+
121119
// Clean up any remaining temporary files
122120
_fileService.DeleteDirectory(config.TempDirectory);
123121
#endif

Services/ICodeGenerationService.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)