2009-11-22 41 views
0

我試圖開始使用IoC,我有一個MVC項目中使用亞音速,我試圖注入亞音速simplerepository到我的控制器,但我得到這個錯誤: StructureMap異常代碼:205 缺少請求實例屬性 「的connectionStringName」 爲InstanceKey 「60b735fb-0a7f-4eb4-be04-635f6f32233d」亞音速SimpleRepository類注入控制器

這裏是我的註冊表類:

public class RepositoryRegistry : Registry 
{ 
    protected override void configure() 
    { 
     ForRequestedType<IRepository>().TheDefault.Is.OfConcreteType(typeof(SimpleRepository)); 

    } 
} 

這裏是我的控制器廠:

public class StoreControllerFactory: DefaultControllerFactory 
{ 
    protected override IController GetControllerInstance(Type controllerType) 
    { 
     IController result = null; 
     if (controllerType!=null) 
     { 
      result = ObjectFactory.GetInstance(controllerType) as Controller; 

     } 
     return result; 
    } 
} 

這就是我如何配置StructureMap:

 protected void Application_Start() 
    { 
     RegisterRoutes(RouteTable.Routes); 

     ObjectFactory.Initialize(x=> 
            { 
             x.AddRegistry(new RepositoryRegistry()); 
            }); 
     ControllerBuilder.Current.SetControllerFactory(new StoreControllerFactory()); 


     var sparkSettings = new SparkSettings().SetDebug(true).AddNamespace("System.Web.Mvc.Html"); 
     ViewEngines.Engines.Clear(); 
     ViewEngines.Engines.Add(new SparkViewFactory(sparkSettings)); 
    } 

任何幫助,將不勝感激!謝謝!

+0

你能爲你的SimpleRepository添加代碼?我認爲這個類有什麼問題(與它的名爲connectionStringName的構造函數參數完全相同)。 – 2009-11-26 13:06:53

回答

0

的問題是,StructureMap使用構造最參數是,我固定的:

ObjectFactory.Configure(
      x => { x.SelectConstructor<SimpleRepository>(() => new SimpleRepository()); });