Objects on demand
This commit is contained in:
@ -6,6 +6,8 @@ namespace PatternDemoCore
|
|||||||
|
|
||||||
public class Entity
|
public class Entity
|
||||||
{
|
{
|
||||||
|
public delegate Entity Factory();
|
||||||
|
|
||||||
private static Random random = new Random();
|
private static Random random = new Random();
|
||||||
private int number;
|
private int number;
|
||||||
|
|
||||||
@ -22,16 +24,17 @@ namespace PatternDemoCore
|
|||||||
|
|
||||||
public class ViewModel
|
public class ViewModel
|
||||||
{
|
{
|
||||||
private readonly IContainer container;
|
private readonly Entity.Factory entityFactory;
|
||||||
|
|
||||||
public ViewModel(IContainer container)
|
public ViewModel(Entity.Factory entityFactory)
|
||||||
{
|
{
|
||||||
this.container = container;
|
this.entityFactory = entityFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Method()
|
public void Method()
|
||||||
{
|
{
|
||||||
var entity = container.Resolve<Entity>();
|
var entity = entityFactory();
|
||||||
|
Console.WriteLine(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +43,16 @@ namespace PatternDemoCore
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello World!");
|
var cb = new ContainerBuilder();
|
||||||
|
cb.RegisterType<Entity>().InstancePerDependency();
|
||||||
|
cb.RegisterType<ViewModel>();
|
||||||
|
|
||||||
|
var container = cb.Build();
|
||||||
|
var vm = container.Resolve<ViewModel>();
|
||||||
|
|
||||||
|
vm.Method();
|
||||||
|
vm.Method();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user