Skip to content

Commit

Permalink
Merge pull request #8
Browse files Browse the repository at this point in the history
Version 1.0.1
  • Loading branch information
imaxs committed Feb 11, 2023
2 parents abda634 + 7c1848b commit 8062de2
Show file tree
Hide file tree
Showing 47 changed files with 682 additions and 141 deletions.
44 changes: 36 additions & 8 deletions Framework/Binding/Implemantions/BindingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace EasyJection.Binding
using Types;
using Reflection;
using System.Runtime.CompilerServices;
using EasyJection.Binding.Extensions;

/// <summary>
/// Implementation of the <see cref="IBindingFactory"/> interface
Expand All @@ -50,7 +51,7 @@ public BindingFactory(IBinder binder, Type bindingType, IReflectionCache cache)
}

/// <inheritdoc cref="IBindingFactory.AddBinding(object)"/>
public IBindingCondition AddBinding(object value)
public IBindingCondition AddBinding(object value, bool UseDefaultConstructor)
{
var binding = new BindingData(this.BindingType, value, BindingInstanceType.Transient);
this.Binder.AddBinding(binding);
Expand All @@ -60,7 +61,9 @@ public IBindingCondition AddBinding(object value)
this.PreCaching(value as Type ?? value.GetType());

var bindInjection = this.CreateBindingInjectionFactoryProvider(binding);
bindInjection.Constructor(true);

if (UseDefaultConstructor)
bindInjection.Constructor(true);

return this.CreateBindingConditionFactoryProvider(binding);
}
Expand Down Expand Up @@ -92,19 +95,19 @@ public IBindingInjection AddBinding(object value, BindingInstanceType instanceTy
return bindInjection;
}

/// <inheritdoc cref="IBindingFactory.To{T}()"/>
public IBindingCondition To<T>() where T : class
/// <inheritdoc cref="IBindingFactory.To{T}(bool)"/>
public IBindingCondition To<T>(bool UseDefaultConstructor = false) where T : class
{
return this.To(typeof(T));
return this.To(typeof(T), UseDefaultConstructor);
}

/// <inheritdoc cref="IBindingFactory.To(Type)"/>
public IBindingCondition To(Type type)
/// <inheritdoc cref="IBindingFactory.To(Type, bool)"/>
public IBindingCondition To(Type type, bool UseDefaultConstructor = false)
{
if (!Helper.IsAssignable(this.BindingType, type))
throw new Exception(Causes.TYPE_NOT_ASSIGNABLE);

return this.AddBinding(type);
return this.AddBinding(type, UseDefaultConstructor);
}

/// <inheritdoc cref="IBindingFactory.ToInstance{T}(T)"/>
Expand Down Expand Up @@ -154,6 +157,31 @@ public void ToFactory(Types.IFactory instance)
this.AddFactoryInstance(null, instance);
}

/// <inheritdoc cref="IBindingFactory.ToFactory{T}(string)"/>
public IBindingInjection ToFactory<T>(string methodName, bool UseDefaultConstructor = false) where T : class
{
var type = typeof(T);
var methodInfo = type.FindMethodByName(methodName);
if (methodInfo is null)
throw new Exception(string.Format(Causes.METHOD_NOT_FOUND, methodName, type.Name));

var binding = new BindingData(this.BindingType, type, BindingInstanceType.Factory);
binding.Factory = new Factory(methodInfo);

this.Binder.AddBinding(binding);

// Precaching
if (type != null)
this.PreCaching(type);

var bindInjection = this.CreateBindingInjectionFactoryProvider(binding);

if (UseDefaultConstructor)
bindInjection.Constructor(true);

return bindInjection;
}

/// <inheritdoc cref="IBindingFactory.ToSelf(bool)"/>
public IBindingInjection ToSelf(bool UseDefaultConstructor = false)
{
Expand Down
20 changes: 16 additions & 4 deletions Framework/Binding/Interfaces/IBindingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,18 @@ public interface IBindingFactory
/// <summary>
/// Binds the key type to a type of <typeparamref name="T"/> as a transient.
/// </summary>
/// <param name="UseDefaultConstructor">Specifying the use of the default constructor to create an instance</param>
/// <typeparam name="T">The type to bind to.</typeparam>
/// <returns>The binding condition object related to this binding.</returns>
IBindingCondition To<T>() where T : class;
IBindingCondition To<T>(bool UseDefaultConstructor = false) where T : class;

/// <summary>
/// Binds the key type to a <paramref name="type"/> as a transient.
/// </summary>
/// <param name="UseDefaultConstructor">Specifying the use of the default constructor to create an instance</param>
/// <param name="type">The related type.</param>
/// <returns>The binding condition object related to this binding.</returns>
IBindingCondition To(Type type);
IBindingCondition To(Type type, bool UseDefaultConstructor = false);

/// <summary>
/// Binds the key type to an <paramref name="instance"/> of a <typeparamref name="T"/> type.
Expand All @@ -175,7 +177,7 @@ public interface IBindingFactory
/// </summary>
/// <param name="UseDefaultConstructor">Specifying the use of the default constructor to create an instance</param>
/// <typeparam name="T">The type which has implemented <see cref="Types.IFactory"/> interface.</typeparam>
/// /// <returns>The binding injection object related to this binding.</returns>
/// <returns>The binding injection object related to this binding.</returns>
IBindingInjection ToFactory<T>(bool UseDefaultConstructor = false) where T : Types.IFactory;

/// <summary>
Expand All @@ -200,12 +202,22 @@ public interface IBindingFactory
/// <param name="instance">The instance of a type which has implemented <see cref="Types.IFactory"/> interface.</param>
void ToFactory(Types.IFactory instance);

/// <summary>
/// Binds the key type to a <typeparamref name="T"/> factory.
/// </summary>
/// <param name="methodName">The name of the method of your <typeparamref name="T"/> factory that will create an instance.</param>
/// <param name="UseDefaultConstructor">Specifying the use of the default constructor to create an instance of <typeparamref name="T"/> factory.</param>
/// <typeparam name="T">The type of your factory.</typeparam>
/// <returns>The binding injection object related to this binding.</returns>
IBindingInjection ToFactory<T>(string methodName, bool UseDefaultConstructor = false) where T : class;

/// <summary>
/// Creates a binding.
/// </summary>
/// <param name="value">Binding value.</param>
/// <param name="UseDefaultConstructor">Specifying the use of the default constructor to create an instance</param>
/// <returns>The binding injection object related to this binding.</returns>
IBindingCondition AddBinding(object value);
IBindingCondition AddBinding(object value, bool UseDefaultConstructor);

/// <summary>
/// Creates a binding.
Expand Down
4 changes: 4 additions & 0 deletions Framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2023-02-11
- Fixed a bug with injection to a default constructor.
- Added the ability to bind a factory class without an inherited 'EasyJection.Types.IFactory' and specify the method for creating instances by method name.

## [1.0.0] - 2023-02-11
- New architecture.
- Documentation expanded.
Expand Down
7 changes: 7 additions & 0 deletions Framework/Reflection/Utils/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@

namespace EasyJection.Reflection.Utils
{
#region Comment
/// <summary>
/// Delegate for the factory's method of creating an instance without parameters.
/// </summary>
#endregion
public delegate object InstantiatorCall(object instance);

#region Comment
/// <summary>
/// Delegate for a constructor call without parameters.
Expand Down
5 changes: 5 additions & 0 deletions Framework/Reflection/Utils/MethodMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,10 @@ public static ParamsMethodCall CreateParameterizedMethod(System.Reflection.Metho
{
return (object instance, object[] parameters) => methodInfo.Invoke(instance, parameters);
}

public static InstantiatorCall CreateInstantinateMethod(System.Reflection.MethodInfo methodInfo)
{
return (object instance) => methodInfo.Invoke(instance, null);
}
}
}
7 changes: 6 additions & 1 deletion Framework/Resolving/Implementions/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ public object Resolve(Type type, IDictionary<Type, object> scopedInstances)
else if (bindingData.InstanceType.HasFlag(BindingInstanceType.Factory))
{
bindingData.InstanceType = BindingInstanceType.Factory | BindingInstanceType.Instance;
bindingData.Factory = instance as IFactory;

if (bindingData.Factory == null)
bindingData.Factory = instance as IFactory;
else
(bindingData.Factory as IFectorySetter).SetFactoryinstance(instance);

return bindingData.Factory.CreateInstance(bindingData);
}

Expand Down
52 changes: 52 additions & 0 deletions Framework/Types/Factory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of the EasyJection Framework.
* Author: Max Karepin (http://github.com/imaxs/)
*
* Copyright © 2021 Max Karepin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;

namespace EasyJection.Types
{
using Binding;
using Reflection.Utils;

public interface IFectorySetter
{
void SetFactoryinstance(object factoryInstance);
}

public class Factory : IFactory, IFectorySetter
{
protected object factoryInstance;
protected InstantiatorCall createInstance;
public Factory(System.Reflection.MethodInfo methodInfo)
{
this.createInstance = MethodMaker.CreateInstantinateMethod(methodInfo);
this.factoryInstance = null;
}

public void SetFactoryinstance(object factoryInstance)
{
this.factoryInstance = factoryInstance;
}

public object CreateInstance(IBindingData bindingData = null)
{
return this.createInstance(this.factoryInstance);
}
}
}
11 changes: 11 additions & 0 deletions Framework/Types/Factory.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions UnityPackage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2023-02-11
- Fixed a bug with injection to a default constructor.
- Added the ability to bind a factory class without an inherited 'EasyJection.Types.IFactory' and specify the method for creating instances by method name.
- Added the 'MonoInstaller' abstract class.
- Updated samples

## [1.0.0] - 2023-02-11
- New architecture.
- Documentation expanded.
Expand Down
12 changes: 0 additions & 12 deletions UnityPackage/Examples/1_BindingGameObject/Cube.cs

This file was deleted.

15 changes: 0 additions & 15 deletions UnityPackage/Examples/1_BindingGameObject/EntryPoint.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,25 @@ namespace EasyJection
#if UNITY_ENGINE_AVAILABLE
using UnityEngine;

public class MonoInstaller : MonoBehaviour
public abstract class MonoInstaller : MonoBehaviour
{
protected IContainer Container;

protected abstract void InstallBindings();

protected MonoInstaller()
{
if (Container == null)
Container = new Container();

this.InstallBindings();
}

protected void OnDestroy()
{
if (Container != null)
Container.Dispose();
}
}
#endif
}
Loading

0 comments on commit 8062de2

Please sign in to comment.