XNA: Adding SpriteBatch as service to class

Started by
3 comments, last by watermelonChris 12 years, 8 months ago
Hey guys, I've made a class (not a game component).

And I'm trying to use Sprite Batch as a service... So I added this to my Game's LoadContent method:

Services.AddService(typeof(SpriteBatch), spriteBatch);


And then in my 'home brewed' class I added:

SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;


But visual studio gives me this error: Error 1 An object reference is required for the non-static field, method, or property 'Microsoft.Xna.Framework.Game.Services.get'

Any ideas? I've tried changing it to Game1.Services.Get...... But that doesn't work either so...
Advertisement
Any reason why you need to add it as a service? Just pass a SpriteBatch in the contructor and you can use it in the class's draw code.



public class Widget
{
SpriteBatch spriteBatch;

public Widget(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
}

public void Draw()
{
spriteBatch.Begin()
/// draw stuff...
}
}

Any reason why you need to add it as a service? Just pass a SpriteBatch in the contructor and you can use it in the class's draw code.



public class Widget
{
SpriteBatch spriteBatch;

public Widget(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
}

public void Draw()
{
spriteBatch.Begin()
/// draw stuff...
}
}



Thank you !!
Also another thing - you're not using the service pattern right, since SpriteBatch is not a "service" per say.

You may have a "SpriteBatchManager" of type "ISpriteBatchService" that manages a SpriteBatch and exposes it to clients (like XNA's GraphicsDeviceManager, which is a type of IGraphicsDeviceService and exposes a GraphicsDevice). So the interface is what you're querying from the service locator (Game.Services), not the implementation and definitely not the content of the service (e.g. a SpriteBatch).

Services are a means of decoupling the concrete implementation of a service/manager object from clients, because a service is added and queried to the service locator by its interface. Client code only is aware of the interface; it doesn't know nor need to know the actual implementation, allowing the service to vary in its implementation without requiring client code to be changed. So having a sprite batch as a service diminishes the point of the pattern, since you're working with a concrete object whose implementation likely won't vary/can't vary nor has its accessed managed (since you're working with it directly), making constructor injection like EJH said the more attractive solution.

Also another thing - you're not using the service pattern right, since SpriteBatch is not a "service" per say.

You may have a "SpriteBatchManager" of type "ISpriteBatchService" that manages a SpriteBatch and exposes it to clients (like XNA's GraphicsDeviceManager, which is a type of IGraphicsDeviceService and exposes a GraphicsDevice). So the interface is what you're querying from the service locator (Game.Services), not the implementation and definitely not the content of the service (e.g. a SpriteBatch).

Services are a means of decoupling the concrete implementation of a service/manager object from clients, because a service is added and queried to the service locator by its interface. Client code only is aware of the interface; it doesn't know nor need to know the actual implementation, allowing the service to vary in its implementation without requiring client code to be changed. So having a sprite batch as a service diminishes the point of the pattern, since you're working with a concrete object whose implementation likely won't vary/can't vary nor has its accessed managed (since you're working with it directly), making constructor injection like EJH said the more attractive solution.


Wow thank you, I didn't quite get all of what your saying though, guessing I should read up on C# OOP?

This topic is closed to new replies.

Advertisement