Also, as part of this project we are using IoC extensively, but some of the components are going to be shared across a few projects using different IoC container.
This seems to be the perfect fit for the PnP's CommonServiceLocator component.
Given the fact that CWAB has it's own IoC container, I made an small Adaptor so CSL can relay on CWAB IoC infrastructure (which is based on ObjectBuilder v1.0, by the way).
This is just the kind of code which can be handy to others.. so I will post it here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.CompositeWeb;
namespace CompositeWeb.ServiceLocator
{
public class ServiceLocatorAdaptor : ServiceLocatorImplBase
{
private CompositionContainer container;
public ServiceLocatorAdaptor(CompositionContainer container)
{
this.container = container;
}
/// <summary>
/// When implemented by inheriting classes, this method will do the actual work of resolving
/// the requested service instance.
/// </summary>
/// <param name="serviceType">Type of instance requested.</param>
/// <param name="key">Name of registered service you want. May be null.</param>
/// <returns>
/// The requested service instance.
/// </returns>
protected override object DoGetInstance(Type serviceType, string key)
{
if (!String.IsNullOrEmpty(key))
throw new NotSupportedException("CompositeWeb IoC framework does not supports 'key' based services");
var ret = container.Services.Get(serviceType);
//if (ret == null)
// throw new ActivationException("No such service available");
return ret;
}
/// <summary>
/// When implemented by inheriting classes, this method will do the actual work of
/// resolving all the requested service instances.
/// </summary>
/// <param name="serviceType">Type of service requested.</param>
/// <returns>
/// Sequence of service instance objects.
/// </returns>
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
return new List<object>() { container.Services.Get(serviceType) };
}
}
}
Greets..

No comments:
Post a Comment