Skip to content

Commit

Permalink
Fix Injector OOP instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 1, 2018
1 parent e610836 commit 2fbe812
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Jellyfish.Demo/Injection/InjectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ namespace Jellyfish.Demo.Injection
{
public class InjectionModel
{
private Injector Injector { get; }

public InjectionModel()
{
Injector = new Injector();
}

public IUser TemplateUser()
{
Injector.Template<IUser>(() => new User());
Expand Down
3 changes: 3 additions & 0 deletions Jellyfish.Tests/TestInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ namespace Jellyfish.Tests
[TestClass]
public class TestInjector
{
private Injector Injector { get; set; }

[TestInitialize]
public void Initialize()
{
Injector = new Injector();
}

[TestMethod]
Expand Down
6 changes: 3 additions & 3 deletions Jellyfish/DependencyInjection/InjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static class InjectionExtensions
/// <typeparam name="T">The type of the object</typeparam>
/// <param name="reference">A reference of the object, or null if static</param>
/// <exception cref="InjectorStoreException">Thrown if some dependencies could not be resolved</exception>
public static void Inject<T>(this T reference)
public static void Inject<T>(this T reference, Injector injector)
{
InjectionResolver.InjectProperties(reference);
InjectionResolver.InjectFields(reference);
InjectionResolver.InjectProperties(reference, injector);
InjectionResolver.InjectFields(reference, injector);
}
}
}
17 changes: 3 additions & 14 deletions Jellyfish/DependencyInjection/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public void Bind<TBase, TSubtype>(params object[] arguments) where TSubtype : TB
/// Declare a template for the given type `<see cref="TBase" />` on how to initialize a variable spontaneously
/// </summary>
/// <typeparam name="TBase">The type of the property or field that gets injected</typeparam>
/// <typeparam name="TSubtype">The `<see cref="TBase"/>`'s sub type to be used for initialization of the base</typeparam>
/// <param name="initializer">The function to call everytime a property or field has to get initialized</param>
/// <exception cref="InjectorStoreException">Thrown if the type `<see cref="TBase" />` could not be templated</exception>
public void Template<TBase, TSubtype>(Func<TSubtype> initializer) where TSubtype : TBase
public void Template<TBase>(Func<TBase> initializer)
{
Templates.AddOrUpdate(typeof(TBase), initializer as Func<object>);
}
Expand Down Expand Up @@ -100,18 +99,8 @@ public void Define<TBase>(TBase instance)
Instances.AddOrUpdate(typeof(TBase), instance);
}

/// <summary>
/// Declare a fixed variable for the given type `<see cref="TSubtype" />` to set all variables of type `
/// <see cref="TBase" />` to
/// </summary>
/// <typeparam name="TBase">The type of the property or field that gets injected</typeparam>
/// <typeparam name="TSubtype">The type of the value to inject the property or field with</typeparam>
/// <param name="instance">The static instance to initialize all variables of type `<see cref="TBase" />` with</param>
/// <exception cref="InjectorStoreException">Thrown if the type `<see cref="TBase" />` could not be defined</exception>
public void Define<TBase, TSubtype>(TSubtype instance) where TSubtype : TBase
{
Instances.AddOrUpdate(typeof(TBase), instance);
}



/// <summary>
/// Initialize the given type `<see cref="TBase" />` with either a templated function to call or a pre-defined instance
Expand Down
4 changes: 2 additions & 2 deletions Jellyfish/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public abstract class ViewModel : ObservableObject
{
protected ViewModel()
{
InjectionResolver.InjectProperties(this);
InjectionResolver.InjectFields(this);
//InjectionResolver.InjectProperties(this);
//InjectionResolver.InjectFields(this);
}
}
}

0 comments on commit 2fbe812

Please sign in to comment.