|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewModelServiceManager.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Class that manages services available to view models.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Catel.Properties;
namespace Catel.MVVM.Services
{
/// <summary>
/// Class that manages services available to view models.
/// </summary>
public class ViewModelServiceManager : IServiceProvider
{
#region Variables
/// <summary>
/// Dictionary of services registered with this view model service manager.
/// </summary>
private readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();
/// <summary>
/// Lock object for the services.
/// </summary>
private readonly object _servicesLock = new object();
#endregion
#region Constructor & destructor
/// <summary>
/// Initializes a new instance of the <see cref="ViewModelServiceManager"/> class.
/// </summary>
/// <remarks>
/// For internal use only.
/// </remarks>
internal ViewModelServiceManager()
{
}
#endregion
#region Properties
#endregion
#region Methods
/// <summary>
/// Clears the services.
/// </summary>
public void Clear()
{
lock (_servicesLock)
{
_services.Clear();
}
}
/// <summary>
/// Adds a new service.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <param name="serviceInstance">The service instance.</param>
public void Add(Type serviceType, object serviceInstance)
{
if (serviceType == null)
{
throw new ArgumentNullException("serviceType");
}
if (serviceInstance == null)
{
throw new ArgumentNullException("serviceInstance");
}
lock (_servicesLock)
{
_services[serviceType] = serviceInstance;
}
}
/// <summary>
/// Removes the specified service.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
public void Remove(Type serviceType)
{
if (serviceType == null)
{
throw new ArgumentNullException("serviceType");
}
lock (_servicesLock)
{
_services.Remove(serviceType);
}
}
#endregion
#region IServiceProvider Members
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <returns>
/// A service object of type <paramref name="serviceType"/>.
/// -or-
/// null if there is no service object of type <paramref name="serviceType"/>.
/// </returns>
public object GetService(Type serviceType)
{
if (serviceType == null)
{
throw new ArgumentNullException("serviceType");
}
if (!serviceType.IsInterface)
{
throw new ArgumentException(Exceptions.ParameterMustBeAnInterface, "serviceType");
}
lock (_servicesLock)
{
return _services.ContainsKey(serviceType) ? _services[serviceType] : null;
}
}
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <typeparam name="T">Type of the service to retrieve.</typeparam>
/// <returns>
/// A service object of the specified parameter type.
/// -or-
/// null if there is no service object of the specified parameter type.
/// </returns>
public T GetService<T>()
{
return (T)GetService(typeof(T));
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.