简体   繁体   中英

Property injection with Simple Injector 1 in Unity3D under .NET 3.5

I'm trying to use Simple Injector v1.6 in combination with the Unity3D game engine ( v5.0 ).
I need to use Property Injection because Unity3D has control over the assembly and will not allow Constructor Injection , because (how unfortunate it may be) that's just the way Unity3D's API is currently.

Unity3D targets the scripting assemblies to .NET 3.5 , so this means that System.ComponontModel.Composition is not available, resulting that I cannot use explicit property injection as described in the Simple Injector documentation .

So my question to the developer(s) of Simple Injector is: ' Is there any other way to utilize property injection into classes that are controlled by the Unity3D engine? '.

There are multiple ways how Simple Injector's default behaviour can be extended when it comes to property injection, but the best way is to override Simple Injector's PropertyInjectionBehavior as explained here .

You say that constructor injection is not an option. This indicates that it is impossible to intercept the creation of some framework types. The Unity3d framework is in control here.

In such situation, you will have to let Simple Injector build up existing types. Never an optimal situation, but there's probably little you can do about that. This wiki page describes how you can let Simple Injector build up external instances.

UPDATE

The answer above is specific to Simple Injector v2. Simple Injector v2 got a lot new compelling features such as the IPropertySelectionBehavior abstraction for enabling property injection, the Registration class to allow building up external instances, and the ExpressionBuilding event to allow intercepting the creation of types to allow injecting properties or fields.

Simple Injector v2 however requires .NET 4.0, so this means that using v2 is not an option for you, because you require .NET 3.5. All those features are not available in Simple Inject v1, so the answer above doesn't make sense for you.

The only option left is to use the Container.InjectProperties(object) method to allowing implicit property injection on externally created instances.

There are however many downsides to implicit property injection, as described here . That's why the InjectProperties method has been marked [Obsolete] since v2.6. The reason for this is described here . For the reasons described there, Simple Injector v3 will not even include the InjectProperties method.

So your only option is to use the InjectProperties method to build up your objects, but please read the referenced articles to understand what the risk of using implicit property injection is. Understanding the risk allows you to make an informed decision about whether this is a risk for you and allows you to apply counter measures, such as writing extra unit tests.

A completely different option, might be the use of Humble Objects . What this means is, that instead of falling back to property injection, you make this class that needs to be created by Unity3D as small as possible with as little code as possible. You move all interesting logic with all its dependencies to a custom component where constructor injection can be applied. Inside the stripped down 'humble' class, you just resolve the newly created component and use it.

This allows you to apply best practices when it comes to dependency injection, without having to fall back to ugly property injection, while keeping the untestable code to an absolute minimum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM