1

I have been searching for the answer for this. But have yet to find what I want to know. Mohammad asked a similar question here but it was never adequately answered. If anyone can help me understand this it would be greatly appreciated.

First way: Why do this ?:

services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
            sp => (DbConnection c) => new IntegrationEventLogService(c));

As opposed to Second Way this?:

services.AddTransient<IIntegrationEventLogService, IntegrationEventLogService> (sp =>
    {
        var bookCatalogueDbContext = sp.GetRequiredService<BookCatalogueDbContext>();
        var dbConnection = bookCatalogueDbContext.Database.GetDbConnection();

        return new IntegrationEventLogService(dbConnection);
    });

The first example is then called here:

private readonly Func<DbConnection, IIntegrationEventLogService> _integrationEventLogServiceFactory;
    private readonly IEventBus _eventBus;
    private readonly BookCatalogueDbContext _bookCatalogueDbContext;
    private readonly IIntegrationEventLogService _eventLogService;
    private readonly ILogger<BookCatalogueIntegrationEventService> _logger;
    private volatile bool disposedValue;

    public BookCatalogueIntegrationEventService(
        IEventBus eventBus,
        ILogger<BookCatalogueIntegrationEventService> logger,
        BookCatalogueDbContext bookCatalogueDbContext,
        Func<DbConnection, IIntegrationEventLogService> integrationEventLogServiceFactory)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        _bookCatalogueDbContext = bookCatalogueDbContext ?? throw new ArgumentNullException(nameof(bookCatalogueDbContext));
        _integrationEventLogServiceFactory = integrationEventLogServiceFactory ?? throw new ArgumentNullException(nameof(integrationEventLogServiceFactory));
        _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
        _eventLogService = _integrationEventLogServiceFactory(_bookCatalogueDbContext.Database.GetDbConnection());  
    }

The second example this called this way:

private readonly IIntegrationEventLogService _integrationEventLogService;
    private readonly IEventBus _eventBus;
    private readonly BookCatalogueDbContext _bookCatalogueDbContext;
    private readonly ILogger<BookCatalogueIntegrationEventService> _logger;
    private volatile bool disposedValue;

    public BookCatalogueIntegrationEventService(
        IEventBus eventBus,
        ILogger<BookCatalogueIntegrationEventService> logger,
        BookCatalogueDbContext bookCatalogueDbContext,
        IIntegrationEventLogService integrationEventLogService)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        _bookCatalogueDbContext = bookCatalogueDbContext ?? throw new ArgumentNullException(nameof(bookCatalogueDbContext));
        _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
        _integrationEventLogService = integrationEventLogService;  
    }

If and why is the first way better?

Ports
  • 95
  • 7

0 Answers0