第一次尝试StructureMap和MVC3通过的NuGet [英] First try StructureMap and MVC3 via NuGet

查看:137
本文介绍了第一次尝试StructureMap和MVC3通过的NuGet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何我已经使用的NuGet到config StructureMap为ASP.NET MVC3,我注意到,它创建App_Start文件夹命名为StructuremapMVC一个CS文件,所以我检查一下,注意是一样的code,但简化的将手动写上放置在Global.asax中...

App_Start节

这是从国际奥委会类

我的code

 公共静态类的IoC
    {
        公共静态初始化的IContainer()
        {
            ObjectFactory.Initialize(X =>
                        {
                            x.Scan(扫描=>
                                    {
                                        scan.TheCallingAssembly();
                                        scan.WithDefaultConventions();
                                        scan.AddAllTypesOf<一个IController>();
                                    });
                            x.For< OpcionDB>()使用。(()=方式>新DatabaseFactory()获取());
                        });
            返回ObjectFactory.Container;
        }
    }

我的问题是,为什么我得到一个例外,当我在我的控制器为后续的(我用这种模式注入一些IOC:<一href=\"http://stackoverflow.com/questions/4442828/entity-framework-4-ctp-4-ctp-5-generic-repository-pattern-and-unit-testable/4458250#4458250\">Entity Framework 4的CTP 4/5 CTP通用存储库模式和单元测试):

 私人只读IAsambleaRepository _aRep;
        私人只读IUnitOfWork _uOw;        公共AsambleaController(IAsambleaRepository AREP,IUnitOfWork UOW)
        {
            _aRep = AREP;
            this._uOw = UOW;
        }        公众的ActionResult列表(串期)
        {
            VAR RS = _aRep.ByPeriodo(期).ToList&LT; Asamblea&GT;();            返回查看();
        }

异常表现:

 异常详细信息:system.missingMethodException而:此对象定义无参数的构造函数。


解决方案

您获得的是错误,因为你没有设置StructureMap解决去构造所需的依赖关系的 AsambleaController 所以它试图找到一个参数的构造函数其中有一个也没有。

所以,你需要做的是 IAsambleaRepository 设置StructureMap和 IUnitOfWork

在一个侧面说明,我会说, IUnitOfWork 应该是你的资料库的依赖,而不是你的控制器......你的控制器不应该需要知道的单位的工作。

I'm trying to figure how to config StructureMap for ASP.NET MVC3 I've already using NuGet and I notice that it creates App_Start Folder with a cs file named as StructuremapMVC, so I check it and notice that is the same code but simplified that will be written manually on App_Start section placed on Global.asax...

This is my code from IoC Class

public static class IoC
    {
        public static IContainer Initialize()
        {
            ObjectFactory.Initialize(x =>
                        {
                            x.Scan(scan =>
                                    {
                                        scan.TheCallingAssembly();
                                        scan.WithDefaultConventions();
                                        scan.AddAllTypesOf<IController>();
                                    });
                            x.For<OpcionDB>().Use(() => new DatabaseFactory().Get());
                        });
            return ObjectFactory.Container;
        }
    }

My Question is Why I get an Exception when I inject some IoC on my Controllers as the follow (I use this pattern : Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable) :

        private readonly IAsambleaRepository _aRep;
        private readonly IUnitOfWork _uOw;

        public AsambleaController(IAsambleaRepository aRep, IUnitOfWork uOw)
        {
            _aRep = aRep;
            this._uOw = uOw;
        }

        public ActionResult List(string period)
        {
            var rs = _aRep.ByPeriodo(period).ToList<Asamblea>();

            return View();
        }

Exception showed :

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

解决方案

You are getting that error because you haven't setup StructureMap to resolve the dependencies needed to contruct the AsambleaController so it tries to find a parameterless constructor which there isn't one.

So what you need to do is setup StructureMap for IAsambleaRepository and IUnitOfWork.

On a side note, I'd say that IUnitOfWork should be a dependency on your repository and not your controller... your controller shouldn't need to know about the unit of work.

这篇关于第一次尝试StructureMap和MVC3通过的NuGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆