Cache property lookups for the object parsing
This commit is contained in:
@@ -6,7 +6,8 @@ using Fasterflect;
|
||||
|
||||
namespace LeafWeb.Core.Utility
|
||||
{
|
||||
public static class ParsedObjectFactory<T> where T : new()
|
||||
public static class ParsedObjectFactory<T>
|
||||
where T : new()
|
||||
{
|
||||
static ParsedObjectFactory()
|
||||
{
|
||||
@@ -14,19 +15,13 @@ namespace LeafWeb.Core.Utility
|
||||
BoolTypeConverter.Register();
|
||||
}
|
||||
|
||||
private static PropertyInfo[] GetProperties()
|
||||
{
|
||||
var propertyInfos = typeof(T).Properties();
|
||||
return propertyInfos.Where(p => p.HasAttribute<ParseInfoAttribute>()).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an object type T filling properties from the given title values
|
||||
/// </summary>
|
||||
/// <param name="titleValues">Colon separated title: values</param>
|
||||
public static T Create(string[] titleValues)
|
||||
{
|
||||
var properties = GetProperties();
|
||||
var properties = new ParseInfoPropertyMatcherWithCache<T>().ParseInfoProperties;
|
||||
var obj = new T();
|
||||
// take each of the
|
||||
for (var index = 0; index < titleValues.Length; index++)
|
||||
@@ -68,7 +63,7 @@ namespace LeafWeb.Core.Utility
|
||||
|
||||
public static T[] Create(string[] titles, string[][] valueArrays)
|
||||
{
|
||||
var properties = GetProperties();
|
||||
var matcher = new ParseInfoPropertyMatcherWithCache<T>();
|
||||
var objs = new T[valueArrays.Length];
|
||||
|
||||
for (var vIndex = 0; vIndex < valueArrays.Length; vIndex++)
|
||||
@@ -84,7 +79,7 @@ namespace LeafWeb.Core.Utility
|
||||
if (IsMissingValue(value))
|
||||
continue;
|
||||
|
||||
var property = MatchProperty(properties, title, position);
|
||||
var property = matcher.MatchProperty(title, position);
|
||||
|
||||
if (property != null)
|
||||
{
|
||||
@@ -101,7 +96,7 @@ namespace LeafWeb.Core.Utility
|
||||
|
||||
public static T Create(Tuple<string, string>[] titleValues)
|
||||
{
|
||||
var properties = GetProperties();
|
||||
var matcher = new ParseInfoPropertyMatcherWithCache<T>();
|
||||
var obj = new T();
|
||||
|
||||
for (var index = 0; index < titleValues.Length; index++)
|
||||
@@ -114,7 +109,7 @@ namespace LeafWeb.Core.Utility
|
||||
if (IsMissingValue(value))
|
||||
continue;
|
||||
|
||||
var property = MatchProperty(properties, title, position);
|
||||
var property = matcher.MatchProperty(title, position);
|
||||
|
||||
if (property != null)
|
||||
{
|
||||
@@ -132,50 +127,6 @@ namespace LeafWeb.Core.Utility
|
||||
return string.IsNullOrEmpty(value) || value == "NA" || value == "-9999";
|
||||
}
|
||||
|
||||
// TODO: this call maybe could be cached
|
||||
private static PropertyInfo MatchProperty(PropertyInfo[] properties, string title, int position)
|
||||
{
|
||||
return
|
||||
properties.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsTitleMatch(title)) ??
|
||||
properties.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsPositionMatch(position));
|
||||
}
|
||||
|
||||
// TODO: this call maybe could be cached
|
||||
private static PropertyInfo MatchPropertyExact(PropertyInfo[] properties, string title, int position)
|
||||
{
|
||||
return
|
||||
properties
|
||||
.FirstOrDefault(p =>
|
||||
{
|
||||
var attribute = p.Attribute<ParseInfoAttribute>();
|
||||
return attribute.IsTitleMatch(title) && attribute.IsPositionMatch(position);
|
||||
});
|
||||
}
|
||||
|
||||
public static bool IsPropertiesTitlesMatch(string[] titles)
|
||||
{
|
||||
var properties = GetProperties();
|
||||
var propertyMatch = 0;
|
||||
var propertyNoMatch = 0;
|
||||
|
||||
for (var i = 0; i < titles.Length; i++)
|
||||
{
|
||||
var title = titles[i];
|
||||
var position = i + 1;
|
||||
if (string.IsNullOrEmpty(title))
|
||||
continue;
|
||||
|
||||
var property = MatchPropertyExact(properties, title, position);
|
||||
|
||||
if (property != null)
|
||||
propertyMatch++;
|
||||
else
|
||||
propertyNoMatch++;
|
||||
}
|
||||
|
||||
return propertyMatch / (double) propertyNoMatch > .9;
|
||||
}
|
||||
|
||||
private static bool TryConvertValue(PropertyInfo property, object value, out object convertedValue)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user