question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Separate dependency injection for framework and migrations

See original GitHub issue

When using third party dependency injection frameworks together with .NET Core, you’ll end up with two IOC containers. One provided by Microsoft for framework dependencies, and one for the application dependencies. I’ll link to some articles of why that is: https://simpleinjector.readthedocs.io/en/latest/servicecollectionintegration.html https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0#default-service-container-replacement

Anyway, it would be nice to be able to explicitly set the implementation of IServiceProvider for the migrations dependency injection. One could argue that the FluentMigration framework IOC registrations belong to the framework of the application, and that the migrations, which are custom written by the integrator, belongs to the applications IOC registrations, and therefor it makes sense being able to separate them.

It is doable already, but the solution becomes quite hacky and dependable on that the registration of the MigrationSource is done accordingly to FluentMigratorServiceCollectionExtensions.AddFluentMigratorCore.

internal static class MigrationRunnerBuilderExtensions
{
    internal static IMigrationRunnerBuilder UseSimpleInjectorForMigrationDependencies(
        this IMigrationRunnerBuilder migrationRunnerBuilder,
        Container container)
    {
        // In order to use simple injector as dependency resolver, the migration source needs to be overridden.
        // The overridden registration can be found here:
        // https://github.com/fluentmigrator/fluentmigrator/blob/ab8e602b2a84f529812ef10791bed4740522d2ed/src/FluentMigrator.Runner/FluentMigratorServiceCollectionExtensions.cs#L89
        migrationRunnerBuilder.Services.Replace(new ServiceDescriptor(typeof(IMigrationSource),
            serviceProvider => new MigrationSource(
                serviceProvider.GetRequiredService<IAssemblySource>(),
                serviceProvider.GetRequiredService<IMigrationRunnerConventions>(),
                container,
                serviceProvider.GetServices<IMigrationSourceItem>()), ServiceLifetime.Scoped));

        return migrationRunnerBuilder;
    }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jzabroskicommented, Sep 27, 2019

I read it, but I need time to think. I respect SimpleInjector authors.

My initial thoughts are: I thought by Microsoft DI existing and ASP.NET Core eliminating static state, we had gotten rid of “DI tunneling” issues (e.g., recording CreatedBy on a EF object using ASP.NET Identity in ASP.NET Classic required tunneling because ASP.NET threading model doesn’t gaurantee you’ll be on the same thread throughout a request’s lifecycle, and if you wanted to add EF objects to a database in parallel, you had to use multiple threads, which broke the ASP.NET Classic Identity chain). After reading the SimpleInjector article, it seems it merely pushed some tunneling up a level. It’s cleaner, but still exists. As best as I can tell, it has to exist.

The remaining questions are likely around this piece of the article:

Simple Injector’s auto cross wiring has the following limitations:

finally:

If you added an overload to AddFluentMigratorCore with another IServiceProvider, would it not be a bit confusing for the integrator considering AddFluentMigratorCore is an extension method on the builtin DI registrator?

I think the general intent is to do something similar to UseSimpleInjector. In that sense, I guess we come full circle and I’m starting to see how your “hack” code above can be cleaned up a bit and generalized.

1reaction
jzabroskicommented, Sep 27, 2019

Thank you, that was a very helpful summary. Sorry for my spastic reply. It takes time going through the daily churn of new issues on this project and sometimes I make inaccurate first guesses as to what the problem is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I inject dependency into migration (using EF-Core ...
I tried to inject IConfiguration into the migration (in constructor), and got exception: "No parameterless constructor defined for this object.".
Read more >
Using a Separate Migrations Project - EF Core
Using a separate migration project for managing database schemas with Entity Framework Core.
Read more >
.NET Core — Using Entity Framework Core in a separate ...
In order to our API project use the context we defined in the DAL project, it must be added through Dependency Injection in...
Read more >
Dependency injection guidelines - .NET
This article provides general guidelines and best practices for implementing dependency injection in .NET applications.
Read more >
Dependency Injection and Different ways to inject it using . ...
The purpose behind the dependency injection design patterns:​​ In simple words, dependency means the object depends on another object to do some work....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found