2009-11-22 68 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()); });