Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with Ninject

Problem with Ninject

Scheduled Pinned Locked Moved C#
helpdockerquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • V venomation

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Ninject? Never heard of it. I'd be willing to bet these guys[^] have though.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups