public void MeasurePerformance() { int iterations = 1000; long reflectionBased = 0; long stronglyTyped = 0; BuilderBase <BuilderStage> builder = new BuilderBase <BuilderStage>(); builder.Strategies.AddNew <SingletonStrategy>(BuilderStage.PreCreation); builder.Strategies.AddNew <CreationStrategy>(BuilderStage.Creation); builder.Policies.SetDefault <ICreationPolicy>(new DefaultCreationPolicy()); Locator locator = new Locator(); IService service = new Service(); IFoo foo = new Foo(); locator.Add(new DependencyResolutionLocatorKey(typeof(IService), null), service); locator.Add(new DependencyResolutionLocatorKey(typeof(IFoo), null), foo); // Build once for warmup builder.BuildUp <MockWithCtorDependencies>(locator, null, null); Stopwatch watch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { builder.BuildUp <MockWithCtorDependencies>(locator, null, null); } watch.Stop(); reflectionBased = watch.ElapsedTicks / iterations; // Build once for warmup PolicyList policies = new PolicyList(); policies.Set <ICreationPolicy>(new MockWithCtorDependenciesPolicy(), typeof(MockWithCtorDependencies), null); builder.BuildUp <MockWithCtorDependencies>(locator, null, null, policies); watch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { builder.BuildUp <MockWithCtorDependencies>(locator, null, null, policies); } watch.Stop(); stronglyTyped = watch.ElapsedTicks / iterations; Console.WriteLine("Reflection: {0}\r\nStrongTyped: {1}\r\nTool {2}% the time of the reflection approach.", reflectionBased, stronglyTyped, stronglyTyped * 100 / reflectionBased); }
public void CanInjectStronglyTypedConstructor() { BuilderBase <BuilderStage> builder = new BuilderBase <BuilderStage>(); builder.Strategies.AddNew <SingletonStrategy>(BuilderStage.PreCreation); builder.Strategies.AddNew <CreationStrategy>(BuilderStage.Creation); builder.Policies.SetDefault <ICreationPolicy>(new DefaultCreationPolicy()); Locator locator = new Locator(); IService service = new Service(); IFoo foo = new Foo(); locator.Add(new DependencyResolutionLocatorKey(typeof(IService), null), service); locator.Add(new DependencyResolutionLocatorKey(typeof(IFoo), null), foo); PolicyList policies = new PolicyList(); policies.Set <ICreationPolicy>(new MockWithCtorDependenciesPolicy(), typeof(MockWithCtorDependencies), null); MockWithCtorDependencies mock = builder.BuildUp <MockWithCtorDependencies>(locator, null, null, policies); Assert.IsNotNull(mock); Assert.AreSame(service, mock.service); Assert.AreSame(foo, mock.foo); }
public void RunningABuilderBaseWithNoStrategiesThrows() { IBuilder <BuilderStage> builder = new BuilderBase <BuilderStage>(); Locator locator = CreateLocator(); builder.BuildUp <Object>(locator, null, null); }
public void BuildUpWithNoLocatorDoesNotThrow() { IBuilder <BuilderStage> builder = new BuilderBase <BuilderStage>(); Locator locator = null; MockStrategy strategy = new MockStrategy(); builder.Strategies.Add(strategy, BuilderStage.PreCreation); builder.BuildUp <object>(locator, null, null); }
public void AddingStrategyByAnyStageRunsItDuringBuild() { IBuilder <BuilderStage> builder = new BuilderBase <BuilderStage>(); Locator locator = CreateLocator(); MockStrategy strategy = new MockStrategy(); builder.Strategies.Add(strategy, BuilderStage.PreCreation); builder.BuildUp <object>(locator, null, null); Assert.IsTrue(strategy.BuildWasRun); }
public void StrategyStagesRunInProperOrder() { IBuilder <BuilderStage> builder = new BuilderBase <BuilderStage>(); Locator locator = CreateLocator(); MockStrategy strategy = new MockStrategy(); builder.Strategies.Add(new MockStrategy("Creation "), BuilderStage.Creation); builder.Strategies.Add(new MockStrategy("PostInitialization "), BuilderStage.PostInitialization); builder.Strategies.Add(new MockStrategy("PreCreation "), BuilderStage.PreCreation); builder.Strategies.Add(new MockStrategy("Initialization "), BuilderStage.Initialization); string s = builder.BuildUp <string>(locator, null, null); Assert.AreEqual("PreCreation Creation Initialization PostInitialization ", s); }
public void LCGInjectionIsFaster() { Locator locator = new Locator(); BuilderBase <BuilderStage> builder = new BuilderBase <BuilderStage>(); builder.Strategies.AddNew <ConstructorReflectionStrategy>(BuilderStage.PreCreation); builder.Strategies.AddNew <CreationStrategy>(BuilderStage.Creation); builder.Strategies.AddNew <PropertySetterStrategy>(BuilderStage.Initialization); PropertySetterPolicy policy = new PropertySetterPolicy(); policy.Properties.Add("B", new PropertySetterInfo("B", new CreationParameter(typeof(Bar)))); builder.Policies.Set <IPropertySetterPolicy>(policy, typeof(Foo), null); long ticks = 0; for (int i = 0; i < iterations; i++) { Stopwatch watch = new Stopwatch(); watch.Start(); Foo a = builder.BuildUp <Foo>(locator, null, null); Assert.IsNotNull(a.B); watch.Stop(); if (i % 100 == 0) { GC.Collect(); } ticks += watch.ElapsedTicks; } long avg = ticks / iterations; locator = new Locator(); builder = new BuilderBase <BuilderStage>(); builder.Strategies.AddNew <ConstructorReflectionStrategy>(BuilderStage.PreCreation); builder.Strategies.AddNew <CreationStrategy>(BuilderStage.Creation); builder.Strategies.AddNew <PropertySetterStrategy>(BuilderStage.Initialization); policy = new PropertySetterPolicy(); policy.Properties.Add("B", new FastPropertySetterInfo("B", new CreationParameter(typeof(Bar)))); builder.Policies.Set <IPropertySetterPolicy>(policy, typeof(Foo), null); ticks = 0; for (int i = 0; i < iterations; i++) { Stopwatch watch = new Stopwatch(); watch.Start(); Foo a = builder.BuildUp <Foo>(locator, null, null); Assert.IsNotNull(a.B); watch.Stop(); if (i % 100 == 0) { GC.Collect(); } ticks += watch.ElapsedTicks; } long avg2 = ticks / iterations; Console.WriteLine("{0} vs {1}", avg, avg2); Assert.IsTrue(avg2 < avg); }
/// <summary> /// Builds an anonymous, non-singleton, non-lifetime managed object of type /// <typeparamref name="TTypeToBuild"/>. /// </summary> /// <typeparam name="TTypeToBuild">The type of the object to build.</typeparam> /// <returns></returns> public TTypeToBuild BuildUp <TTypeToBuild>() { return(builder.BuildUp <TTypeToBuild>(locator, null, null)); }