diff --git a/Stylet.sln b/Stylet.sln index 510ad23..bf1e58c 100644 --- a/Stylet.sln +++ b/Stylet.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.30110.0 +# Visual Studio Express 2013 for Windows Desktop +VisualStudioVersion = 12.0.21005.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stylet", "Stylet\Stylet.csproj", "{2435BD00-AC12-48B0-AD36-9BAB2FDEC3F5}" EndProject @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{AE3462 .nuget\NuGet.targets = .nuget\NuGet.targets EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sandbox", "Sandbox\Sandbox.csproj", "{2C2BAF14-AF38-459A-BB80-9B7CADE315A5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,6 +30,10 @@ Global {13AFA20D-CCEA-4A58-920E-4D8816C7296B}.Debug|Any CPU.Build.0 = Debug|Any CPU {13AFA20D-CCEA-4A58-920E-4D8816C7296B}.Release|Any CPU.ActiveCfg = Release|Any CPU {13AFA20D-CCEA-4A58-920E-4D8816C7296B}.Release|Any CPU.Build.0 = Release|Any CPU + {2C2BAF14-AF38-459A-BB80-9B7CADE315A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C2BAF14-AF38-459A-BB80-9B7CADE315A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C2BAF14-AF38-459A-BB80-9B7CADE315A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C2BAF14-AF38-459A-BB80-9B7CADE315A5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Stylet/StyletIoC.cs b/Stylet/StyletIoC.cs index cabbd33..9e3578c 100644 --- a/Stylet/StyletIoC.cs +++ b/Stylet/StyletIoC.cs @@ -32,7 +32,7 @@ namespace Stylet #region Main Class private Dictionary> registrations = new Dictionary>(); - private bool compiled; + private bool compilationStarted; public void AutoBind(Assembly assembly = null) { @@ -44,18 +44,25 @@ namespace Stylet public IStyletIoCBindTo Bind() { + this.CheckCompilationStarted(); return new BindTo(this, false); } public IStyletIoCBindTo BindSingleton() { + this.CheckCompilationStarted(); return new BindTo(this, true); } + private void CheckCompilationStarted() + { + if (this.compilationStarted) + throw new StyletIoCException("Once you've started to retrieve items from the container, or have called Compile(), you cannot register new services"); + } + public void Compile() { - if (this.compiled) - throw new StyletIoCException("This StyletIoC container has already been compiled"); + this.compilationStarted = true; var toRemove = new List(); foreach (var kvp in this.registrations) @@ -65,7 +72,7 @@ namespace Stylet { try { - registration.EnsureGenerator(this); + registration.GetGenerator(this); } catch (StyletIoCFindConstructorException e) { @@ -85,40 +92,29 @@ namespace Stylet foreach (var remove in toRemove) this.registrations[kvp.Key].Remove(remove); } - - this.compiled = true; } public object Get(Type type, string key = null) { - this.EnsureCompiled(); - return this.GetRegistration(type, key).Generator(); + return this.GetRegistration(type, key).GetGenerator(this)(); } public T Get(string key = null) { - this.EnsureCompiled(); + return (T)this.Get(typeof(T), key); } public IEnumerable GetAll(Type type, string key = null) { - this.EnsureCompiled(); - return this.GetRegistrations(type, key).Select(x => x.Generator()); + return this.GetRegistrations(type, key).Select(x => x.GetGenerator(this)()); } public IEnumerable GetAll(string key = null) { - this.EnsureCompiled(); return this.GetAll(typeof(T), key).Cast(); } - private void EnsureCompiled() - { - if (!this.compiled) - throw new StyletIocNotCompiledException("You need to run Compile() after adding bindings, before fetching any"); - } - private bool CanResolve(Type type) { return this.registrations.ContainsKey(type); @@ -240,9 +236,8 @@ namespace Stylet { string Key { get; } Type Type { get; } - Func Generator { get; } bool WasAutoCreated { get; set; } - void EnsureGenerator(StyletIoC service); + Func GetGenerator(StyletIoC service); Expression GetInstanceExpression(StyletIoC service); } @@ -252,10 +247,11 @@ namespace Stylet public string Key { get { return this.creator.Key; } } public Type Type { get { return this.creator.Type; } } - public Func Generator { get; protected set; } public bool WasAutoCreated { get; set; } - public abstract void EnsureGenerator(StyletIoC service); + protected Func generator { get; set; } + + public abstract Func GetGenerator(StyletIoC service); public abstract Expression GetInstanceExpression(StyletIoC service); } @@ -272,10 +268,11 @@ namespace Stylet return this.creator.GetInstanceExpression(service); } - public override void EnsureGenerator(StyletIoC service) + public override Func GetGenerator(StyletIoC service) { - if (this.Generator == null) - this.Generator = Expression.Lambda>(this.GetInstanceExpression(service)).Compile(); + if (this.generator == null) + this.generator = Expression.Lambda>(this.GetInstanceExpression(service)).Compile(); + return this.generator; } } @@ -299,13 +296,14 @@ namespace Stylet this.instanceInstantiated = true; } - public override void EnsureGenerator(StyletIoC service) + public override Func GetGenerator(StyletIoC service) { - if (this.Generator != null) - return; - this.EnsureInstantiated(service); - this.Generator = () => this.instance; + + if (this.generator == null) + this.generator = () => this.instance; + + return this.generator; } public override Expression GetInstanceExpression(StyletIoC service) diff --git a/StyletUnitTests/StyletIoCTests.cs b/StyletUnitTests/StyletIoCTests.cs index 9960cb7..7ef8b60 100644 --- a/StyletUnitTests/StyletIoCTests.cs +++ b/StyletUnitTests/StyletIoCTests.cs @@ -24,7 +24,6 @@ namespace StyletUnitTests { var ioc = new StyletIoC(); ioc.Bind().ToSelf(); - ioc.Compile(); var obj1 = ioc.Get(); var obj2 = ioc.Get(); @@ -38,7 +37,6 @@ namespace StyletUnitTests { var ioc = new StyletIoC(); ioc.BindSingleton().ToSelf(); - ioc.Compile(); var obj1 = ioc.Get(); var obj2 = ioc.Get(); @@ -52,7 +50,6 @@ namespace StyletUnitTests { var ioc = new StyletIoC(); ioc.Bind().ToFactory(c => new SimpleTest()); - ioc.Compile(); var obj1 = ioc.Get(); var obj2 = ioc.Get(); @@ -66,7 +63,6 @@ namespace StyletUnitTests { var ioc = new StyletIoC(); ioc.BindSingleton().ToFactory(c => new SimpleTest()); - ioc.Compile(); var obj1 = ioc.Get(); var obj2 = ioc.Get(); @@ -80,7 +76,6 @@ namespace StyletUnitTests { var ioc = new StyletIoC(); ioc.Bind().To(); - ioc.Compile(); var obj1 = ioc.Get(); var obj2 = ioc.Get(); @@ -94,7 +89,6 @@ namespace StyletUnitTests { var ioc = new StyletIoC(); ioc.BindSingleton().To(); - ioc.Compile(); var obj1 = ioc.Get(); var obj2 = ioc.Get();