Skip to content

Commit

Permalink
Deferred the building of factories, converters, formatters, and layou…
Browse files Browse the repository at this point in the history
…ts until they are used to decrease startup/initialization time.
  • Loading branch information
phatboyg committed Jan 7, 2018
1 parent 8f3f4b0 commit c51cf43
Show file tree
Hide file tree
Showing 56 changed files with 3,379 additions and 1,871 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ packages

*.ncrunch*
.vs
*.psess
*.vspx

.fake

Expand Down
138 changes: 76 additions & 62 deletions src/Machete.sln
Original file line number Diff line number Diff line change
@@ -1,62 +1,76 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete", "Machete/Machete.csproj", "{F223FB77-E83F-4A63-B8E2-73624B492545}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.Tests", "Machete.Tests\Machete.Tests.csproj", "{1D41759F-660E-4664-883E-190BB9A041EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.HL7", "Machete.HL7\Machete.HL7.csproj", "{EE9BE5FD-E053-473F-A91F-C7216138F044}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.HL7.Tests", "Machete.HL7.Tests\Machete.HL7.Tests.csproj", "{17297476-28E0-4544-9C1E-E3F51162FEBD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.HL7Schema", "Machete.HL7Schema\Machete.HL7Schema.csproj", "{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.X12", "Machete.X12\Machete.X12.csproj", "{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.X12.Tests", "Machete.X12.Tests\Machete.X12.Tests.csproj", "{DE3734D5-33CB-4432-866C-1C734BE30F50}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machete.X12Schema", "Machete.X12Schema\Machete.X12Schema.csproj", "{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F223FB77-E83F-4A63-B8E2-73624B492545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F223FB77-E83F-4A63-B8E2-73624B492545}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F223FB77-E83F-4A63-B8E2-73624B492545}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F223FB77-E83F-4A63-B8E2-73624B492545}.Release|Any CPU.Build.0 = Release|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Release|Any CPU.Build.0 = Release|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Release|Any CPU.Build.0 = Release|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Release|Any CPU.Build.0 = Release|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Release|Any CPU.Build.0 = Release|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Release|Any CPU.Build.0 = Release|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Release|Any CPU.Build.0 = Release|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete", "Machete\Machete.csproj", "{F223FB77-E83F-4A63-B8E2-73624B492545}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.Tests", "Machete.Tests\Machete.Tests.csproj", "{1D41759F-660E-4664-883E-190BB9A041EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.HL7", "Machete.HL7\Machete.HL7.csproj", "{EE9BE5FD-E053-473F-A91F-C7216138F044}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.HL7.Tests", "Machete.HL7.Tests\Machete.HL7.Tests.csproj", "{17297476-28E0-4544-9C1E-E3F51162FEBD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.HL7Schema", "Machete.HL7Schema\Machete.HL7Schema.csproj", "{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.X12", "Machete.X12\Machete.X12.csproj", "{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.X12.Tests", "Machete.X12.Tests\Machete.X12.Tests.csproj", "{DE3734D5-33CB-4432-866C-1C734BE30F50}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Machete.X12Schema", "Machete.X12Schema\Machete.X12Schema.csproj", "{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProfileConsole", "ProfileConsole\ProfileConsole.csproj", "{8827A6E3-F0A4-41EC-AA77-95B468B632B6}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F223FB77-E83F-4A63-B8E2-73624B492545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F223FB77-E83F-4A63-B8E2-73624B492545}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F223FB77-E83F-4A63-B8E2-73624B492545}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F223FB77-E83F-4A63-B8E2-73624B492545}.Release|Any CPU.Build.0 = Release|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D41759F-660E-4664-883E-190BB9A041EB}.Release|Any CPU.Build.0 = Release|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE9BE5FD-E053-473F-A91F-C7216138F044}.Release|Any CPU.Build.0 = Release|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17297476-28E0-4544-9C1E-E3F51162FEBD}.Release|Any CPU.Build.0 = Release|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D6B014C-10E5-42A8-A5D9-8C544F06C93D}.Release|Any CPU.Build.0 = Release|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FED61E73-7033-4B7C-89FF-4A0DFDD3D062}.Release|Any CPU.Build.0 = Release|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE3734D5-33CB-4432-866C-1C734BE30F50}.Release|Any CPU.Build.0 = Release|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A4EF736-F9B0-4E37-A77A-90FF0C3CF436}.Release|Any CPU.Build.0 = Release|Any CPU
{8827A6E3-F0A4-41EC-AA77-95B468B632B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8827A6E3-F0A4-41EC-AA77-95B468B632B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8827A6E3-F0A4-41EC-AA77-95B468B632B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8827A6E3-F0A4-41EC-AA77-95B468B632B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5AD2420D-AED0-4761-8871-FE0D4B450BA6}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using Entities;


public class DynamicEntityConverterBuilder<TEntity, TSchema> :
IEntityConverterBuilder<TEntity, TSchema>
where TSchema : Entity
Expand All @@ -21,15 +20,11 @@ public DynamicEntityConverterBuilder(ISchemaBuilder<TSchema> schemaBuilder, IEnt
_schemaBuilder = schemaBuilder;
_entitySelector = entitySelector;

ImplementationType = schemaBuilder.GetImplementationType<TEntity>();

_properties = new List<IEntityPropertyConverter<TEntity>>();
_initializers = new List<IEntityInitializer<TEntity>>();
_valueInfos = new Dictionary<string, ValueInfo>(StringComparer.OrdinalIgnoreCase);
}

public Type ImplementationType { get; }

public IEntityConverter<T> GetEntityConverter<T>()
where T : TSchema
{
Expand All @@ -50,12 +45,9 @@ public void Add(IEntityInitializer<TEntity> property)

public IEntityConverter<TEntity> Build()
{
var entityType = new SchemaEntityInfo(typeof(TEntity), typeof(TSchema), _entitySelector, _valueInfos);

var entityFactoryType = typeof(DynamicEntityFactory<,>).MakeGenericType(typeof(TEntity), ImplementationType);
var entityFactory = (IEntityFactory<TEntity>) Activator.CreateInstance(entityFactoryType, entityType, _initializers);
var entityInfo = new SchemaEntityInfo(typeof(TEntity), typeof(TSchema), _entitySelector, _valueInfos);

return new DynamicEntityConverter<TEntity, TSchema>(entityType, entityFactory, _properties);
return new DynamicEntityConverter<TEntity, TSchema>(entityInfo, new TemporaryEntityFactory<TEntity>(entityInfo, _initializers), _properties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Entities;
using Formatters;
using Internals.Extensions;

Expand All @@ -13,8 +12,8 @@ public class DynamicEntityFormatterBuilder<TEntity, TSchema> :
where TSchema : Entity
where TEntity : TSchema
{
readonly ISchemaBuilder<TSchema> _schemaBuilder;
readonly IDictionary<int, IEntityPropertyFormatter<TEntity>> _propertyFormatters;
readonly ISchemaBuilder<TSchema> _schemaBuilder;

public DynamicEntityFormatterBuilder(ISchemaBuilder<TSchema> schemaBuilder)
{
Expand Down Expand Up @@ -42,7 +41,7 @@ public void Add(int position, IEntityPropertyFormatter<TEntity> formatter)
public IEntityFormatter<TEntity> Build()
{
var lastPosition = _propertyFormatters.Max(x => x.Key);
List<IEntityPropertyFormatter<TEntity>> formatters = Enumerable.Range(0, lastPosition + 1)
var formatters = Enumerable.Range(0, lastPosition + 1)
.Select(index => _propertyFormatters.ContainsKey(index) ? _propertyFormatters[index] : new EmptyEntityPropertyFormatter<TEntity>())
.ToList();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace Machete.SchemaConfiguration.Builders
{
using System;
using System.Collections.Generic;
using Layouts;

using Layouts;

public class DynamicLayoutBuilder<TLayout, TSchema> :
ILayoutBuilder<TLayout, TSchema>
Expand All @@ -18,12 +16,8 @@ public DynamicLayoutBuilder(ISchemaLayoutBuilder<TSchema> builder)
_builder = builder;

_properties = new List<ILayoutProperty<TLayout, TSchema>>();

ImplementationType = builder.GetImplementationType<TLayout>();
}

public Type ImplementationType { get; }

public ILayoutParserFactory<T, TSchema> GetLayout<T>()
where T : Layout
{
Expand All @@ -37,10 +31,7 @@ public void Add(ILayoutProperty<TLayout, TSchema> property)

public ILayoutParserFactory<TLayout, TSchema> Build()
{
var entityFactoryType = typeof(DynamicLayoutFactory<,>).MakeGenericType(typeof(TLayout), ImplementationType);
var layoutFactory = (ILayoutFactory<TLayout>) Activator.CreateInstance(entityFactoryType);

return new DynamicLayout<TLayout, TSchema>(layoutFactory, _properties);
return new DynamicLayout<TLayout, TSchema>(new TemporaryLayoutFactory<TLayout>(), _properties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using Formatters;
using Internals.Extensions;
using Internals.Reflection;
using Translators;
using Translators.FactoryProviders;
using TypeSelectors;
Expand All @@ -16,12 +15,12 @@ public class SchemaBuilder<TSchema> :
where TSchema : Entity
{
readonly IDictionary<Type, IEntityConverter> _entityConverters;
readonly IEntitySelectorFactory _entitySelectorFactory;
readonly IDictionary<Type, IEntityFormatter> _entityFormatters;
readonly IEntitySelectorFactory _entitySelectorFactory;
readonly Dictionary<Type, ILayoutFormatter> _layoutFormatters;
readonly IDictionary<Type, ILayoutParserFactory> _layouts;
IEntityTranslatorFactoryProvider<TSchema> _entityTranslateFactoryProvider;
readonly ITranslatorFactoryProvider<TSchema> _translateFactoryProvider;
readonly Dictionary<Type, ILayoutFormatter> _layoutFormatters;
IEntityTranslatorFactoryProvider<TSchema> _entityTranslateFactoryProvider;

public SchemaBuilder(IEntitySelectorFactory entitySelectorFactory)
{
Expand All @@ -36,23 +35,11 @@ public SchemaBuilder(IEntitySelectorFactory entitySelectorFactory)
_translateFactoryProvider = new SchemaTranslatorFactoryProvider<TSchema>();
}

Type ISchemaLayoutBuilder<TSchema>.GetImplementationType<T>()
{
return TypeCache<T>.ImplementationType;
}

Type ISchemaBuilder<TSchema>.GetImplementationType<T>()
{
return TypeCache<T>.ImplementationType;
}

public IEntityConverter<T> GetEntityConverter<T>()
where T : TSchema
{
if (_entityConverters.TryGetValue(typeof(T), out var result))
{
return result as IEntityConverter<T>;
}

throw new KeyNotFoundException($"The {typeof(T).Name} entity converter was not found");
}
Expand All @@ -61,24 +48,11 @@ public IEntityFormatter<T> GetEntityFormatter<T>()
where T : TSchema
{
if (_entityFormatters.TryGetValue(typeof(T), out var result))
{
return result as IEntityFormatter<T>;
}

throw new KeyNotFoundException($"The {typeof(T).Name} entity formatter was not found");
}

public ILayoutParserFactory<T, TSchema> GetLayout<T>()
where T : Layout
{
if (_layouts.TryGetValue(typeof(T), out var result))
{
return result as ILayoutParserFactory<T, TSchema>;
}

throw new MacheteSchemaException($"The layout {TypeCache<T>.ShortName} was not found.");
}

public void Add<T>(IEntityConverter<T> converter)
where T : TSchema
{
Expand All @@ -100,6 +74,15 @@ public void Add<T>(ILayoutFormatter<T> formatter)
_layoutFormatters[formatter.LayoutType] = formatter;
}

public ILayoutParserFactory<T, TSchema> GetLayout<T>()
where T : Layout
{
if (_layouts.TryGetValue(typeof(T), out var result))
return result as ILayoutParserFactory<T, TSchema>;

throw new MacheteSchemaException($"The layout {TypeCache<T>.ShortName} was not found.");
}

public void SetTranslateFactoryProvider(IEntityTranslatorFactoryProvider<TSchema> entityTranslateFactoryProvider)
{
_entityTranslateFactoryProvider = entityTranslateFactoryProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public SchemaLayoutBuilder(ISchemaLayoutBuilder<TSchema> schemaBuilder)
_schemaBuilder = schemaBuilder;
}

Type ISchemaLayoutBuilder<TSchema>.GetImplementationType<T>()
{
return _schemaBuilder.GetImplementationType<T>();
}

public ILayoutParserFactory<T, TSchema> GetLayout<T>()
where T : Layout
{
Expand Down
Loading

0 comments on commit c51cf43

Please sign in to comment.