Seitenhierarchie

  Wiki Navigation

    Loading...


 Recently Updated


 Latest Releases

 MediaPortal 1.34
            Releasenews | Download
 MediaPortal 2.5
            Releasenews | Download



GlobalServiceProvider

Singleton Class to provide a global instance of ServiceProvider

Example:

ServiceProvider services = GlobalServiceProvider.Instance;

Also provides pass through methods for some of the ServiceProvider methods

void Add<T>(object service)

Adds a class/service to the dictionary

T Get<T>()

Get a class of Type T from the dictionary

void Replace<T>(object service)

Replaces a class in the dictionary

This means in most cases an instance of ServiceProvider is not required.

ServiceProvider

Is a class which stores other classes in a dictionary, to be used as services.

  • Classes to be stored must have an Interface.
  • Only one type of each class can be stored.

This enables classes to be distributed over the whole project without making them static. A service can be anything you want and allows an easy way to pass information between classes. This concept is known as Dependency Injection (DI).

Methods:

ServiceProvider()

Constructor

void Add<T>(object service)

Adds a class/service to the dictionary

bool IsRegistered<T>()

Checks to see if a class is registered in the dictionary

T Get<T>()

Get a class of Type T from the dictionary

void Remove<T>()

removes a class from the dictionary

void Replace<T>(object service)

Replaces a class in the dictionary

Examples: To add a service to the Service Provider

IMyService service = new MyService();
GlobalServiceProvider.Add<IMyService>(service);

To get service from Service Provider

IMyService service = GlobalServiceProvider.Get<IMySerivce>();

To use a service each class needs a pointer to the service. it can get this every time it needs it, or get it once in the constructor and store it as a member variable.

Here is an example of getting the service in the constructor:

public class MyClass
{
  IMyService _service;  // An example service
  
  public MyClass()
  {
     _service = GlobalServiceProvider.Get<IMyService>();
  } 

  public void MyMethod()
  {
     _service.DoSomething(); // use the service
  }
}

   

 

This page has no comments.