1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-30 19:56:54 +00:00
Sonarr/NzbDrone.Common/ServiceFactory.cs

45 lines
1.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Collections.Generic;
2013-05-10 23:53:50 +00:00
using NzbDrone.Common.Composition;
namespace NzbDrone.Common
{
public interface IServiceFactory
{
T Build<T>() where T : class;
IEnumerable<T> BuildAll<T>() where T : class;
object Build(Type contract);
2013-05-13 02:52:55 +00:00
IEnumerable<Type> GetImplementations(Type contract);
}
public class ServiceFactory : IServiceFactory
{
2013-05-10 23:53:50 +00:00
private readonly IContainer _container;
2013-05-10 23:53:50 +00:00
public ServiceFactory(IContainer container)
{
_container = container;
}
public T Build<T>() where T : class
{
return _container.Resolve<T>();
}
public IEnumerable<T> BuildAll<T>() where T : class
{
return _container.ResolveAll<T>().GroupBy(c => c.GetType().FullName).Select(g => g.First());
}
public object Build(Type contract)
{
return _container.Resolve(contract);
}
2013-05-13 02:52:55 +00:00
public IEnumerable<Type> GetImplementations(Type contract)
{
return _container.GetImplementations(contract);
}
}
}