aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions
Apache License 2.0
874 stars 319 forks source link

Adding support for constrained open generics #635

Closed jbogard closed 5 years ago

jbogard commented 6 years ago

(again)

See #471 for details. Figured I'd give this another shot.

The general scenario is:

Suppose I have some interface, IRepository<T>. This interface allows me to save an entity to a database, perhaps wrapping EFCore DbContext. As part of saving, I want to validate so I have IValidator<T>.

The implementation DbContextRepository<T> uses an enumerable of IValidator<T> to validate:

class DbContextRepository<T> {
    public DbContextRepository(
        DbContext db, 
        IEnumerable<IValidator<T>> validators) {
        // etc
    }
}

I want to be able to support:

class IFooValidator<T> : IValidator<T> where T : IFoo { }

instead of

class IFooValidator : IValidator<IFoo> {}

For the reasons in #471.

In case anything has changed, just wanted to throw this out there. I modified the approach to do what Autofac does, which is actually check the constraints if they exist (instead of the previous try-catch-swallow approach).

muratg commented 5 years ago

@davidfowl could you either merge or close this? @natemcmaster is going to move this to the mondo repo and moving PRs are tricky. Thanks.

jbogard commented 5 years ago

I vote the first ;)

On Wed, Oct 17, 2018 at 5:10 PM Murat Girgin notifications@github.com wrote:

@davidfowl https://github.com/davidfowl could you either merge or close this? @natemcmaster https://github.com/natemcmaster is going to move this to the mondo repo and moving PRs are tricky. Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aspnet/DependencyInjection/pull/635#issuecomment-430807377, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGYMmYawtJ75SkkLOCwE5HqlfhY_6lSks5ul6rJgaJpZM4TNU2C .

pakrym commented 5 years ago

We would like to get this feature in for 3.0 but in a bit different shape (I managed to convince @davidfowl to catch first chance exceptions). It means, unfortunately, this PR would have to be closed and reopened against new repo.

jbogard commented 5 years ago

I’ll reopen on the new repo when it shows up then.

On Wed, Oct 17, 2018 at 5:44 PM Pavel Krymets notifications@github.com wrote:

We would like to get this feature in for 3.0 but in a bit different shape (I managed to convince @davidfowl https://github.com/davidfowl to catch first chance exceptions). It means, unfortunately, this PR would have to be closed and reopened against new repo.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aspnet/DependencyInjection/pull/635#issuecomment-430815017, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGYMkd2XRmpyv0YNhE4UyvaP6W9UHG3ks5ul7K1gaJpZM4TNU2C .

muratg commented 5 years ago

Thanks @jbogard!

davidfowl commented 5 years ago

first chance exceptions are the 👿. We should do what we can in ns2.0 and ask for new APIs in corefx to make it possible to avoid throwing.

jbogard commented 5 years ago

@davidfowl I agree but this is what every container developer has had to deal with. May be easier now to expose the APIs now.