using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Linq;
|
using System.Reflection;
|
using Microsoft.Practices.Unity;
|
using Microsoft.Practices.Unity.Configuration;
|
|
namespace CY.Infrastructure
|
{
|
/// <summary>
|
/// Represents the Service Locator.
|
/// </summary>
|
public sealed class ServiceLocator : IServiceProvider
|
{
|
#region Private Fields
|
private readonly IUnityContainer container;
|
#endregion
|
|
#region Private Static Fields
|
private static readonly ServiceLocator instance = new ServiceLocator();
|
#endregion
|
|
#region Ctors
|
/// <summary>
|
/// Initializes a new instance of <c>ServiceLocator</c> class.
|
/// </summary>
|
private ServiceLocator()
|
{
|
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
|
container = new UnityContainer();
|
section.Configure(container);
|
}
|
#endregion
|
|
#region Public Static Properties
|
/// <summary>
|
/// Gets the singleton instance of the <c>ServiceLocator</c> class.
|
/// </summary>
|
public static ServiceLocator Instance
|
{
|
get { return instance; }
|
}
|
#endregion
|
|
#region Private Methods
|
private IEnumerable<ParameterOverride> GetParameterOverrides(object overridedArguments)
|
{
|
List<ParameterOverride> overrides = new List<ParameterOverride>();
|
Type argumentsType = overridedArguments.GetType();
|
argumentsType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
.ToList()
|
.ForEach(property =>
|
{
|
var propertyValue = property.GetValue(overridedArguments, null);
|
var propertyName = property.Name;
|
overrides.Add(new ParameterOverride(propertyName, propertyValue));
|
});
|
return overrides;
|
}
|
#endregion
|
|
#region Public Methods
|
/// <summary>
|
/// Gets the service instance with the given type.
|
/// </summary>
|
/// <typeparam name="T">The type of the service.</typeparam>
|
/// <returns>The service instance.</returns>
|
public T GetService<T>()
|
{
|
return container.Resolve<T>();
|
}
|
/// <summary>
|
/// Gets the service instance with the given type by using the overrided arguments.
|
/// </summary>
|
/// <typeparam name="T">The type of the service.</typeparam>
|
/// <param name="overridedArguments">The overrided arguments.</param>
|
/// <returns>The service instance.</returns>
|
public T GetService<T>(object overridedArguments)
|
{
|
var overrides = GetParameterOverrides(overridedArguments);
|
return container.Resolve<T>(overrides.ToArray());
|
}
|
/// <summary>
|
/// Gets the service instance with the given type by using the overrided arguments.
|
/// </summary>
|
/// <param name="serviceType">The type of the service.</param>
|
/// <param name="overridedArguments">The overrided arguments.</param>
|
/// <returns>The service instance.</returns>
|
public object GetService(Type serviceType, object overridedArguments)
|
{
|
var overrides = GetParameterOverrides(overridedArguments);
|
return container.Resolve(serviceType, overrides.ToArray());
|
}
|
#endregion
|
|
#region IServiceProvider Members
|
/// <summary>
|
/// Gets the service instance with the given type.
|
/// </summary>
|
/// <param name="serviceType">The type of the service.</param>
|
/// <returns>The service instance.</returns>
|
public object GetService(Type serviceType)
|
{
|
return container.Resolve(serviceType);
|
}
|
|
#endregion
|
}
|
}
|