2013-05-27 34 views
0

我使用mvvm light工具包,我有一個問題simpleIOC。我想解析一個接口,但該類有兩個構造函數,並且simpleIOC不知道哪個是默認值。我怎樣才能簽署一個類的默認構造函數?simpleIOC唱默認構造函數

//ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 
      ////if (ViewModelBase.IsInDesignModeStatic) 
      ////{ 
      //// // Create design time view services and models 
      //// SimpleIoc.Default.Register<IDataService, DesignDataService>(); 
      ////} 
      ////else 
      ////{ 
      //// // Create run time view services and models 
      //// SimpleIoc.Default.Register<IDataService, DataService>(); 
      ////} 

這是ninject,正如你看到的,有2 cunstructor,我想使用的第一個,所以我必須簽署它,原因有2個構造函數,國際奧委會不知道,這是必須注射。 我想這樣做,與SimpleIOC從MVVM光工具包,因爲現在我用的是國際奧委會Ninject

[Inject] 
     public RepositoryFactories() 
     { 
      repositoryFactories = GetSpecFactories(); 
     } 

     public RepositoryFactories(IDictionary<Type, Func<DbContext, object>> factories) 
     { 
      repositoryFactories = factories; 
     } 
+0

爲什麼該類有兩個構造函數?這堂課怎麼樣?你的註冊是怎樣的? – Steven

+0

這是ioc容器名稱 ServiceLocator.SetLocatorProvider(()=> SimpleIoc.Default); 我有一個抽象工廠,有2個構造函數,一個用於注入,一個用於獲取基本的 – user1693057

+0

如果真的想幫助你,但沒有任何代碼示例,則不可能提供任何具體的反饋和可能的解決方案。 – Steven

回答

4

嘗試[PreferredConstructor]代替[進樣]

據來自MVVM光工具包的source code文檔正是你在找什麼:

/// <summary> 
/// When used with the SimpleIoc container, specifies which constructor 
/// should be used to instantiate when GetInstance is called. 
/// If there is only one constructor in the class, this attribute is 
/// not needed. 
/// </summary> 
//// [ClassInfo(typeof(SimpleIoc))] 
[AttributeUsage(AttributeTargets.Constructor)] 
public sealed class PreferredConstructorAttribute : Attribute 
{ 
} 

PS。看起來CodePlex更喜歡與特殊HTML字符(如%20和%28)上的實際空格和括號進行鏈接,因此到原始源文件的鏈接有點不完整。無論如何,你可以在這個位置找到文件:

https://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/Ioc/PreferredConstructor.cs