Problem with Ninject
-
I have the following code:
IKernel kernal = new StandardKernel(); kernal.Bind().To(); kernal.Bind().To(); kernal.Bind().To(); package = kernal.Get(); package.Get().Force = new Vector2(10,10); package.Get().Position = new Vector2(2,2);
LocationCom:
\[Family("Location")\] class LocationCom : BaseComponent, ILocationComponent { public Vector2 Position { get; set; } }
MovementCom:
\[Family("Movement")\] class MovementCom : BaseComponent, IRealtimeComponent { private readonly ILocationComponent locationCom; public Vector2 Force { get; set; } \[Inject\] public MovementCom(ILocationComponent locationCom) { this.locationCom = locationCom; } public void Update(GameTime gameTime) { locationCom.Position += Force \* (float)gameTime.ElapsedGameTime.TotalSeconds; } }
ComponentPackage:
public ComponentPackage(IEnumerable components) { foreach (var component in components) { Add(component); } }
The idea is that the movement component will get a reference to location component when injected into the package. The problem is that MovementCom receives a new instance of LocationCom, thus when it applies force it is applying it to an unrelated position (not inside the package). Does Ninject resolve this issue or would I have to write some "adaptor" code for the dependencies to work, I am fairly new to using an IOC container so it is likely to be an easy question ;P
-
I have the following code:
IKernel kernal = new StandardKernel(); kernal.Bind().To(); kernal.Bind().To(); kernal.Bind().To(); package = kernal.Get(); package.Get().Force = new Vector2(10,10); package.Get().Position = new Vector2(2,2);
LocationCom:
\[Family("Location")\] class LocationCom : BaseComponent, ILocationComponent { public Vector2 Position { get; set; } }
MovementCom:
\[Family("Movement")\] class MovementCom : BaseComponent, IRealtimeComponent { private readonly ILocationComponent locationCom; public Vector2 Force { get; set; } \[Inject\] public MovementCom(ILocationComponent locationCom) { this.locationCom = locationCom; } public void Update(GameTime gameTime) { locationCom.Position += Force \* (float)gameTime.ElapsedGameTime.TotalSeconds; } }
ComponentPackage:
public ComponentPackage(IEnumerable components) { foreach (var component in components) { Add(component); } }
The idea is that the movement component will get a reference to location component when injected into the package. The problem is that MovementCom receives a new instance of LocationCom, thus when it applies force it is applying it to an unrelated position (not inside the package). Does Ninject resolve this issue or would I have to write some "adaptor" code for the dependencies to work, I am fairly new to using an IOC container so it is likely to be an easy question ;P
Ninject? Never heard of it. I'd be willing to bet these guys[^] have though.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak