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