Sunday, May 26, 2019

Service Container C#

Service Container Presented by Anton Cherkasov Service Providers Services are the basic building blocks of the . NET design-time architecture. They helper to expose access to specific features from external objects. Designers and components can obtain other helpers through the use of dish out providers. A. NET service provider is stand for by the System. IServiceProvider interface. The interfaceprovides a mechanism for retrieving a specific service object, given the type of object to retrieve.Here is the only member of this interface objectGetService(TypeserviceType) This system acting, when overridden, exit 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 larboard 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. voidRemoveService(TypeserviceType) voidRemoveService(TypeserviceType, boolpromote) ServiceContainer Class The . NET Framework comes with a class that is already derived from IServiceContainer to be use d 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 seduces weak backrest 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?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.