Thursday, October 24, 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 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 provid er 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 prog ramming, based on contracts; ââ¬â applicable to implement the service-factory; ââ¬â suitable to create an architecture that supports plug-ins. Questions?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.