Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop support for Common Service Locator adapter #26

Closed
dotnetjunkie opened this issue Apr 17, 2015 · 0 comments
Closed

Drop support for Common Service Locator adapter #26

dotnetjunkie opened this issue Apr 17, 2015 · 0 comments
Assignees
Milestone

Comments

@dotnetjunkie
Copy link
Collaborator

We are dropping support for the Common Service Locator adapter for Simple Injector v3. The Common Service Locator project is a painful failure from a design perspectice and the pattern behind it is an anti-pattern and even for reusable libraries and frameworks there are better methods available.

If users feel the need to use a framework or library that requires the CSL, the writing of an adapter is trivial as the following example shows:

using System;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation;
using SimpleInjector;

public class SimpleInjectorServiceLocatorAdapter : IServiceLocator {
    private readonly Container container;
    public SimpleInjectorServiceLocatorAdapter(Container container) {
        this.container = container;
    }

    public IEnumerable<TService> GetAllInstances<TService>() {
        return this.container.GetAllInstances(typeof(TService)).Cast<TService>();
    }

    public IEnumerable<object> GetAllInstances(Type serviceType) {
        IServiceProvider serviceProvider = this.container;
        Type collectionType = typeof(IEnumerable<>).MakeGenericType(serviceType);
        var collection = (IEnumerable<object>)serviceProvider.GetService(collectionType);
        return collection ?? Enumerable.Empty<object>();
    }

    public TService GetInstance<TService>(string key) {
        return (TService)this.GetInstance(typeof(TService));
    }

    public TService GetInstance<TService>() {
        return (TService)this.container.GetInstance(typeof(TService));
    }

    public object GetInstance(Type serviceType, string key) {
        return this.GetInstance(serviceType);
    }

    public object GetInstance(Type serviceType) {
        return this.container.GetInstance(serviceType);
    }

    public object GetService(Type serviceType) {
        IServiceProvider serviceProvider = this.container;
        return serviceProvider.GetService(serviceType);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant