-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from mewlist/property-injection
Property Injection
- Loading branch information
Showing
14 changed files
with
167 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Doinject | ||
{ | ||
internal class PropertyInjector | ||
{ | ||
private DIContainer Container { get; } | ||
private ParameterBuilder ParameterBuilder { get; } | ||
|
||
public PropertyInjector(DIContainer container) | ||
{ | ||
Container = container; | ||
ParameterBuilder = container.ParameterBuilder; | ||
} | ||
|
||
public async ValueTask DoInject<T>( | ||
T target, | ||
TargetPropertiesInfo properties) | ||
{ | ||
if (!properties.Any()) return; | ||
var targetType = target.GetType(); | ||
var resolverType = targetType; | ||
|
||
if (targetType == typeof(IInjectableComponent)) | ||
resolverType = target.GetType(); | ||
|
||
Container.MarkInjected(resolverType); | ||
|
||
foreach (var propertyInfo in properties.InjectProperties) | ||
{ | ||
var instance = await Container.ResolveAsync(propertyInfo.PropertyType); | ||
propertyInfo.SetValue(target, instance); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace Doinject | ||
{ | ||
internal class TargetPropertiesInfo | ||
{ | ||
public List<PropertyInfo> InjectProperties { get; } = new(); | ||
|
||
public TargetPropertiesInfo(Type targetType) | ||
{ | ||
foreach (var propertyInfo in targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) | ||
{ | ||
if (propertyInfo.GetCustomAttributes(typeof(InjectAttribute), true).Length <= 0) | ||
continue; | ||
|
||
if (!propertyInfo.CanWrite || !propertyInfo.SetMethod.IsPublic) | ||
throw new Exception($"Property must have a public setter. {targetType.Name}.{propertyInfo.Name}"); | ||
|
||
InjectProperties.Add(propertyInfo); | ||
} | ||
} | ||
|
||
public bool Any() | ||
=> InjectProperties.Count > 0; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using UnityEngine; | ||
|
||
namespace Doinject.Tests | ||
{ | ||
public class PropertyInjectionComponent : MonoBehaviour, IInjectableComponent | ||
{ | ||
[Inject] | ||
public InjectedObject InjectedObject { get; set; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using UnityEngine; | ||
|
||
namespace Doinject.Tests | ||
{ | ||
public class PropertyInjectionWithNonPublicSetterComponent : MonoBehaviour, IInjectableComponent | ||
{ | ||
[Inject] | ||
public InjectedObject InjectedObject { get; private set; } | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters