Added wrapper class to get handler class to instantiate with correct generic type

This commit is contained in:
Philip Dimitratos 2017-10-11 15:42:00 -07:00
Родитель c357195249
Коммит ad26f86c3b
2 изменённых файлов: 39 добавлений и 7 удалений

Просмотреть файл

@ -36,7 +36,7 @@ namespace Sia.Gateway.Initialization
if (env.IsDevelopment()) services.AddDbContext<IncidentContext>(options => options.UseInMemoryDatabase("Live"));
if (env.IsStaging()) services.AddDbContext<IncidentContext>(options => options.UseSqlServer(config.GetConnectionString("incidentStaging")));
AddTicketingConnector(services, env, config);
services.AddTicketingConnector(env, config);
services.AddScoped<IEventRepository, EventRepository>();
services.AddScoped<IEngagementRepository, EngagementRepository>();
@ -45,7 +45,7 @@ namespace Sia.Gateway.Initialization
services.AddSingleton<AzureActiveDirectoryAuthenticationInfo>(i => incidentAuthConfig);
}
private static void AddTicketingConnector(IServiceCollection services, IHostingEnvironment env, IConfigurationRoot config)
private static void AddTicketingConnector(this IServiceCollection services, IHostingEnvironment env, IConfigurationRoot config)
{
if (TryGetConfigValue(config, "Connector:Ticket:Path", out var ticketConnectorAssemblyPath))
{
@ -111,14 +111,12 @@ namespace Sia.Gateway.Initialization
private static void AddIncidentClient(this IServiceCollection services, Type ticketType)
{
var clientType = typeof(IncidentRepository<>).MakeGenericType(new Type[] { ticketType });
services.AddScoped(typeof(IIncidentRepository), clientType);
services.AddScoped(typeof(IIncidentRepositoryLogic), clientType);
services.AddScoped<IIncidentRepository, IncidentRepositoryWrapper>();
}
public static void AddThirdPartyServices(this IServiceCollection services, IConfigurationRoot config)
{
//Adds every request type in the Sia.Gateway assembly
services.AddMediatR(typeof(GetIncidentRequest).GetTypeInfo().Assembly);
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper, UrlHelper>(iFactory
=> new UrlHelper(iFactory.GetService<IActionContextAccessor>().ActionContext)
@ -148,6 +146,9 @@ namespace Sia.Gateway.Initialization
redisOptions.Options.Password = config["Redis:Password"];
});
services.AddScoped<HubConnectionBuilder>();
//Adds every request type in the Sia.Gateway assembly
services.AddMediatR(typeof(GetIncidentRequest).GetTypeInfo().Assembly);
}
}
}

Просмотреть файл

@ -22,7 +22,7 @@ namespace Sia.Gateway.ServiceRepositories
{
}
public class IncidentRepository<TTicket> : IIncidentRepository
public class IncidentRepository<TTicket> : IIncidentRepositoryLogic
{
private readonly IncidentContext _context;
private readonly Connector<TTicket> _connector;
@ -74,6 +74,37 @@ namespace Sia.Gateway.ServiceRepositories
return Mapper.Map<Incident>(dataIncident);
}
}
//Why does this exist?
//Purely because I haven't been able to get Mediatr to work with generics
public interface IIncidentRepositoryLogic
{
Task<Incident> Handle(GetIncidentRequest getIncident);
Task<IEnumerable<Incident>> Handle(GetIncidentsRequest request);
Task<IEnumerable<Incident>> Handle(GetIncidentsByTicketRequest request);
Task<Incident> Handle(PostIncidentRequest request);
}
public class IncidentRepositoryWrapper : IIncidentRepository
{
private readonly IIncidentRepositoryLogic _actualIncidentRepository;
public IncidentRepositoryWrapper(IIncidentRepositoryLogic actualIncidentRepository)
{
_actualIncidentRepository = actualIncidentRepository;
}
public Task<Incident> Handle(GetIncidentRequest message)
=> _actualIncidentRepository.Handle(message);
public Task<IEnumerable<Incident>> Handle(GetIncidentsRequest message)
=> _actualIncidentRepository.Handle(message);
public Task<IEnumerable<Incident>> Handle(GetIncidentsByTicketRequest message)
=> _actualIncidentRepository.Handle(message);
public Task<Incident> Handle(PostIncidentRequest message)
=> _actualIncidentRepository.Handle(message);
}
}