Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy use services (IServiceProvider)?
    primarykey
    data
    text
    <p>I'm coming to this question from exploring the XNA framework, but I'd like a general understanding.</p> <pre><code>ISomeService someService = (ISomeService)Game.GetServices(typeof(ISomeService)); </code></pre> <p>and then we do something with whatever functions/properties are in the interface:</p> <pre><code>someService.DoSomething(); // let's say not a static method but doesn't matter </code></pre> <p>I'm trying to figure out why this kind of implementation is any better than:</p> <pre><code>myObject = InstanceFromComponentThatWouldProvideTheService(); myObject.DoSomething(); </code></pre> <p>When you use the services way to get your interface, you're really just getting an instance of the component that provides the service anyway. Right? You can't have an interface "instance". And there's only one class that can be the provider of a service. So all you really have is an instance of your component class, with the only difference being that you only have access to a subset of the component object (whatever subset is in the interface).</p> <p>How is this any different from just having public and private methods and properties? In other words, the public methods/properties of the component <em>is</em> the "interface", and we can stop with all this roundaboutness. You can still change how you implement that "interface" without breaking anything (until you change the method signature, but that would break the services implementation too).</p> <p>And there is going to be a 1-to-1 relationship between the component and the service anyway (more than one class can't register to be a provider of the service), and I can't see a class being a provider of more than one service (srp and all that).</p> <p>So I guess I'm trying to figure out what problem this kind of framework is meant to solve. What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload