2011-05-19 23 views
-1

現在我們有一個包含所有數據庫調用的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())); 
+0

帕特里克,我不確定我是否理解你的設計。 1. GetProxy 實際返回什麼樣的接口? 2.這20個接口是「ICommunity」,「IDesktop」等。這些是你的域實體嗎?你能描述一下你不能改變系統部分的設計嗎?以代碼而不是文字顯示。 – Steven 2011-05-21 09:40:41

+0

更新了問題以回答問題。 – Patrick 2011-05-23 08:39:13

回答

3

我認爲你需要躲在另一個抽象創作,例如:

public interface ICommunityRepository 
{ 
    ICommunity GetByEmailAddress(string address); 
} 

public class ManageProviderCommunityRepository 
    : ICommunityRepository 
{ 
    public ICommunity GetByEmailAddress(string address) 
    { 
     var id = new UserIden { Email = address }; 
     return ManageProvider.GetProxy<ICommunity>(id); 
    } 
} 

這將隱藏兩個ManageProvider和這種抽象背後的UserIden,並允許更多的東西取代它以後有用並使測試更容易。

註冊現在是很容易的:

RegisterType<ICommunityRepository, ManageProviderCommunityRepository>(); 

而不是調用myContainer.Resolve(如你在你的例子做)的,注入的依賴在你的類。這可以防止您使用Service Locator anti-pattern

+0

我不能只用ICommunity接口創建一個新類而不是創建一個新接口? – Patrick 2011-05-20 07:56:51

+0

我不知道,因爲我不知道你在做什麼。用這個信息更新你的問題,也許我可以說一些有用的:-) – Steven 2011-05-20 07:59:23

+0

更新了問題;-) – Patrick 2011-05-20 08:24:42

0

也許你可以做這樣的事情,使用InjectionFactory:

myContainer.RegisterType<ICommunity>(
     new InjectionFactory(c => ManageProvider.GetProxy<ICommunity>(new UserIden {Email = "[email protected]"}))); 
    var classReturned = myContainer.Resolve<ICommunity>(); 

...雖然你不會能夠將UserIden作爲參數傳遞給Resolve調用,所以我不確定這是否是您想要的。

要註冊程序集的所有公共類,您可以遍歷Assembly.GetTypes()並以相同的方式註冊它們?

+0

我需要能夠將UserIdent傳遞給unity/get代理。它將採取登錄用戶的電子郵件,然後調用GetProxy。 – Patrick 2011-05-20 07:58:01