Skip to content

Commit ebde1c5

Browse files
committed
feat: DI
1 parent 1f9335a commit ebde1c5

12 files changed

Lines changed: 45 additions & 50 deletions

File tree

src/MapperAI.Core/Clients/MapperClientFactory.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@
55

66
namespace MapperAI.Core.Clients;
77

8-
public class MapperClientFactory : IMapperClientFactory
8+
public class MapperClientFactory(IMapperSerializer serializer, HttpClient httpClient) : IMapperClientFactory
99
{
10-
private readonly IMapperSerializer _serializer;
11-
private readonly HttpClient _httpClient;
12-
13-
public MapperClientFactory(IMapperSerializer serializer, HttpClient httpClient)
14-
{
15-
_serializer = serializer;
16-
_httpClient = httpClient;
17-
}
18-
1910
public IMapperClient CreateClient(MapperClientConfiguration configuration)
2011
{
2112
switch (configuration.Type)
2213
{
2314
case ModelType.Ollama:
24-
return new OllamaMapperClient(configuration, _serializer, _httpClient);
15+
return new OllamaMapperClient(configuration, serializer, httpClient);
2516

2617
default:
27-
return new GeminiMapperClient(configuration, _serializer, _httpClient);
18+
return new GeminiMapperClient(configuration, serializer, httpClient);
2819
}
2920
}
3021
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
using MapperAI.Core.Enums;
2+
using MapperAI.Core.Extensions.Enums;
23

34
namespace MapperAI.Core.Clients.Models;
45

56
public class MapperClientConfiguration
67
{
7-
public string Model { get; set; }
88
public string? ApiKey { get; set; }
99
public ModelType Type { get; set; }
10-
11-
12-
public MapperClientConfiguration(string model, string? apiKey, ModelType type)
10+
public string Model => Type.GetEnumDescriptionValue();
11+
12+
public MapperClientConfiguration( string? apiKey, ModelType type)
1313
{
14-
Model = model;
1514
ApiKey = apiKey;
1615
Type = type;
1716
}
1817

19-
public MapperClientConfiguration(string model, ModelType type)
20-
{
21-
Model = model;
22-
Type = type;
23-
}
24-
2518
}

src/MapperAI.Core/DI/DependencyInjection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace MapperAI.Core.DI;
1212

1313
public static class DependencyInjection
1414
{
15-
public static IServiceCollection AddMapperAI(this IServiceCollection services, string apiKey, string model, ModelType modelType)
15+
public static IServiceCollection AddMapperAI(this IServiceCollection services, string apiKey, ModelType modelType)
1616
{
17-
services.AddMapperClientConfiguration(apiKey, model, modelType);
17+
services.AddMapperClientConfiguration(apiKey, modelType);
1818
services.AddHttpClient();
1919
services.AddSingleton<IMapperSerializer, MapperSerializer>();
2020
services.AddSingleton<IMapperClientFactory, MapperClientFactory>();
@@ -27,9 +27,9 @@ public static IServiceCollection AddMapperAI(this IServiceCollection services, s
2727
return services;
2828
}
2929

30-
private static void AddMapperClientConfiguration(this IServiceCollection services, string apiKey, string model, ModelType modelType)
30+
private static void AddMapperClientConfiguration(this IServiceCollection services, string apiKey, ModelType modelType)
3131
{
32-
MapperClientConfiguration configuration = new(apiKey, model, modelType);
32+
MapperClientConfiguration configuration = new(apiKey, modelType);
3333
services.AddSingleton<MapperClientConfiguration>(s => configuration);
3434

3535
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace MapperAI.Core.Enums;
1+
using System.ComponentModel;
2+
3+
namespace MapperAI.Core.Enums;
24

35
public enum ModelType
46
{
5-
6-
Gemini,
7+
[Description("gemini-2.0-flash")]
8+
GeminiFlash2_0,
79
ChatGpt,
810
Ollama
911
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel;
2+
using System.Reflection;
3+
4+
namespace MapperAI.Core.Extensions.Enums;
5+
6+
public static class EnumExtension
7+
{
8+
public static string GetEnumDescriptionValue(this Enum data)
9+
{
10+
FieldInfo? field = data.GetType().GetField(data.ToString());
11+
var attribute = field?.GetCustomAttribute<DescriptionAttribute>();
12+
return attribute?.Description ?? throw new InvalidEnumArgumentException();
13+
}
14+
}

src/MapperAI.Core/Initializers/DependencyInitializer.cs renamed to src/MapperAI.Core/Extensions/Initializers/InitializerExtension.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Reflection;
1+
using System.Reflection;
22

3-
namespace MapperAI.Core.Initializers;
3+
namespace MapperAI.Core.Extensions.Initializers;
44

5-
public class DependencyInitializer
5+
public static class InitializerExtension
66
{
7-
8-
public static void Initialize(object obj)
7+
public static void Initialize(this object obj)
98
{
109
var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
1110
var genericProperties = properties.Where(x => x.PropertyType.IsGenericType);
@@ -17,6 +16,7 @@ public static void Initialize(object obj)
1716
}
1817
}
1918
}
19+
2020
private static void InitializeDependencyProperties(Type? itemType, PropertyInfo property, object obj)
2121
{
2222
if (itemType == null)
@@ -47,5 +47,4 @@ private static void InitializeDependencyProperties(Type? itemType, PropertyInfo
4747

4848
property.SetValue(obj, listInstance);
4949
}
50-
5150
}

src/MapperAI.Core/Mappers/Models/FileMapperConfiguration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class FileMapperConfiguration
66
public string InputFolder { get; set; }
77
public string? NameSpace { get; set; }
88
public string Extension { get; set; } = "C#";
9-
109
public string? LanguageVersion { get; set; }
1110
public string? FileName { get; set; }
1211
public bool IsUniqueClass => FileName != null;

src/MapperAI.Core/Mappers/PdfMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using iText.Kernel.Pdf.Canvas.Parser;
33
using MapperAI.Core.Clients.Interfaces;
44
using MapperAI.Core.Clients.Models;
5-
using MapperAI.Core.Initializers;
5+
using MapperAI.Core.Extensions.Initializers;
66
using MapperAI.Core.Mappers.Interfaces;
77
using MapperAI.Core.Serializers.Interfaces;
88

@@ -27,7 +27,7 @@ public PdfMapper(IMapperSerializer serializer, IMapperClientFactory mapperClient
2727
IMapperClient iai = _mapperClientFactory.CreateClient(_clientConfiguration);
2828
string pdfContent = ExtractPdfContent(pdfPath);
2929
T destinyObject = new T();
30-
DependencyInitializer.Initialize(destinyObject);
30+
destinyObject.Initialize();
3131
string prompt = CreatePrompt(pdfContent, _serializer.Serialize(destinyObject));
3232
MapperClientResponse result = await iai.SendAsync(prompt, cancellationToken);
3333
return _serializer.Deserialize<T>(result.Value);

test/MapperAI.Test/ClassMapperTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace MapperAI.Test;
88
public class ClassMapperTests : BaseTests
99
{
1010
private readonly IClassMapper _classMapper;
11-
private readonly MapperClientConfiguration _clientConfiguration;
1211

1312
public ClassMapperTests()
1413
{
15-
_clientConfiguration = new MapperClientConfiguration("gemini-2.0-flash", Environment.GetEnvironmentVariable("GEMINI_KEY"),ModelType.Gemini);
16-
_classMapper = new ClassMapper(Serializer, Factory, _clientConfiguration);
14+
var clientConfiguration = new MapperClientConfiguration(Environment.GetEnvironmentVariable("GEMINI_KEY"),ModelType.GeminiFlash2_0);
15+
_classMapper = new ClassMapper(Serializer, Factory, clientConfiguration);
16+
1717
}
1818

1919

test/MapperAI.Test/DI/DependencyInjectionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void AddMapperAI_ShouldRegisterServices()
1515
var services = new ServiceCollection();
1616

1717
// Act
18-
services.AddMapperAI("fake-key", "gemini-2.0-flash", ModelType.Gemini);
18+
services.AddMapperAI("fake-key", ModelType.GeminiFlash2_0);
1919
var provider = services.BuildServiceProvider();
2020

2121
// Assert

0 commit comments

Comments
 (0)