Service Container C#

Service Container Presented by: Anton Cherkasov Service Providers §Services are the basic building blocks of the . NET design-time architecture. They help to expose access to specific features from external objects. §Designers and components can obtain other services through the use of service providers. A. NET service provider is represented by the System. IServiceProvider interface. The interface provides a mechanism for retrieving a specific service object, given the type of object to retrieve.

Here is the only member of this interface: object GetService(Type serviceType) This method, when overridden, will return an object that represents the specified service type. If the service provider cannot return an object of the specified type, then callers should expect the return value to be null. IServiceContainer Interface §A service container allows for services to be added and removed to it. Therefore, external objects can have more control over what a service provider exposes.

A service container is actually a service provider. The interface that represents a service container is System. IServiceContainer, which is derived from IServiceProvider, as shown: public interface IServiceContainer : IServiceProvider IServiceContainer Interface Methods §Here are the two methods of this interface: AddService: This method adds the specified service to the service container. This method has four overloads. void AddService(Type serviceType, object service); oid AddService(Type serviceType, ServiceCreatorCallback callback); void AddService(Type serviceType, object service, bool promote); void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote); RemoveService: This method removes the specified service from the service container. This method has two overloads. void RemoveService(Type serviceType); void RemoveService(Type serviceType, bool promote); ServiceContainer Class §The . NET Framework comes with a class that is already derived from IServiceContainer to be used with the designer framework.

This class is: System. ComponentModel. Design. ServiceContainer §This class has two constructor overloads: the default constructor, and one that takes a parent service provider as a parameter. This class uses a Hashtable to store the available services. It stores both service instances and service callbacks into this table for retrieval. How does it work? Pros §The combination of services, providers, and service containers form a simple design pattern that gives a lot of advantages. For example: creates weak binding between client components and the services they use; – creates a simple repository and service discovery mechanism that allows easily scale the application (or part thereof). – allows for lazy loading of services, there is a method AddService overload, creating services, when they ask for the first time; – is an alternative to static classes; – supports programming, based on contracts; – applicable to implement the service-factory; – suitable to create an architecture that supports plug-ins. Questions?

We will write a custom essay sample on
Service Container C#
or any similar topic only for you
We will write a custom essay sample on
Service Container C#
or any similar topic only for you
Order now
Order now

Custom writing services

×

Hi there, would you like to get such a paper? How about receiving a customized one? Check it out