Microsoft.Practices.ServiceLocation
dependency injection
CommonServiceLocator
Microsoft patterns and practices
software design

Where does Microsoft.Practices.ServiceLocation come from?

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

Overview

`Microsoft.Practices.ServiceLocation` is a key component often found in .NET applications that utilize dependency injection and service location patterns. The library is primarily used to manage the instantiation and provision of services in a decoupled manner. Understanding its origin, use cases, and integration within a .NET ecosystem is valuable for developers looking to design flexible, maintainable, and testable software architectures.

Origin and Purpose

The `Microsoft.Practices.ServiceLocation` is part of the larger Microsoft Enterprise Library (MEL), which was designed to provide reusable software components that address common cross-cutting concerns in enterprise development. Specifically, the `ServiceLocation` assembly provides an abstraction over dependency injection and service location.

The Enterprise Library was a part of the patterns & practices initiative by Microsoft, aimed at producing guidance to help developers build quality enterprise-level applications. The Service Location pattern addressed the need for a more dynamic version of dependency injection, allowing for services to be located and utilized at runtime.

Technical Explanation

The core functionality provided by the library centers around the `IServiceLocator` interface. This interface is used to abstract the creation and retrieval of services, typically implemented in conjunction with a dependency injection container.

Key Components

  • `IServiceLocator`: Defines methods for retrieving services by type.
  • `ServiceLocatorProvider`: A delegate that returns the current `IServiceLocator` instance.
  • `ServiceLocator`: A static class that provides a global access point to the `IServiceLocator`.

Example Usage

Below is a simple implementation illustrating how `IServiceLocator` might be used:

  • Unity
  • Autofac
  • StructureMap
  • Ninject
  • Decoupling: Provides a way to decouple application classes from specific implementations.
  • Ease of Configuration: Changing or replacing services becomes a matter of altering the configuration.
  • Runtime Flexibility: Services can be located and instantiated at runtime, offering more dynamic behavior.
  • Global State: The locator introduces a global state through static access, which can make testing and tracking service lifetimes more complex.
  • Overhead: Adds a layer of abstraction, which may introduce overhead and potentially obscure direct dependencies.
  • Obsolescence: Newer patterns and tools (e.g., ASP.NET Core built-in DI) offer more integrated solutions.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD