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

Added constructor parameters filling #169

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/GenFu/FillerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public class FillerManager

static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);


public FillerManager()
{
ResetFillers();
}
public FillerManager() => ResetFillers();

public void ResetFillers()
{
Expand Down Expand Up @@ -94,7 +90,6 @@ public void ResetFillers()
_propertyFillers.Add(new MedicalProcedureFiller());

_propertyFillers.Add(new StringFiller());

}

_specificPropertyFillersByObjectType = new Dictionary<string, IDictionary<string, IPropertyFiller>>();
Expand Down Expand Up @@ -149,7 +144,6 @@ public void RegisterFiller(IPropertyFiller filler)
{
typeFillers[key] = filler;
}

}
}
finally
Expand All @@ -159,13 +153,19 @@ public void RegisterFiller(IPropertyFiller filler)
}

public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
=> GetFiller(propertyInfo.DeclaringType, propertyInfo.Name, propertyInfo.PropertyType);

public IPropertyFiller GetFiller(ParameterInfo parameterInfo)
=> GetFiller(parameterInfo.Member.DeclaringType, parameterInfo.Name, parameterInfo.ParameterType);

public IPropertyFiller GetFiller(Type declaringType, string name, Type type)
{
rwl.EnterReadLock();
var newRegistrations = new Dictionary<Type, IPropertyFiller>();
IPropertyFiller result = null;
try
{
Type objectType = propertyInfo.DeclaringType;
Type objectType = declaringType;
while (objectType != null && result == null)
{
//First try to get a specific filler based on a full type name (including namespace)
Expand All @@ -174,7 +174,7 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName))
{
IDictionary<string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName];
result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
result = GetMatchingPropertyFiller(name, type, propertyFillers);
}

//Second try to get a more generic filler based on only the class name (no namespace)
Expand All @@ -184,7 +184,7 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName))
{
IDictionary<string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName];
result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
result = GetMatchingPropertyFiller(name, type, propertyFillers);
}
}

Expand All @@ -194,19 +194,19 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
if (result == null)
{
//Finally, grab a generic property filler for that property type
if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType))
if (_genericPropertyFillersByPropertyType.ContainsKey(type))
{
result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType];
result = _genericPropertyFillersByPropertyType[type];
}
else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum))
else if (type.GetTypeInfo().BaseType == typeof(System.Enum))
{
result = new EnumFiller(propertyInfo.PropertyType);
result = new EnumFiller(type);
}
else
{
//TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.)
result = new CustomFiller<object>("*", typeof(object), true, () => null);
newRegistrations[propertyInfo.PropertyType] = result;
newRegistrations[type] = result;
}
}
}
Expand Down Expand Up @@ -287,13 +287,13 @@ public IPropertyFiller GetMethodFiller(MethodInfo methodInfo)
}
}

private static IPropertyFiller GetMatchingPropertyFiller(PropertyInfo propertyInfo, IDictionary<string, IPropertyFiller> propertyFillers)
private static IPropertyFiller GetMatchingPropertyFiller(string name, Type type, IDictionary<string, IPropertyFiller> propertyFillers)
{
IPropertyFiller result = null;
foreach (IPropertyFiller propertyFiller in propertyFillers.Values)
{
if (propertyFiller.PropertyType == propertyInfo.PropertyType &&
propertyFiller.PropertyNames.Any(s => propertyInfo.Name.ToLowerInvariant().Equals(s.ToLowerInvariant())))
if (propertyFiller.PropertyType == type &&
propertyFiller.PropertyNames.Any(s => name.ToLowerInvariant().Equals(s.ToLowerInvariant())))
{
result = propertyFiller;
break;
Expand All @@ -302,7 +302,6 @@ private static IPropertyFiller GetMatchingPropertyFiller(PropertyInfo propertyIn
return result;
}


private static IPropertyFiller GetMatchingMethodFiller(MethodInfo methodInfo, IDictionary<string, IPropertyFiller> propertyFillers)
{
const string setPattern = @"^Set([A-Z].*|_.*)";
Expand Down
91 changes: 26 additions & 65 deletions src/GenFu/GenFu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ static GenFu()
Random = new Random();
}

public static T New<T>() where T : new()
public static T New<T>()
{
return (T)New(typeof(T));
}

public static object New(Type type)
{
object instance = Activator.CreateInstance(type);
var ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)
.OrderByDescending(c => c.GetParameters().Length).First();
var parameters =
ctor.GetParameters()
.Select(p => _fillerManager.GetFiller(p).GetValue(null))
.ToArray();
object instance = Activator.CreateInstance(type, parameters);
return New(instance);
}

public static T New<T>(T instance)
{
return (T)New((object)instance);

}
public static T New<T>(T instance) => (T)New((object)instance);

public static object New(object instance)
{
if (instance != null)
if (instance is not null)
{
var type = instance.GetType();
if (type.FullName == "System.Guid")
Expand Down Expand Up @@ -71,30 +73,21 @@ public static object New(object instance)
}


public static List<T> ListOf<T>() where T : new()
{
return ListOf(typeof(T)).Cast<T>().ToList();
}
public static List<T> ListOf<T>() => ListOf(typeof(T)).Cast<T>().ToList();

public static List<object> ListOf(Type type) => BuildList(type, _listCount);

public static List<object> ListOf(Type type)
{
return BuildList(type, _listCount);
}
/// <summary>
/// Creates a new list of <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="itemCount">Number of items to add</param>
/// <returns></returns>
public static List<T> ListOf<T>(int itemCount) where T : new()
{
return ListOf(typeof(T), itemCount).Cast<T>().ToList();
}
public static List<T> ListOf<T>(int itemCount)
=> ListOf(typeof(T), itemCount).Cast<T>().ToList();

public static List<object> ListOf(Type type, int itemCount)
{
return BuildList(type, itemCount);
}
=> BuildList(type, itemCount);

private static List<object> BuildList(Type type, int itemCount)
{
Expand All @@ -108,31 +101,19 @@ private static List<object> BuildList(Type type, int itemCount)
return result;
}

public static IEnumerable<T> LazyOf<T>() where T : new()
{
return LazyOf(typeof(T)).Cast<T>();
}
public static IEnumerable<T> LazyOf<T>() => LazyOf(typeof(T)).Cast<T>();

public static IEnumerable<object> LazyOf(Type type)
{
return BuildLazy(type, _listCount);
}
public static IEnumerable<object> LazyOf(Type type) => BuildLazy(type, _listCount);

/// <summary>
/// Creates a new lazy collection of <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="itemCount">Number of items to add</param>
/// <returns></returns>
public static IEnumerable<T> LazyOf<T>(int itemCount) where T : new()
{
return LazyOf(typeof(T), itemCount).Cast<T>();
}
public static IEnumerable<T> LazyOf<T>(int itemCount) => LazyOf(typeof(T), itemCount).Cast<T>();

private static IEnumerable<object> LazyOf(Type type, int itemCount)
{
return BuildLazy(type, itemCount);
}
private static IEnumerable<object> LazyOf(Type type, int itemCount) => BuildLazy(type, itemCount);


private static IEnumerable<object> BuildLazy(Type type, int itemCount)
Expand All @@ -143,15 +124,9 @@ private static IEnumerable<object> BuildLazy(Type type, int itemCount)
}
}

public static IEnumerable<T> ForeverOf<T>() where T : new()
{
return ForeverOf(typeof(T)).Cast<T>();
}
public static IEnumerable<T> ForeverOf<T>() => ForeverOf(typeof(T)).Cast<T>();

private static IEnumerable<object> ForeverOf(Type type)
{
return BuildForever(type);
}
private static IEnumerable<object> ForeverOf(Type type) => BuildForever(type);

private static IEnumerable<object> BuildForever(Type type)
{
Expand All @@ -174,25 +149,16 @@ private static void CallSetterMethod(object instance, MethodInfo method)
method.Invoke(instance, new[] { filler.GetValue(instance) });
}



public static DateTime MinDateTime
{
get { return new GenericFillerDefaults(_fillerManager).GetMinDateTime(); }

set
{
new GenericFillerDefaults(_fillerManager).SetMinDateTime(value);
}
get => new GenericFillerDefaults(_fillerManager).GetMinDateTime();
set => new GenericFillerDefaults(_fillerManager).SetMinDateTime(value);
}

public static DateTime MaxDateTime
{
get { return new GenericFillerDefaults(_fillerManager).GetMaxDateTime(); }
set
{
new GenericFillerDefaults(_fillerManager).SetMaxDateTime(value);
}
get => new GenericFillerDefaults(_fillerManager).GetMaxDateTime();
set => new GenericFillerDefaults(_fillerManager).SetMaxDateTime(value);
}

public static Random Random { get; private set; }
Expand Down Expand Up @@ -250,9 +216,4 @@ public class Defaults

public const string STRING_LOADFAIL = "The resource list for {0} failed to load.";
}


}