我不太清楚如何实现这一点,还是最好的策略是什么,基本上我有一个(MVC)控制器工厂类 - 使用IoC容器来注册和解析类型
public TestController(IService1 service1, IService2 service2,...)
{ }
(在时刻只有2个参数,但可能会增加)。
我的想法是,我会创建一个服务工厂类,因此不是每个服务的参数,我们可以代替有一个工厂,然后得到什么服务,我们需要
private IService1 _service1;
public TestController(IServiceFactory serviceFactory)
{
// this could also be called from a separate action,
// so we only get it when we need it
_service1 = serviceFactory.Get<IService1>();
}
现在我的服务工厂实现是有点废话,只是有本质上我是缓存所有注册服务的字典和类型:
/// <summary>
/// Service factory class
/// </summary>
/// <remarks>
/// Only one instance of this class should be created during the lifetime of the application
/// </remarks>
public class ServiceFactory : IServiceFactory
{
/// <summary>
/// Locking object
/// </summary>
private static readonly object _lock = new object();
/// <summary>
/// Collection of mappings
/// </summary>
private IDictionary<string, Func<IService>> _mappings;
/// <summary>
/// Default constructor
/// </summary>
public ServiceFactory()
{
_mappings = new Dictionary<string, Func<IService>>();
registerMappings();
}
/// <summary>
/// Get a service from the factory
/// </summary>
public T GetService<T>() where T : IService
{
if (_mappings.Count == 0)
throw new InvalidOperationException("There are no mappings");
lock (_lock)
{
var typeName = typeof(T).Name;
if (_mappings.ContainsKey(typeName))
return (T)_mappings[typeName]();
return default(T);
}
}
/// <summary>
/// Register the mappings needed for this service factory
/// </summary>
private void registerMappings()
{
register<IService1>(() => new Service1())
.register<IService2>(() => new Service2())
.
.
.register<IServiceN>(() => new ServiceN());
}
/// <summary>
/// Register the service classes
/// </summary>
private ServiceFactory register<T>(Func<IService> mapping) where T : IService
{
var type = typeof(T).Name;
if (!_mappings.ContainsKey(type))
_mappings.Add(type, mapping);
return this;
}
}
我的问题是,我可以在维修厂使用IoC容器,让它处理注册和类型的解决?这是一个好方法吗?
或者我可以有一个更基本的问题,我需要一个服务工厂,我应该使用吗?
这仅仅是因为我的MVC控制器需要重构,即我可以尝试和坚持一个控制器为每个服务一个简单的事情?
只是想在最好的办法是什么在这里的一些技巧,我还是个新人,当谈到DI /工厂模式/一些,其他模式等:)
非常感谢。
看起来你正试图建立自己的IoC容器。使用IoC容器时,您不需要工厂。 – 2013-04-10 18:19:47
将依赖注入想象为*“将依赖关系传递给类构造函数”可能会有帮助。*如果您这么想,那么所有这些都可能变得更加清晰。对于这个简单的机制,IoC容器实际上只是很多仪式。因此,如果你的场景与编写一个以某个接口为参数的类的想法兼容,然后在构建该类时提供了该接口的一些实现,那么它对于IoC来说可能是一个很好的候选者。 – 2013-04-10 18:19:59
我不会在这里添加额外的复杂层。 – 2013-04-10 18:26:15