2024-01-26 15:50:41 +03:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using System.Reflection;
|
|
|
|
|
2024-02-08 17:11:11 +03:00
|
|
|
namespace RhSolutions.Parsers;
|
2024-01-26 15:50:41 +03:00
|
|
|
|
2024-02-08 17:11:11 +03:00
|
|
|
public static class ParsersRegistration
|
2024-01-26 15:50:41 +03:00
|
|
|
{
|
2024-02-09 16:48:42 +03:00
|
|
|
public static void AddProductParsers(this IServiceCollection services)
|
2024-01-26 15:50:41 +03:00
|
|
|
{
|
|
|
|
var types = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
.SelectMany(s => s.GetTypes())
|
2024-02-08 17:11:11 +03:00
|
|
|
.Where(p => p.IsDefined(typeof(ParserKey), true));
|
2024-01-26 15:50:41 +03:00
|
|
|
|
|
|
|
foreach (Type t in types)
|
|
|
|
{
|
2024-02-09 16:48:42 +03:00
|
|
|
string key = GetParserKey(t);
|
2024-02-08 17:11:11 +03:00
|
|
|
services.AddKeyedTransient(typeof(IProductParser), key, t);
|
2024-01-26 15:50:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 16:48:42 +03:00
|
|
|
private static string GetParserKey(Type t)
|
2024-01-26 15:50:41 +03:00
|
|
|
{
|
2024-02-08 17:11:11 +03:00
|
|
|
return t.GetCustomAttribute<ParserKey>()?.Value ?? string.Empty;
|
2024-01-26 15:50:41 +03:00
|
|
|
}
|
|
|
|
}
|