modesttree / Zenject

Dependency Injection Framework for Unity3D

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Suggestion] Create extension using Microsoft.Extensions.DependencyInjection.Abstractions

schodemeiss opened this issue · comments

It would be nice if we could create an Adaptor or Extension for Extenject that extends the Microsoft.Extensions.DependencyInjection.Abstractions lib. That way, we can rely on an abstracted DIContainer rather than binding our game projects directly to Extenject. However, I appreciate there's a lot of Unity specific things in Extenject.

Other DI's have done this, so I'm sure it's possible. Please see: https://github.com/aspnet/Extensions/tree/master/src/DependencyInjection

I'm currently working on a solution for this, but i hit some conceptual problems I can't seem to solve.

Working but not ideal solution
The problem: this works, but it requires instantiation of every type which is registered with a factory method(see line 24), which is not a desired solution and could cause problems(ie.: singleton classes which contains constructor logic that is expected to run at the time of first instantiation).

Not working solution, seems ideal but don't understand the problem.

When I try to register a service which is registered with an implementation factory i hit a binding assert stating: Expected type 'object' to derive from type 'IExampleInterface'
(see line 22)

I know the registration is working, im experimenting with a service collection which is already working in a wpf application where we just use the IServiceProvider.

Any idea how to solve this? I don't really understand why this happens with from method.
Also tried:

binding = Container.Bind(service.ServiceType).FromMethod(_ => { var implementation = service.ImplementationFactory(serviceProvider); return Convert.ChangeType(implementation, implementation.GetType()); });

Which did not help either and produced the same error message

So, fast forward 10 months, I have managed to solve this. The package is available on Github and OpenUPM.