简体   繁体   English

ASP.Net MVC控制器注入

[英]ASP.Net MVC Controller injection

I am looking for suggestions on good ways to design a new ASP.NET Mvc project. 我正在寻找有关设计新的ASP.NET Mvc项目的好方法的建议。 It is of medium size and is relatively simple in structure. 它中等大小,结构相对简单。 I had initially used Spring.Net to wire my service objects into the correct constructors but now management has told me that Spring.Net or any other IOC container is not to be use in our environment. 我最初使用Spring.Net将我的服务对象连接到正确的构造函数,但现在管理层告诉我,Spring.Net或任何其他IOC容器不能在我们的环境中使用。

I am having a bit of trouble figuring out a good way to set this up without DI. 在没有DI的情况下找出一个很好的方法来解决这个问题我有点麻烦。 I would like to be able to assign service implementations to controllers in a way that allows for a low amount of coupling between controllers and services and to limit, as much as possible, the dependably of controllers on individual service implementations. 我希望能够以允许控制器和服务之间的低量耦合的方式将服务实现分配给控制器,并尽可能地限制各个服务实现上的控制器。 I guess my question boils down to that fact that I am unsure of where I should manually wire up my application in the MVC model. 我想我的问题归结为这样一个事实,我不确定我应该在MVC模型中手动连接我的应用程序。

Any suggestions? 有什么建议么?

Since you are using ASP.NET MVC 3 you could write a custom dependency resolver . 由于您使用的是ASP.NET MVC 3,因此可以编写自定义依赖项解析程序 You will of course still design your controllers taking interfaces in their constructors in order to weaken the coupling between the layers. 当然,您仍然会设计控制器在其构造函数中使用接口,以减弱层之间的耦合。 And then in the custom dependency resolver, in order to satisfy the ridiculous requirement that was imposed to you, you will have to manually say that when you have an ISomeService you return an instance of SomeServiceImpl . 然后在自定义依赖项解析器中,为了满足强加给您的荒谬要求,您必须手动说当您有ISomeService时返回SomeServiceImpl的实例。 You know, the kind of things that object containers and DI frameworks already do for you. 你知道,那些反对容器和DI框架的东西已经为你做了。 So you will basically have to reinvent some wheels. 所以你基本上必须重新发明一些轮子。 By the way Ayende blogged about how you could build a custom IoC container in 15 lines of code but of course that's not a reason that you should ever do anything like this. 顺便说一句,Ayende在博客中写了关于如何用15行代码构建自定义IoC容器的方式,但当然这不是你应该做这样的事情的原因。

People imposing such requirements should face a trial and be sentenced to never ever have to approach an application design. 强加这种要求的人应该面临审判并被判处永远不必接近申请设计。 Imposing such requirement illustrates some total lack of knowledge about good practices in designing applications. 强加这样的要求表明在设计应用程序方面缺乏对良好实践的了解。 Those people should be advised before they bring further damage to the company. 在给公司带来进一步的损害之前,应该告知这些人。

So simply explain those people that by reinventing wheels there are 2 mistakes: 所以简单地解释那些通过重新发明轮子有2个错误的人:

  • you will waste a lot of time for something that was already done by someone else 你会浪费很多时间去做别人已经完成的事情
  • you will make errors as you will not take into considerations all the edge cases that were taken into account by someone else designing a reusable DI framework. 你会犯错误,因为你不会考虑设计可重复使用的DI框架的其他人考虑的所有边缘情况。

At the end of the day you will ship your application late on schedule (as you would have wasted time to write plumbing code) and even worse you will ship an application that will contain potential bugs. 在一天结束时,您将按计划延迟发送应用程序(因为您将浪费时间编写管道代码),更糟糕的是,您将发布一个包含潜在错误的应用程序。

Conclusion: expensive and buggy product. 结论:昂贵且有缺陷的产品。 Your management must have really has lost its mind :-) 你的管理层肯定已经失去了理智:-)

Conclusion2: use an existing DI framework. 结论2:使用现有的DI框架。 Management won't even notice as they don't seem to understand the technical aspects of an application by imposing such requirements. 管理层甚至不会注意到,因为他们似乎没有通过强加这些要求来理解申请的技术方面。

First of all, I would question why management has mandated that you can't use a pattern and tools that would allow you to achieve loose coupling and dependency injection. 首先,我会质疑为什么管理层要求您不能使用允许您实现松散耦合和依赖注入的模式和工具。 Is this something that can be discussed and reasoned about? 这是可以讨论和推理的东西吗?

With an IoC container, it is trivial to implement an IControllerFactory that resolves controllers from the container and injects the necessary services. 使用IoC容器,实现一个IControllerFactory来解决容器中的控制器并注入必要的服务是微不足道的。

In MVC 3, there is IDependencyResolver which you could use to achieve slightly looser coupling (via a Service Locator pattern/anti-pattern) than instantiating services directly inside controllers; 在MVC 3中,有一个IDependencyResolver可以用来实现稍微松散的耦合(通过服务定位器模式/反模式),而不是直接在控制器内实例化服务; this interface was designed to be used with an IoC container though really and would be a poorer substitute on its own. 这个界面设计用于IoC容器,但实际上它本身就是一个较差的替代品。

Does your boss have pointy hair? 你的老板有尖尖的头发吗? http://www.dilbert.com/ http://www.dilbert.com/

You could save yourself some time by using http://unitymvc3.codeplex.com/ rather than writing your own custom dependency resolver. 使用http://unitymvc3.codeplex.com/可以节省一些时间,而不是编写自己的自定义依赖项解析器。 It's downloadable via Nuget http://nuget.org/ . 它可以通过Nuget http://nuget.org/下载。 If you use an IOC container such as this, and use constructor injection, you will find your unit tests are much easier to write. 如果你使用像这样的IOC容器,并使用构造函数注入,你会发现你的单元测试更容易编写。 That's assuming your manager believes in unit testing ;-) Good luck! 假设你的经理相信单元测试;-)祝你好运!

management has told me that Spring.Net or any other IOC container is not to be use in our environment. 管理层告诉我,Spring.Net或任何其他IOC容器不能在我们的环境中使用。

Management is telling you that you are not allowed to write loose coupled, testable and higly maintainable applications, that's what they are telling you. 管理层告诉您,您不能编写松散耦合,可测试和高度可维护的应用程序,这就是他们告诉您的内容。 That's an odd restriction. 这是一个奇怪的限制。

Dependency injection is an advanced technology that many developers don't understand. 依赖注入是许多开发人员不理解的高级技术。 However, management will never understand it, and it is not their job to understand it. 但是,管理层永远不会理解它,理解它并不是他们的工作。 They may dictate the type of technology (.NET vs. Java for instance), because this impacts the kinds of personnel they need to hire, but when they start dictating on low level implementation details that prevent you from writing descent software, your company is heading for disaster. 他们可能会决定技术的类型(例如.NET与Java),因为这会影响他们需要雇佣的人员类型,但当他们开始指示阻止您编写血统软件的低级实施细节时,您的公司就是前往灾难。

Leave that company now you can! 现在离开那家公司吧!

Your other option is using the source code of the Simple Injector . 您的另一个选择是使用Simple Injector源代码 The code base is small enough (about 500 lines, just use the SimpleInjector.NET project) to be able to copy it to a local project (and the license permits it). 代码库足够小(大约500行,只需使用SimpleInjector.NET项目)就可以将它复制到本地项目(并且许可证允许它)。 This way it's your own local DI container, but fully tested :-) 这样它就是你自己的本地DI容器,但经过全面测试:-)

Good luck. 祝好运。

You need a new home. 你需要一个新家。 There are plenty of organizations looking for talented engineers who care about quality and scalable architectures. 有很多组织正在寻找关注质量和可扩展架构的优秀工程师。 Make sure you find the right one and they will be glad to find you. 确保找到合适的人,他们很乐意找到你。

Avoid the temptation to save your current team from their own short-sightedness. 避免将现有团队从他们自己的短视中拯救出来的诱惑。 From your descriptions, it sounds like you are already a pariah despite your talents. 根据你的描述,听起来你已经是一个贱民,尽管你的才华。 Accept the fact that you won't be listened to. 接受你不会被倾听的事实。

The best strategy is to fake being a born-again team player. 最好的策略是假装成为重生的团队成员。 Build your MVC project exactly the way your boss has asked, ie the dumb way, with no separation of concerns. 按照老板提出的方式建立你的MVC项目,即愚蠢的方式,没有任何关注点的分离。 When version 1 of your project is finish and passes QA (if you have any QA), your boss will probably think he is vindicated. 当你的项目的第1版完成并通过QA(如果你有任何QA),你的老板可能会认为他是正确的。 Be prepared for this reaction. 为这个反应做好准备。

The best hope you have of enlightening your current team members is to show them that even if you build software using the same dumb practices they are comfortable with, you can still run rings around them. 您对现有团队成员的最大启发是向他们展示即使您使用他们熟悉的相同哑巴练习来构建软件,您仍然可以围绕它们运行环。 This can be fun. 这很有趣。 Then, when you leave, you give them a chance to reflect on the possibility that you were on to something. 然后,当你离开时,你给他们一个机会来思考你对某事的可能性。 They can either take that chance or opt for continued comfort, but it won't be your problem any longer. 他们既可以抓住机会,也可以选择继续保持舒适,但这不再是你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM