Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Microsoft dependency injection implementation #370

Merged

Conversation

weitzhandler
Copy link
Contributor

@weitzhandler weitzhandler commented Aug 1, 2019

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

An implementation of IDependencyResolver for Microsoft.Extensions.DependencyInjection.
Fixes #362.
Related to #233.

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features) - should be added when merged and package published on NuGet.
  • Docs have been added / updated (for bug fixes / features)

@weitzhandler weitzhandler requested a review from a team August 1, 2019 13:56
@weitzhandler weitzhandler changed the title Microsoft dependency injection implementation feature: Microsoft dependency injection implementation Aug 1, 2019
@glennawatson
Copy link
Contributor

You need to modify build.cake file.

build.cake Show resolved Hide resolved
@glennawatson
Copy link
Contributor

You'll probably have to implement something like a ContractRegistration<TType>, where it has a concurrent dictionary inside with registrations based on contracts. I don't think the scope registrations help in this case.

@glennawatson
Copy link
Contributor

The contract functionality definitely has to remain as is, if a contract exists it's a different "context", this is mostly used by splat/rxui internal functionality.

@glennawatson
Copy link
Contributor

Alright I think that's all the stuff I can find. Rodney will likely do a sweep as well.

Thanks @weitzhandler

@weitzhandler
Copy link
Contributor Author

Thanks for your comments @glennawatson!
I think I misunderstand a bit how Splat DI works in regards to the contracts:

  • What happens if you try to contract-resolve a service that was registered without a contract?
  • How is re-registering a type with a contract affecting the original registration?

@glennawatson
Copy link
Contributor

Essentially contracts give you independent registrations of the original one.

so <A, contract null> is a different regristration pool to <A, contract "blah">

@glennawatson
Copy link
Contributor

Contracts are commonly used by users in the scenario where they want to show different views based on contexts on the same view models.

@glennawatson
Copy link
Contributor

glennawatson commented Aug 1, 2019

public struct ContractInjector<T>
{
   public ConcurrentDictionary<string, T> TypeDictionary = new ConcurrentDictionary<string, T>();
}

public void Register<T>(Func<T> register, string contract)
{
    if (string.IsNullOrWhitespace(contract))
       container.Register<T>(register); // whatever the funciton name is in MSDI 
    else
       container.Register<ContractInjector<T>>(register); // You'll likely have to get any existing ContractInjector's created and input it there against the string.
}

Since this is mostly for RxUI benefit this is sufficient way to fix it.

You can also add extension methods for the MS DI containers so that users can register on both.

Solved.

@weitzhandler
Copy link
Contributor Author

weitzhandler commented Aug 1, 2019

So no scopes at all?

@glennawatson
Copy link
Contributor

Don't think it helps no, it's a similar concept but not quite what we need. Unless you can see a way of achieving the same results with scopes.

@weitzhandler
Copy link
Contributor Author

weitzhandler commented Aug 1, 2019

I'll ditch the scopes entirely, because it's wrong. Say we use it, if we have a contract and a type, it becomes a singleton under that contract, whilst when users register with splat they intend to register transients, right?

Anyway, I'll finish it up a bit later. Thank you two for your comments and help!

@glennawatson
Copy link
Contributor

Yeah, a contract is independent of singleton/transient.

@weitzhandler
Copy link
Contributor Author

Returns null. We shouldn't be throwing exceptions.

Oh, didn't know that. Then I should change the GetRequiredService as well

Btw. Make sure you compile the code before you submit. Also run the build batch file from the root directory too so you know if the unit tests pass.

I did before commiting.

@glennawatson
Copy link
Contributor

It's failing on compile errors btw.

@weitzhandler
Copy link
Contributor Author

Why doesn't HasRegistration have a contract?

@weitzhandler
Copy link
Contributor Author

It's failing on compile errors btw.

Sorry about that. I'll make sure to perform this action on every commit. Apologies.

@glennawatson
Copy link
Contributor

I think hasregistration was added later. Probably should have it.

@weitzhandler
Copy link
Contributor Author

I think hasregistration was added later. Probably should have it.

#372

@glennawatson
Copy link
Contributor

All the resources I found say inheritance of these classes can be a bit dangerous. They aren't really designed for it

@weitzhandler
Copy link
Contributor Author

Currently it passes all included test cases and some more, but I think more testing can be added to the base DependencyResolver in general. I would like to do that in the near future, as I get some available time.

@glennawatson
Copy link
Contributor

@weitzhandler I think apart from that one last multithreading issue I think this is good to go and like you said further tests/monitoring.

I know this has been a long process compared to some other PRs you done so thanks for your persistence.

@weitzhandler
Copy link
Contributor Author

weitzhandler commented Aug 5, 2019

Thank YOU for your persistence, not only with this PR, but with the entire repo, and also for teaching me a lot of stuff, I'm so grateful!

@todo
Copy link

todo bot commented Aug 5, 2019

if list empty remove contract entirely

// TODO if list empty remove contract entirely
// need to find how to atomically update or remove
// https://github.com/dotnet/corefx/issues/24246
return list;
});
}


This comment was generated by todo based on a TODO comment in 5c73294 in #370. cc @weitzhandler.

@glennawatson glennawatson merged commit e48eec3 into reactiveui:master Aug 5, 2019
@weitzhandler weitzhandler deleted the microsoft-dependency-injection branch August 5, 2019 11:03
@weitzhandler
Copy link
Contributor Author

Related: #378

@lock lock bot locked and limited conversation to collaborators Nov 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: Add Microsoft.Extensions.DependencyInjection adapter
3 participants