mirror of https://github.com/AMT-Cheif/Stylet.git
Remove restriction on calling Compile()
This commit is contained in:
parent
6592efead9
commit
44deff3ac8
10
Stylet.sln
10
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
|
||||
|
|
|
@ -32,7 +32,7 @@ namespace Stylet
|
|||
#region Main Class
|
||||
|
||||
private Dictionary<Type, List<IRegistration>> registrations = new Dictionary<Type, List<IRegistration>>();
|
||||
private bool compiled;
|
||||
private bool compilationStarted;
|
||||
|
||||
public void AutoBind(Assembly assembly = null)
|
||||
{
|
||||
|
@ -44,18 +44,25 @@ namespace Stylet
|
|||
|
||||
public IStyletIoCBindTo<TService> Bind<TService>()
|
||||
{
|
||||
this.CheckCompilationStarted();
|
||||
return new BindTo<TService>(this, false);
|
||||
}
|
||||
|
||||
public IStyletIoCBindTo<TService> BindSingleton<TService>()
|
||||
{
|
||||
this.CheckCompilationStarted();
|
||||
return new BindTo<TService>(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<IRegistration>();
|
||||
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<T>(string key = null)
|
||||
{
|
||||
this.EnsureCompiled();
|
||||
|
||||
return (T)this.Get(typeof(T), key);
|
||||
}
|
||||
|
||||
public IEnumerable<object> 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<T> GetAll<T>(string key = null)
|
||||
{
|
||||
this.EnsureCompiled();
|
||||
return this.GetAll(typeof(T), key).Cast<T>();
|
||||
}
|
||||
|
||||
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<object> Generator { get; }
|
||||
bool WasAutoCreated { get; set; }
|
||||
void EnsureGenerator(StyletIoC service);
|
||||
Func<object> 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<object> Generator { get; protected set; }
|
||||
public bool WasAutoCreated { get; set; }
|
||||
|
||||
public abstract void EnsureGenerator(StyletIoC service);
|
||||
protected Func<object> generator { get; set; }
|
||||
|
||||
public abstract Func<object> 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<object> GetGenerator(StyletIoC service)
|
||||
{
|
||||
if (this.Generator == null)
|
||||
this.Generator = Expression.Lambda<Func<object>>(this.GetInstanceExpression(service)).Compile();
|
||||
if (this.generator == null)
|
||||
this.generator = Expression.Lambda<Func<object>>(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<object> 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)
|
||||
|
|
|
@ -24,7 +24,6 @@ namespace StyletUnitTests
|
|||
{
|
||||
var ioc = new StyletIoC();
|
||||
ioc.Bind<SimpleTest>().ToSelf();
|
||||
ioc.Compile();
|
||||
var obj1 = ioc.Get<SimpleTest>();
|
||||
var obj2 = ioc.Get<SimpleTest>();
|
||||
|
||||
|
@ -38,7 +37,6 @@ namespace StyletUnitTests
|
|||
{
|
||||
var ioc = new StyletIoC();
|
||||
ioc.BindSingleton<SimpleTest>().ToSelf();
|
||||
ioc.Compile();
|
||||
var obj1 = ioc.Get<SimpleTest>();
|
||||
var obj2 = ioc.Get<SimpleTest>();
|
||||
|
||||
|
@ -52,7 +50,6 @@ namespace StyletUnitTests
|
|||
{
|
||||
var ioc = new StyletIoC();
|
||||
ioc.Bind<SimpleTest>().ToFactory(c => new SimpleTest());
|
||||
ioc.Compile();
|
||||
var obj1 = ioc.Get<SimpleTest>();
|
||||
var obj2 = ioc.Get<SimpleTest>();
|
||||
|
||||
|
@ -66,7 +63,6 @@ namespace StyletUnitTests
|
|||
{
|
||||
var ioc = new StyletIoC();
|
||||
ioc.BindSingleton<SimpleTest>().ToFactory(c => new SimpleTest());
|
||||
ioc.Compile();
|
||||
var obj1 = ioc.Get<SimpleTest>();
|
||||
var obj2 = ioc.Get<SimpleTest>();
|
||||
|
||||
|
@ -80,7 +76,6 @@ namespace StyletUnitTests
|
|||
{
|
||||
var ioc = new StyletIoC();
|
||||
ioc.Bind<ISimpleTest>().To<SimpleTest>();
|
||||
ioc.Compile();
|
||||
var obj1 = ioc.Get<ISimpleTest>();
|
||||
var obj2 = ioc.Get<ISimpleTest>();
|
||||
|
||||
|
@ -94,7 +89,6 @@ namespace StyletUnitTests
|
|||
{
|
||||
var ioc = new StyletIoC();
|
||||
ioc.BindSingleton<ISimpleTest>().To<SimpleTest>();
|
||||
ioc.Compile();
|
||||
var obj1 = ioc.Get<ISimpleTest>();
|
||||
var obj2 = ioc.Get<ISimpleTest>();
|
||||
|
||||
|
|
Loading…
Reference in New Issue