2013-12-13 10 views
0

我在註冊StructureMap容器​​中的下列類時遇到困難。只有當我將參數類型從String更改爲對象類型時,才能看到此類獲得註冊。我究竟做錯了什麼?結構圖不會在構造函數中使用字符串參數註冊類

public class SomeCommand : ICommand 
{ 

    public SomeCommand(String path) 
    { 
     this.Path = path; 
    } 
    public string Path { get; private set; } 


    public Guid CommandId 
    { 
     get { return null; } 
    } 
} 

public class ObjectsRegistry : StructureMap.Configuration.DSL.Registry 
{ 
    public ObjectsRegistry() 
    { 
     Scan 
     (
      (scanner) => 
      { 
       scanner.TheCallingAssembly(); 
       scanner.Assembly(Assembly.GetExecutingAssembly()); 
       scanner.WithDefaultConventions(); 
       scanner.RegisterConcreteTypesAgainstTheFirstInterface(); 
       scanner.AddAllTypesOf(typeof(ICommand)); 
      } 
     ); 
    }  
} 

回答

1

StructureMap不能自動解析原始數據類型。

如果你知道在註冊時的值,你可以。如果您使用的是appSetting

ObjectFactory.Initialize(x => 
{ 
    x.For<ICommand>() 
     .Use<SomeCommand>() 
     .Ctor<string>("path") 
     .EqualToAppSetting("key"); 
}); 

但是使用這個語法

ObjectFactory.Initialize(x => 
{ 
    x.For<ICommand>() 
     .Use<SomeCommand>() 
     .Ctor<string>("path") 
     .Is(""); 
}); 

,如果你想爲每個實例不同pathICommand那麼StructureMap不能爲你創建這些實例。例如,您可以定義一個ICommandBuilder抽象,用於創建ICommand的實例或使Path成爲可設置的屬性。

相關問題