我不太清楚如何實現這一點,還是最好的策略是什麼,基本上我有一個(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