Skip to content

Commit 130204a

Browse files
committed
Merge branch 'infrastructure-update'
2 parents c92d950 + 055e8b8 commit 130204a

17 files changed

+462
-132
lines changed

src/EcsRx.Examples/Application/EcsRxConsoleApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace EcsRx.Examples.Application
66
{
77
public abstract class EcsRxConsoleApplication : EcsRxApplication
88
{
9-
protected override IDependencyContainer DependencyContainer { get; } = new NinjectDependencyContainer();
9+
public override IDependencyContainer Container { get; } = new NinjectDependencyContainer();
1010
}
1111
}

src/EcsRx.Examples/ExampleApps/ComputedGroupExample/ComputedGroupExampleApplication.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55
using EcsRx.Examples.ExampleApps.ComputedGroupExample.ComputedGroups;
66
using EcsRx.Examples.ExampleApps.ComputedGroupExample.Systems;
77
using EcsRx.Groups;
8+
using EcsRx.Infrastructure.Extensions;
89

910
namespace EcsRx.Examples.ExampleApps.ComputedGroupExample
1011
{
1112
public class ComputedGroupExampleApplication : EcsRxConsoleApplication
1213
{
1314
private bool _quit;
1415

15-
protected override void ApplicationStarting()
16-
{
17-
RegisterSystem<RandomlyChangeHpSystem>();
18-
base.ApplicationStarting();
19-
}
20-
2116
protected override void ApplicationStarted()
2217
{
2318
var namedHealthGroup = EntityCollectionManager.GetObservableGroup(new Group(typeof(HasHealthComponent), typeof(HasNameComponent)));
@@ -26,7 +21,7 @@ protected override void ApplicationStarted()
2621

2722
SystemExecutor.AddSystem(displayHealthSystem);
2823

29-
RegisterAllBoundSystems();
24+
this.StartAllBoundSystems();
3025

3126
var defaultPool = EntityCollectionManager.GetCollection();
3227
defaultPool.CreateEntity(new CharacterBlueprint("Bob", 200));

src/EcsRx.Examples/ExampleApps/HealthExample/HealthExampleApplication.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using EcsRx.Examples.ExampleApps.HealthExample.Events;
77
using EcsRx.Examples.ExampleApps.HealthExample.Systems;
88
using EcsRx.Extensions;
9+
using EcsRx.Infrastructure.Extensions;
910

1011
namespace EcsRx.Examples.ExampleApps.HealthExample
1112
{
@@ -15,15 +16,9 @@ public class HealthExampleApplication : EcsRxConsoleApplication
1516
private IEntity _enemy;
1617
private readonly Random _random = new Random();
1718

18-
protected override void ApplicationStarting()
19-
{
20-
RegisterSystem<TakeDamageSystem>();
21-
RegisterSystem<DisplayHealthChangesSystem>();
22-
}
23-
2419
protected override void ApplicationStarted()
2520
{
26-
RegisterAllBoundSystems();
21+
this.StartAllBoundSystems();
2722

2823
var defaultPool = EntityCollectionManager.GetCollection();
2924
_enemy = defaultPool.CreateEntity(new EnemyBlueprint(100));

src/EcsRx.Examples/ExampleApps/HelloWorldExample/HelloWorldExampleApplication.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22
using EcsRx.Examples.Application;
33
using EcsRx.Examples.ExampleApps.HelloWorldExample.Components;
44
using EcsRx.Examples.ExampleApps.HelloWorldExample.Systems;
5+
using EcsRx.Infrastructure.Extensions;
56

67
namespace EcsRx.Examples.ExampleApps.HelloWorldExample
78
{
89
public class HelloWorldExampleApplication : EcsRxConsoleApplication
910
{
1011
private bool _quit;
1112

12-
protected override void ApplicationStarting()
13-
{
14-
RegisterSystem<TalkingSystem>();
15-
}
16-
1713
protected override void ApplicationStarted()
1814
{
19-
RegisterAllBoundSystems();
15+
this.StartAllBoundSystems();
2016

2117
var defaultPool = EntityCollectionManager.GetCollection();
2218
var entity = defaultPool.CreateEntity();

src/EcsRx.Examples/ExampleApps/Performance/EntityPerformanceApplication.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using EcsRx.Examples.ExampleApps.Performance.Components;
1010
using EcsRx.Examples.ExampleApps.Performance.Helper;
1111
using EcsRx.Extensions;
12+
using EcsRx.Infrastructure.Extensions;
1213

1314
namespace EcsRx.Examples.ExampleApps.Performance
1415
{
@@ -32,7 +33,7 @@ protected override void ApplicationStarted()
3233
.Select(x => Activator.CreateInstance(x) as IComponent)
3334
.ToArray();
3435

35-
var componentRepository = DependencyContainer.Resolve<IComponentRepository>();
36+
var componentRepository = Container.Resolve<IComponentRepository>();
3637

3738
_entities = new List<IEntity>();
3839
for (var i = 0; i < EntityCount; i++)

src/EcsRx.Examples/ExampleApps/Performance/Modules/CustomFrameworkModule.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
using EcsRx.Groups.Observable;
1212
using EcsRx.Infrastructure.Dependencies;
1313
using EcsRx.Infrastructure.Events;
14+
using EcsRx.Infrastructure.Extensions;
1415
using EcsRx.MicroRx;
1516
using EcsRx.MicroRx.Events;
1617
using EcsRx.Systems.Handlers;
1718

1819
namespace EcsRx.Examples.ExampleApps.Performance.Modules
1920
{
20-
public class CustomFrameworkModule : IDependencyModule
21+
public class OptimizedFrameworkModule : IDependencyModule
2122
{
2223
public void Setup(IDependencyContainer container)
2324
{

src/EcsRx.Examples/ExampleApps/Performance/OptimizedEntityPerformanceApplication.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using EcsRx.Examples.ExampleApps.Performance.Modules;
1212
using EcsRx.Extensions;
1313
using EcsRx.Infrastructure.Dependencies;
14+
using EcsRx.Infrastructure.Extensions;
1415

1516
namespace EcsRx.Examples.ExampleApps.Performance
1617
{
@@ -24,9 +25,9 @@ public class OptimizedEntityPerformanceApplication : EcsRxConsoleApplication
2425

2526
private List<IEntity> _entities;
2627

27-
protected override IDependencyModule GetFrameworkModule()
28-
{ return new CustomFrameworkModule(); }
29-
28+
protected override void LoadModules()
29+
{ Container.LoadModule<OptimizedFrameworkModule>(); }
30+
3031
protected override void ApplicationStarted()
3132
{
3233
var componentNamespace = typeof(Component1).Namespace;
@@ -40,7 +41,7 @@ protected override void ApplicationStarted()
4041

4142
_availableComponentTypeIds = Enumerable.Range(0, 20).ToArray();
4243

43-
var componentRepository = DependencyContainer.Resolve<IComponentRepository>();
44+
var componentRepository = Container.Resolve<IComponentRepository>();
4445

4546
_entities = new List<IEntity>();
4647
for (var i = 0; i < EntityCount; i++)

src/EcsRx.Examples/ExampleApps/Performance/OptimizedGroupPerformanceApplication.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using EcsRx.Examples.ExampleApps.Performance.Modules;
1010
using EcsRx.Extensions;
1111
using EcsRx.Infrastructure.Dependencies;
12+
using EcsRx.Infrastructure.Extensions;
1213

1314
namespace EcsRx.Examples.ExampleApps.Performance
1415
{
@@ -19,8 +20,8 @@ public class OptimizedGroupPerformanceApplication : EcsRxConsoleApplication
1920
private readonly RandomGroupFactory _groupFactory = new RandomGroupFactory();
2021
private readonly Random _random = new Random();
2122

22-
protected override IDependencyModule GetFrameworkModule()
23-
{ return new CustomFrameworkModule(); }
23+
protected override void LoadModules()
24+
{ Container.LoadModule<OptimizedFrameworkModule>(); }
2425

2526
protected override void ApplicationStarted()
2627
{

src/EcsRx.Infrastructure.Ninject/NinjectDependencyContainer.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Linq;
35
using EcsRx.Infrastructure.Dependencies;
46
using Ninject;
@@ -29,19 +31,19 @@ public NinjectDependencyContainer(IKernel kernel = null)
2931

3032
public object NativeContainer => _kernel;
3133

32-
public void Bind<TFrom, TTo>(BindingConfiguration configuration = null) where TTo : TFrom
34+
public void Bind(Type fromType, Type toType, BindingConfiguration configuration = null)
3335
{
34-
var bindingSetup = _kernel.Bind<TFrom>();
36+
var bindingSetup = _kernel.Bind(fromType);
3537

3638
if (configuration == null)
3739
{
38-
bindingSetup.To<TTo>().InSingletonScope();
40+
bindingSetup.To(toType).InSingletonScope();
3941
return;
4042
}
4143

4244
if (configuration.ToInstance != null)
4345
{
44-
var instanceBinding = bindingSetup.ToConstant((TFrom) configuration.ToInstance);
46+
var instanceBinding = bindingSetup.ToConstant(configuration.ToInstance);
4547

4648
if(configuration.AsSingleton)
4749
{ instanceBinding.InSingletonScope(); }
@@ -51,15 +53,15 @@ public void Bind<TFrom, TTo>(BindingConfiguration configuration = null) where TT
5153

5254
if (configuration.ToMethod != null)
5355
{
54-
var methodBinding = bindingSetup.ToMethod(x => (TTo)configuration.ToMethod(this));
56+
var methodBinding = bindingSetup.ToMethod(x => configuration.ToMethod(this));
5557

5658
if(configuration.AsSingleton)
5759
{ methodBinding.InSingletonScope(); }
5860

5961
return;
6062
}
6163

62-
var binding = bindingSetup.To<TTo>();
64+
var binding = bindingSetup.To(toType);
6365

6466
if(configuration.AsSingleton)
6567
{ binding.InSingletonScope(); }
@@ -77,34 +79,33 @@ public void Bind<TFrom, TTo>(BindingConfiguration configuration = null) where TT
7779
{ binding.WithConstructorArgument(constructorArg.Key, constructorArg.Value); }
7880
}
7981

80-
public void Bind<T>(BindingConfiguration configuration = null)
81-
{ Bind<T,T>(configuration); }
82+
public void Bind(Type type, BindingConfiguration configuration = null)
83+
{ Bind(type, type, configuration); }
8284

83-
public bool HasBinding<T>(string name = null)
84-
{ return _kernel.GetBindings(typeof(T)).Any(x => x.Metadata.Name == name); }
85-
86-
public T Resolve<T>(string name = null)
85+
public bool HasBinding(Type type, string name = null)
8786
{
87+
var applicableBindings = _kernel.GetBindings(type);
88+
8889
if(string.IsNullOrEmpty(name))
89-
{ return _kernel.Get<T>(); }
90-
91-
return _kernel.Get<T>(name);
90+
{ return applicableBindings.Any(); }
91+
92+
return applicableBindings.Any(x => x.Metadata.Name == name);
9293
}
9394

94-
public void Unbind<T>()
95+
public object Resolve(Type type, string name = null)
9596
{
96-
_kernel.Unbind<T>();
97+
if(string.IsNullOrEmpty(name))
98+
{ return _kernel.Get(type); }
99+
100+
return _kernel.Get(type, name);
97101
}
98102

99-
public IEnumerable<T> ResolveAll<T>()
100-
{ return _kernel.GetAll<T>(); }
103+
public void Unbind(Type type)
104+
{ _kernel.Unbind(type); }
105+
106+
public IEnumerable ResolveAll(Type type)
107+
{ return _kernel.GetAll(type); }
101108

102-
public void LoadModule<T>() where T : IDependencyModule, new()
103-
{
104-
var module = new T();
105-
LoadModule(module);
106-
}
107-
108109
public void LoadModule(IDependencyModule module)
109110
{ module.Setup(this); }
110111
}

src/EcsRx.Infrastructure/Dependencies/IDependencyContainer.cs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24

35
namespace EcsRx.Infrastructure.Dependencies
46
{
@@ -18,56 +20,51 @@ public interface IDependencyContainer
1820
/// will not be cross platform, so be weary if you need it or not.
1921
/// </summary>
2022
object NativeContainer { get; }
21-
23+
2224
/// <summary>
2325
/// Binds from one type to another, generally from an interface to a concrete class
2426
/// </summary>
27+
/// <param name="fromType">Type to bind from</param>
28+
/// <param name="toType">Type to bind to</param>
2529
/// <param name="configuration">Optional configuration</param>
26-
/// <typeparam name="TFrom">Type to bind from</typeparam>
27-
/// <typeparam name="TTo">Type to bind to</typeparam>
28-
void Bind<TFrom, TTo>(BindingConfiguration configuration = null) where TTo : TFrom;
29-
30+
void Bind(Type fromType, Type toType, BindingConfiguration configuration = null);
31+
3032
/// <summary>
31-
/// Bind to itself, useful for concrete classes to themselves
33+
/// Bind the type to itself/instance/method, useful for concrete class bindings
3234
/// </summary>
35+
/// <param name="type">The type to bind</param>
3336
/// <param name="configuration">Optional configuration</param>
34-
/// <typeparam name="T">Both source and destination binding, i.e concrete class</typeparam>
35-
void Bind<T>(BindingConfiguration configuration = null);
37+
/// <remarks>This is useful for self binding concrete classes</remarks>
38+
void Bind(Type type, BindingConfiguration configuration = null);
3639

3740
/// <summary>
3841
/// Checks to see if a binding exists in the container
3942
/// </summary>
43+
/// <param name="type">Type to check against</param>
4044
/// <param name="name">Optional name of the binding</param>
41-
/// <typeparam name="T">Type to check against</typeparam>
42-
/// <returns>True if the type has been bound, false if not</returns>
43-
bool HasBinding<T>(string name = null);
45+
/// <returns></returns>
46+
bool HasBinding(Type type, string name = null);
4447

4548
/// <summary>
4649
/// Gets an instance of a given type from the underlying DI container
4750
/// </summary>
51+
/// <param name="type">Type of the object you want</param>
4852
/// <param name="name">Optional name of the binding</param>
49-
/// <typeparam name="T">Type of the object you want</typeparam>
5053
/// <returns>An instance of the given type</returns>
51-
T Resolve<T>(string name = null);
52-
54+
object Resolve(Type type, string name = null);
55+
5356
/// <summary>
54-
/// Unbinds a type from the given context
57+
/// Unbinds a type from the container
5558
/// </summary>
56-
/// <typeparam name="T">The type to unbind</typeparam>
57-
void Unbind<T>();
59+
/// <param name="type">The type to unbind</param>
60+
void Unbind(Type type);
5861

5962
/// <summary>
6063
/// Gets an enumerable of a given type from the underlying DI container
6164
/// </summary>
62-
/// <typeparam name="T">Type to resolve</typeparam>
65+
/// <param name="type">Type to resolve</param>
6366
/// <returns>All matching instances of that type within the underlying container</returns>
64-
IEnumerable<T> ResolveAll<T>();
65-
66-
/// <summary>
67-
/// Loads the given modules bindings into the underlying di container
68-
/// </summary>
69-
/// <typeparam name="T">Type of module to load</typeparam>
70-
void LoadModule<T>() where T : IDependencyModule, new();
67+
IEnumerable ResolveAll(Type type);
7168

7269
/// <summary>
7370
/// Loads the given modules bindings into the underlying di container

0 commit comments

Comments
 (0)