Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #195

Merged
merged 10 commits into from Oct 13, 2018
2 changes: 1 addition & 1 deletion appveyor.yml
@@ -1,5 +1,5 @@
environment:
build_version: 6.4.1
build_version: 6.4.2
Version: $(build_version)
COVERALLS_REPO_TOKEN:
secure: +OWHMxYHaMp6iRNNLZcMZq423PhYWxMky+B2C0p3U8v7tpdoKRMzWZKJ1LuYO60O
Expand Down
Expand Up @@ -15,9 +15,7 @@ public class FuncCompiledCondition : ICompiledCondition
/// <param name="condition">condition function</param>
public FuncCompiledCondition(Func<IActivationStrategy, StaticInjectionContext, bool> condition)
{
if (condition == null) throw new ArgumentNullException(nameof(condition));

_condition = condition;
_condition = condition ?? throw new ArgumentNullException(nameof(condition));
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Grace/DependencyInjection/Conditions/WhenClassHas.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Grace.DependencyInjection.Conditions
{
Expand Down Expand Up @@ -29,7 +30,8 @@ public WhenClassHas(Type attributeType, Func<Attribute, bool> filter = null)
/// <returns>meets condition</returns>
public bool MeetsCondition(IActivationStrategy strategy, StaticInjectionContext staticInjectionContext)
{
var targetInfo = staticInjectionContext.TargetInfo;
var targetInfo = staticInjectionContext.InjectionStack.FirstOrDefault(
info => info.RequestingStrategy?.StrategyType == ActivationStrategyType.ExportStrategy);

if (targetInfo != null)
{
Expand Down
Expand Up @@ -25,11 +25,9 @@ public class WhenConditionConfiguration<T> : IWhenConditionConfiguration<T>
/// <param name="t">T to return</param>
public WhenConditionConfiguration(Action<ICompiledCondition> addAction, T t)
{
if (addAction == null) throw new ArgumentNullException(nameof(addAction));

if (t == null) throw new ArgumentNullException(nameof(t));

AddAction = addAction;
AddAction = addAction ?? throw new ArgumentNullException(nameof(addAction));

TValue = t;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Grace/DependencyInjection/Conditions/WhenInjectedInto.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Grace.Data;

namespace Grace.DependencyInjection.Conditions
Expand Down Expand Up @@ -27,9 +28,7 @@ public WhenInjectedInto(params Type[] types)
/// <param name="typeTest"></param>
public WhenInjectedInto(Func<Type, bool> typeTest)
{
if (typeTest == null) throw new ArgumentNullException(nameof(typeTest));

_typeTest = typeTest;
_typeTest = typeTest ?? throw new ArgumentNullException(nameof(typeTest));
}
/// <summary>
/// Test if being injected into a specific type
Expand All @@ -39,7 +38,9 @@ public WhenInjectedInto(Func<Type, bool> typeTest)
/// <returns></returns>
public bool MeetsCondition(IActivationStrategy strategy, StaticInjectionContext staticInjectionContext)
{
var targetInfo = staticInjectionContext.TargetInfo;
var targetInfo =
staticInjectionContext.InjectionStack.FirstOrDefault(
info => info.RequestingStrategy?.StrategyType == ActivationStrategyType.ExportStrategy);

return targetInfo?.InjectionType != null && _typeTest(targetInfo.InjectionType);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Grace/DependencyInjection/Conditions/WhenMemberHas.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Grace.DependencyInjection.Conditions
{
Expand Down Expand Up @@ -27,15 +28,14 @@ public WhenMemberHas(Func<TAttribute, bool> filter)
/// <returns>meets condition</returns>
public bool MeetsCondition(IActivationStrategy strategy, StaticInjectionContext staticInjectionContext)
{
var targetInfo = staticInjectionContext.TargetInfo;
var targetInfo = staticInjectionContext.InjectionStack.FirstOrDefault(
info => info.RequestingStrategy?.StrategyType == ActivationStrategyType.ExportStrategy);

if (targetInfo != null)
{
foreach (var attribute in targetInfo.InjectionMemberAttributes)
{
var attrT = attribute as TAttribute;

if (attrT != null && (_filter == null || _filter(attrT)))
if (attribute is TAttribute attrT && (_filter == null || _filter(attrT)))
{
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Grace/DependencyInjection/Conditions/WhenTargetHas.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Grace.DependencyInjection.Conditions
{
Expand Down Expand Up @@ -29,7 +30,8 @@ public WhenTargetHas(Type attributeType, Func<Attribute,bool> filter = null)
/// <returns>meets condition</returns>
public bool MeetsCondition(IActivationStrategy strategy, StaticInjectionContext staticInjectionContext)
{
var targetInfo = staticInjectionContext.TargetInfo;
var targetInfo = staticInjectionContext.InjectionStack.FirstOrDefault(
info => info.RequestingStrategy?.StrategyType == ActivationStrategyType.ExportStrategy);

if (targetInfo != null)
{
Expand Down
Expand Up @@ -76,6 +76,20 @@ public static IExportTypeSetConfiguration ExportAssemblies(this IExportRegistrat
return registrationBlock.Export<T>().As<TInterface>();
}

/// <summary>
/// Export type as keyed type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TInterface"></typeparam>
/// <param name="registrationBlock"></param>
/// <param name="key"></param>
/// <returns></returns>
public static IFluentExportStrategyConfiguration<T> ExportAsKeyed<T, TInterface>(
this IExportRegistrationBlock registrationBlock, object key)
{
return registrationBlock.Export<T>().AsKeyed(typeof(TInterface), key);
}

/// <summary>
/// Extension to export a list of types to a registration block
/// </summary>
Expand Down
Expand Up @@ -69,6 +69,13 @@ public interface IFluentExportStrategyConfiguration
/// <returns>configuraiton object</returns>
IFluentExportStrategyConfiguration ImportMembers(Func<MemberInfo, bool> selector = null, bool includeMethods = false);

/// <summary>
/// Import property by name
/// </summary>
/// <param name="propertyName">property name</param>
/// <returns>configuration object</returns>
IFluentImportPropertyConfiguration ImportProperty(string propertyName);

/// <summary>
/// Apply a lifestlye to export strategy
/// </summary>
Expand All @@ -93,6 +100,13 @@ public interface IFluentExportStrategyConfiguration
/// </summary>
IWhenConditionConfiguration<IFluentExportStrategyConfiguration> When { get; }

/// <summary>
/// Configure constructor parameter
/// </summary>
/// <param name="parameterType">parameter type</param>
/// <returns></returns>
IFluentWithCtorConfiguration WithCtorParam(Type parameterType = null);

/// <summary>
/// Configure constructor parameter
/// </summary>
Expand Down
@@ -1,5 +1,48 @@
namespace Grace.DependencyInjection
{
/// <summary>
/// Configure import property
/// </summary>
public interface IFluentImportPropertyConfiguration : IFluentExportStrategyConfiguration
{/// <summary>
/// use a filter delegate when importing property
/// </summary>
/// <param name="consider">filter delegate</param>
/// <returns>configuration object</returns>
IFluentImportPropertyConfiguration Consider(ActivationStrategyFilter consider);

/// <summary>
/// Default value if one can not be found
/// </summary>
/// <param name="defaultValue">default value</param>
/// <returns>configuration object</returns>
IFluentImportPropertyConfiguration DefaultValue(object defaultValue);

/// <summary>
/// Is the property required
/// </summary>
/// <param name="isRequired">is required</param>
/// <returns>configuration object</returns>
IFluentImportPropertyConfiguration IsRequired(bool isRequired = true);

/// <summary>
/// Locate with a particular key
/// </summary>
/// <param name="locateKey">locate key</param>
/// <returns>configuration object</returns>
IFluentImportPropertyConfiguration LocateWithKey(object locateKey);

/// <summary>
/// Provide value to be used for property
/// </summary>
/// <param name="value">value to use for property</param>
/// <returns></returns>
IFluentImportPropertyConfiguration Value(object value);


}


/// <summary>
/// configuration for importing a property
/// </summary>
Expand Down Expand Up @@ -34,5 +77,12 @@ public interface IFluentImportPropertyConfiguration<T, in TProp> : IFluentExport
/// <param name="locateKey">locate key</param>
/// <returns>configuration object</returns>
IFluentImportPropertyConfiguration<T, TProp> LocateWithKey(object locateKey);

/// <summary>
/// Provide value for property
/// </summary>
/// <param name="value">property value</param>
/// <returns></returns>
IFluentImportPropertyConfiguration<T, TProp> Value(TProp value);
}
}
71 changes: 71 additions & 0 deletions src/Grace/DependencyInjection/IFluentWithCtorConfiguration.cs
Expand Up @@ -2,6 +2,77 @@

namespace Grace.DependencyInjection
{

/// <summary>
/// configure constructor parameter
/// </summary>
public interface IFluentWithCtorConfiguration : IFluentExportStrategyConfiguration
{
/// <summary>
/// Applies a filter to be used when resolving a parameter constructor
/// It will be called each time the parameter is resolved
/// </summary>
/// <param name="filter">filter delegate to be used when resolving parameter</param>
/// <returns>configuration object</returns>
IFluentWithCtorConfiguration Consider(ActivationStrategyFilter filter);

/// <summary>
/// Assign a default value if no better option is found
/// </summary>
/// <param name="defaultValue">default value</param>
/// <returns>configuration object</returns>
IFluentWithCtorConfiguration DefaultValue(object defaultValue);

/// <summary>
/// Default value func
/// </summary>
/// <param name="defaultValueFunc"></param>
/// <returns></returns>
IFluentWithCtorConfiguration DefaultValue(Func<object> defaultValueFunc);

/// <summary>
/// Default value function
/// </summary>
/// <param name="defaultValueFunc"></param>
/// <returns></returns>
IFluentWithCtorConfiguration DefaultValue(Func<IExportLocatorScope, StaticInjectionContext, IInjectionContext, object> defaultValueFunc);

/// <summary>
/// Mark the parameter as dynamic
/// </summary>
/// <param name="isDynamic"></param>
/// <returns>configuration object</returns>
IFluentWithCtorConfiguration IsDynamic(bool isDynamic = true);

/// <summary>
/// Is the parameter required when resolving the type
/// </summary>
/// <param name="isRequired">is the parameter required</param>
/// <returns>configuration object</returns>
IFluentWithCtorConfiguration IsRequired(bool isRequired = true);

/// <summary>
/// Locate with a particular key
/// </summary>
/// <param name="locateKey">ocate key</param>
/// <returns>configuration object</returns>
IFluentWithCtorConfiguration LocateWithKey(object locateKey);

/// <summary>
/// Name of the parameter being configured
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
IFluentWithCtorConfiguration Named(string name);

/// <summary>
/// Use a specific type for parameter
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
IFluentWithCtorConfiguration Use(Type type);
}

/// <summary>
/// configure constructor parameter
/// </summary>
Expand Down
Expand Up @@ -217,9 +217,7 @@ public IExportTypeSetConfiguration ExportAttributedTypes()
/// <returns></returns>
public IExportTypeSetConfiguration ImportConstructorSelection(Func<Type, IConstructorExpressionCreator> method)
{
if (method == null) throw new ArgumentNullException(nameof(method));

_constructorSelectionMethod = method;
_constructorSelectionMethod = method ?? throw new ArgumentNullException(nameof(method));

return this;
}
Expand Down Expand Up @@ -249,9 +247,7 @@ public IExportTypeSetConfiguration UsingLifestyle(ICompiledLifestyle lifestyle)
/// <returns>configuration object</returns>
public IExportTypeSetConfiguration UsingLifestyle(Func<Type, ICompiledLifestyle> lifestyleFunc)
{
if (lifestyleFunc == null) throw new ArgumentNullException(nameof(lifestyleFunc));

_lifestyleFunc = lifestyleFunc;
_lifestyleFunc = lifestyleFunc ?? throw new ArgumentNullException(nameof(lifestyleFunc));

return this;
}
Expand Down
Expand Up @@ -188,6 +188,25 @@ protected virtual IActivationExpressionResult CreateMemberInjectExpressions(IInj
newResult.AddExtraExpression(Expression.Assign(memberExpression, memberResult.Expression));
}
}
else
{
Expression memberExpression;

if (memberKVP.Key is FieldInfo)
{
memberExpression = Expression.Field(variable, memberKVP.Key as FieldInfo);
}
else if (memberKVP.Key is PropertyInfo)
{
memberExpression = Expression.Property(variable, (PropertyInfo)memberKVP.Key);
}
else
{
throw new LocateException(request.GetStaticInjectionContext(), $"{memberKVP.Key.GetType().Name} member type not supported");
}

newResult.AddExtraExpression(Expression.Assign(memberExpression, expression));
}
}

newResult.Expression = variable;
Expand Down