Skip to content

Commit

Permalink
Container logics improved and localization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CrackAndDie committed Feb 14, 2024
1 parent fc53196 commit 1b92577
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 58 deletions.
8 changes: 4 additions & 4 deletions Hypocrite.Avalonia/Container/LightContainerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Hypocrite.Container
{
public class LightContainerExtension : IContainerExtension<ILightContainer>, IContainerInfo
{
private AbdrakovScopedProvider _currentScope;
private LightScopedProvider _currentScope;

/// <summary>
/// The instance of the wrapped container
Expand Down Expand Up @@ -197,13 +197,13 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
protected IScopedProvider CreateScopeInternal()
{
var child = Instance;
_currentScope = new AbdrakovScopedProvider(child);
_currentScope = new LightScopedProvider(child);
return _currentScope;
}

private class AbdrakovScopedProvider : IScopedProvider
private class LightScopedProvider : IScopedProvider
{
public AbdrakovScopedProvider(ILightContainer container)
public LightScopedProvider(ILightContainer container)
{
Container = container;
}
Expand Down
2 changes: 1 addition & 1 deletion Hypocrite.Avalonia/MVVM/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hypocrite.Mvvm
{
public class ViewModelBase : EngineViewModelBase, INavigationAware, IRequireInjection
public class ViewModelBase : EngineViewModelBase, INavigationAware
{
public ViewModelBase()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ public static class ContainerRegistrationExtensions
{
public static object GetInstance(this IContainerRegistration registration, ILightContainer container, bool withInjections = true)
{
// this is for cached Type instances
if (registration.Instance != null && registration.RegistrationType == RegistrationType.Type)
{
return registration.Instance;
}

if (registration.Instance != null && registration.RegistrationType == RegistrationType.Instance)
{
// all the injections should be resolved in the instance on its first resolve
if (container.InstanceCreator.RequiresInjections(registration.Instance) && withInjections)
{
container.InstanceCreator.ResolveInjections(registration.Instance, container);
}
if (registration.Instance is IRequireInjection reqInj2) reqInj2.OnResolveReady(); // callback
return registration.Instance;
}

Expand All @@ -24,7 +29,6 @@ public static object GetInstance(this IContainerRegistration registration, ILigh
{
registration.Instance = instance;
}
if (instance is IRequireInjection reqInj) reqInj.OnResolveReady(); // callback
return instance;
}

Expand Down
2 changes: 1 addition & 1 deletion Hypocrite.Core/Container/Interfaces/ILightContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public interface ILightContainer : IDisposable
object Resolve(Type type);
object Resolve(Type type, bool withInjections);
object Resolve(Type type, string name, bool withInjections);
void ResolveInjections(Type type);
void ResolveInjections(object instance);
}
}
12 changes: 0 additions & 12 deletions Hypocrite.Core/Container/Interfaces/IRequireInjection.cs

This file was deleted.

19 changes: 3 additions & 16 deletions Hypocrite.Core/Container/LightContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,11 @@ public object Resolve(Type type, string name, bool withInjections)
return null;
}

public void ResolveInjections(Type type)
public void ResolveInjections(object instance)
{
if (IsRegistered(type, out IContainerRegistration registration))
if (InstanceCreator.RequiresInjections(instance))
{
switch (registration.RegistrationType)
{
case RegistrationType.Type:
case RegistrationType.Instance:
{
registration.GetInstance(this, true);
break;
}
case RegistrationType.Func:
{
registration.GetFunc();
break;
}
}
InstanceCreator.ResolveInjections(instance, this);
}
}

Expand Down
22 changes: 14 additions & 8 deletions Hypocrite.Core/Container/Registration/DefaultInstanceCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ public object CreateInstance(IContainerRegistration registration, ILightContaine
}

var instance = constructor?.Invoke(constructorParams.ToArray());
if (registration.RegistrationType == RegistrationType.Type)
{
// save the instance as cache
registration.Instance = instance;
}

if (withInjections && RequiresInjections(instance))
ResolveInjections(instance, container);

if (registration.RegistrationType == RegistrationType.Type)
{
// remove the cached instance
registration.Instance = null;
}
return instance;
}

Expand All @@ -48,7 +60,7 @@ public void ResolveInjections(object instance, ILightContainer container, Type t
continue;
var dep = container.Resolve(f.FieldType, false);
f.SetValue(instance, dep);
container.ResolveInjections(f.FieldType);
container.ResolveInjections(dep);
}
}
// properties
Expand All @@ -62,20 +74,14 @@ public void ResolveInjections(object instance, ILightContainer container, Type t
continue;
var dep = container.Resolve(p.PropertyType, false);
p.SetValue(instance, dep, null);
container.ResolveInjections(p.PropertyType);
container.ResolveInjections(dep);
}
}

if (type.BaseType != null)
{
ResolveInjections(instance, container, type.BaseType);
}

// callback
if (instance is IRequireInjection reqInj)
{
reqInj.OnInjectionsReady();
}
}

public bool RequiresInjections(object instance, Type type = null)
Expand Down
8 changes: 1 addition & 7 deletions Hypocrite.Core/Container/Registration/RegistrationType.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hypocrite.Core.Container.Registration
namespace Hypocrite.Core.Container.Registration
{
public enum RegistrationType : byte
{
Expand Down
10 changes: 5 additions & 5 deletions Hypocrite.Wpf/Container/LightContainerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using Prism.Ioc.Internals;
using Hypocrite.Core.Container;

namespace Hypocrite.Container.PrismAdapter
namespace Hypocrite.Container
{
public class LightContainerExtension : IContainerExtension<ILightContainer>, IContainerInfo
{
private AbdrakovScopedProvider _currentScope;
private LightScopedProvider _currentScope;

/// <summary>
/// The instance of the wrapped container
Expand Down Expand Up @@ -196,13 +196,13 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
protected IScopedProvider CreateScopeInternal()
{
var child = Instance;
_currentScope = new AbdrakovScopedProvider(child);
_currentScope = new LightScopedProvider(child);
return _currentScope;
}

private class AbdrakovScopedProvider : IScopedProvider
private class LightScopedProvider : IScopedProvider
{
public AbdrakovScopedProvider(ILightContainer container)
public LightScopedProvider(ILightContainer container)
{
Container = container;
}
Expand Down
20 changes: 20 additions & 0 deletions Hypocrite.Wpf/Localization/LocalizationChangedExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Windows;

namespace Hypocrite.Localization
{
public class LocalizationChangedExpression
{
/// <summary>
/// Constructor for ResourceReferenceExpression
/// </summary>
/// <param name="resourceKey">
/// Name of the resource being referenced
/// </param>
public LocalizationChangedExpression(object resourceKey)
{
_resourceKey = resourceKey;
}

private object _resourceKey; // Name of the resource being referenced by this expression
}
}
90 changes: 90 additions & 0 deletions Hypocrite.Wpf/Localization/LocalizedResourceTestExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;

namespace Hypocrite.Localization
{
[MarkupExtensionReturnType(typeof(object))]
public class LocalizedResourceTestExtension : MarkupExtension, INotifyPropertyChanged
{
/// <summary>
/// Holds the Binding to get the key
/// </summary>
private Binding _binding;
/// <summary>
/// Holds the Key to a .resx object
/// </summary>
private string _key;
/// <summary>
/// Gets or sets the Key to a .resx object
/// </summary>
public string Key
{
get => _key;
set
{
if (_key != value)
{
_key = value;
OnNotifyPropertyChanged(nameof(Key));
}
}
}

/// <summary>
/// Gets or sets the initialize value.
/// This is ONLY used to support the localize extension in blend!
/// </summary>
/// <value>The initialize value.</value>
[EditorBrowsable(EditorBrowsableState.Never)]
[ConstructorArgument("key")]
public object InitializeValue { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Constructor that takes no parameters
/// </summary>
public LocalizedResourceTestExtension()
{
}

/// <summary>
/// Constructor that takes the resource key that this is a static reference to.
/// </summary>
public LocalizedResourceTestExtension(
object key)
{
if (key is Binding binding)
_binding = binding;
else
Key = key?.ToString();
}


/// <summary>
/// Return an object that should be set on the targetObject's targetProperty
/// for this markup extension. For DynamicResourceExtension, this is the object found in
/// a resource dictionary in the current parent chain that is keyed by ResourceKey
/// </summary>
/// <returns>
/// The object to set on this property.
/// </returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Key == null)
{
throw new InvalidOperationException();
}

return null;
}

internal void OnNotifyPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}
2 changes: 1 addition & 1 deletion Hypocrite.Wpf/MVVM/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Prism.Mvvm;
using System.Windows;
using Prism;
using Hypocrite.Container.PrismAdapter;
using Hypocrite.Container;

namespace Hypocrite.Mvvm
{
Expand Down
2 changes: 1 addition & 1 deletion Hypocrite.Wpf/MVVM/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hypocrite.Mvvm
{
public class ViewModelBase : EngineViewModelBase, INavigationAware, IRequireInjection
public class ViewModelBase : EngineViewModelBase, INavigationAware
{
public ViewModelBase()
{
Expand Down

0 comments on commit 1b92577

Please sign in to comment.