XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

How best to access a service?

Last post 11/11/2009 5:42 PM by Cygon4. 6 replies.
  • 11/11/2009 11:08 AM

    How best to access a service?

    Hi Guys,

    This isn't a technical question, I don't think, more a style question.

    I've got a few GameComponents both drawable and non. They will need access to services.

    which is better?

    • Define a property of type I_MyService and explicitly set that to an instance of the service during set-up?

     - OR -

    • Just get an instance of the service in the normal course of the components code when needed?

    XNA Gamer
  • 11/11/2009 11:41 AM In reply to

    Re: How best to access a service?

    I think retrieving the service everytime shouldn't be a bottleneck or something.
    However, i'd go for a member variable which i'd set when first accessed...
  • 11/11/2009 12:07 PM In reply to

    Re: How best to access a service?

    Use a service for when there is only one instance of that type of component. A bit like the singleton approach in a way.
    I like to retrieve my services in any Loadcontent methods I may have in my classes and keep the member variable alive until the class is no longer needed.
  • 11/11/2009 12:16 PM In reply to

    Re: How best to access a service?

    It also depends on the nature of the service. If the service is dynamic, like a plug-in that could be pulled out at any time, then you shouldn't cache a reference.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 11/11/2009 2:10 PM In reply to

    Re: How best to access a service?

    conkerjo:
    I like to retrieve my services in any Loadcontent methods I may have in my classes and keep the member variable alive until the class is no longer needed.

    Ditto.

    Though to be fair, knowing that you can only have one service of a certain class, I used reflector, and in the GameServiceContainer class you can see that GetService() takes it's data from a dictionary, so performance-wise it's not really that much of a hit, even if you call GetService every time.
  • 11/11/2009 2:16 PM In reply to

    Re: How best to access a service?

    Time to advocate for Dependency Injection and Inversion of Control and the related (yes, this is a shameless plug) Bnoerj.Sinject.
    We are boki. The rest is known.

    The not so known part of the rest: It is Björn or Bjoern, but never Bjorn.

    Twitter ~ Bnoerj ~ SharpSteer ~ SgtConker.com
  • 11/11/2009 5:42 PM In reply to

    Re: How best to access a service?

    Same here.

    Either use XNA's component concept and query your services in Initialize() (or LoadContent() or when needed if you wish, just not in the constructor),

    or use dependency injection and add the required services as parameters to your component's constructor, any optional services as properties. Personally, I'm using Ninject with some tricks to make it work even before the Game class' constructor runs.

    Most Inversion of Control containers provide their own implementation of the IServiceContainer interface, so it even integrates well with normal GameComponents if said components don't use XNA's own (slightly flawed due to the reference to the Game class) GameComponent but implement IUpdateable and optionally IDrawable themselves (I have written my own Component and DrawableComponent classes as did one of our MVPs around here, would have provided a link if I could only find it).
    Check out my website and blog for some interesting articles and useful utility classes!
    Nuclex Framework: threaded particles, skinnable GUI, vector fonts, texture atlasses and lots more.
    WiX XNA Installer: Professional-looking MSI installer template for XNA games.
Page 1 of 1 (7 items) Previous Next