現在我們有一個包含所有數據庫調用的dll文件,我無法更改它。不過,我需要從我的Mvc 3項目打電話給我。該進程調用它很簡單,我使用以下命令:Microsoft Unity 2,如何註冊以下內容?
ManageProvider.GetProxy<T>(ident);
T是我想要得到的類從背面的接口(它像它自己的IOC)和Ident是用戶識別類。所以通過致電
var classReturned = ManageProvider.GetProxy<ICommunity>(new UserIden{ Email = "[email protected]" });
我會得到一個類與所有的社區功能。 現在我想在我的Mvc 3項目中實現Unity。問題是,我可以通過統一將這些調用添加到dll文件嗎?
我想通過使用來解決電話:
var classReturned = myContainer.Resolve<ICommunity>(new UserIden{ Email = "[email protected]" });
如何註冊這個在Unity(或者是它甚至有可能)?
更新:
1)它是更好地叫與電子郵件/用戶的ident的方法,而不是定義依賴屬性的? (如下)
2)目前在dll文件中有大約20個接口。我應該將它們全部添加到同一個回收站嗎? (ex below)
public class ProxyWrapper : IDllRepository
{
[Dependency]
public UserIdent UserIdent { get; set; }
public ICommunity GetCommunity()
{
return ManageProvider.GetProxy<ICommunity>(UserIdent);
}
public IDesktop GetDesktop()
{
return ManageProvider.GetProxy<IDesktop>(UserIdent);
}
}
public interface IDllRepository
{
ICommunity GetCommunity();
IDesktop GetDesktop();
}
最好的方法是什麼,我將如何從我的代碼中調用它? [Dependency]屬性是否也屬於Service Locator反模式?
更新11年5月23日
1)是的,類似的東西。它們包含提供給包含dll文件的所有項目的所有邏輯。
關於ManagerProvider。它接受一個接口並返回映射到此接口的類。所以對於社區,界面看起來像這樣(去掉了很多電話,以保持它短的,也有文章,評論,社區創建/更新等):
List<CommunityThread> GetThreads(int pStartRowIndex, int pMaximumRows, string pOrderBy, string pSearchExpression);
Guid? CreateThread(string pTitle, string pDescription, string pPostContent);
bool DeleteThread(Guid pThreadId);
List<CommunityThread> GetCommunityUserThreads(Guid pCommunityUserId);
2)我無法更新ManageProvider.GetProxy是如何工作的。 GetProxy是硬編碼的dll文件中的一個類。這是社區的一部分。如果typeof(接口)...返回類,那麼類對所有其他接口也是如此。
private static IManageProxy GetProxyForInterface<T>(UserIdent pIdent)
{
....
if (typeof(T).Equals(typeof(ICommunity)))
return new PCommunity();
....
}
3)使用這種新的包裝類註冊後,我可以把它通過下面的代碼(MvcUnityContainer是一個靜態類,只有有一個名爲容器)屬性:
var c = MvcUnityContainer.Container.Resolve<IBackendRepository>(new PropertyOverride("UserIdent",
new UserIdent()));
的Global.asax
IUnityContainer container = InitContainer();
MvcUnityContainer.Container = container;
DependencyResolver.SetResolver(new UnityMvcResolver(container));
的問題是,我需要靜態類MvcUnityContainer?是否可以配置DependecyResolver爲我做這件事?喜歡的東西(的問題是,它不接受override參數):
var c = DependencyResolver.Current.GetService<IBackendRepository>(new PropertyOverride("UserIdent", new UserIdent()));
帕特里克,我不確定我是否理解你的設計。 1. GetProxy實際返回什麼樣的接口? 2.這20個接口是「ICommunity」,「IDesktop」等。這些是你的域實體嗎?你能描述一下你不能改變系統部分的設計嗎?以代碼而不是文字顯示。 –
Steven
2011-05-21 09:40:41
更新了問題以回答問題。 – Patrick 2011-05-23 08:39:13