将Subsonic SimpleRepository类注入控制器

时间:2009-11-22 20:20:54

标签: inversion-of-control subsonic3 structuremap

我正在尝试开始使用IoC,我有一个使用亚音速的MVC项目,我正在尝试向我的控制器注入亚音速简单存储库,但我收到此错误: StructureMap例外代码:205 缺少InstanceKey的请求实例属性“connectionStringName”“60b735fb-0a7f-4eb4-be04-635f6f32233d”

这是我的注册表类:

    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));
    }

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

问题是StructureMap使用的构造函数参数最多,我修复了这个:

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