4

I am looking for suggestions on good ways to design a new ASP.NET Mvc project. 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.

I am having a bit of trouble figuring out a good way to set this up without 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.

Any suggestions?

11
  • 9
    I'm curious as to why management has dictated that you cannot use DI? Is it normal for management to make technical decisions of this nature for developers? If so... run. Commented Oct 4, 2011 at 19:41
  • It's a good question and i'm not sure really, some of the other developers here aren't really familiar with the concept and don't want to give it a try so management has sided with them for now. Commented Oct 4, 2011 at 19:46
  • 1
    Would the same restrictions apply to using the Managed Extensibility Framework (mef.codeplex.com), since it is part of .Net? Commented Oct 4, 2011 at 20:01
  • 1
    Using a dependency injection framework like ninject makes DI trivial. Maybe you should revisit the topic with the group and upper management? Commented Oct 4, 2011 at 20:10
  • 1
    @zaq: In that case, follow the person leading the team, and don't force a team conflict. However, whenever you can, design and write the code you make according to your own (highest) standard. In other words, with dependency injection in mind (i.e. use poor mans DI), and support your code with unit tests. This way you will do your part to writing good software. In other words, lead by example. When the time comes, carefully motivate your team mates and the team lead to look deeper into DI. I agree with the team lead that DI feels complicated (at first), but it is so damn well worth the effort. Commented Oct 5, 2011 at 8:18

5 Answers 5

5

Since you are using ASP.NET MVC 3 you could write a custom dependency resolver. 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. You know, the kind of things that object containers and DI frameworks already do for you. 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.

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:

  • 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.

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. Management won't even notice as they don't seem to understand the technical aspects of an application by imposing such requirements.

Sign up to request clarification or add additional context in comments.

5 Comments

I agree with your points and I tried to explain these issues (maybe I did a bad job explaining it) but there is a fear that the time required for the other developers to pickup the concepts will not be worth it and that it may be difficult to hire new developers that are familiar with a particular framework.
@zaq, and you think that the time you will waste into reinventing the wheels by rolling a custom DI framework will be worth it? If the developers cannot grasp the concept of an existing DI framework by reading the millions of tutorials out there, grasping the concept of a custom built DI framework with custom dependency resolvers will be far more important, believe me.
Oh, I couldn't agree more but it's not me that needs to be convinced unfortunately.
I was just hoping someone might have an idea on how to accomplish this without creating new service objects directly in the controllers, or declaring factory/singleton methods in all of the services (Without IOC).
@zaq, in my answer I suggested you using inversion of control and have your controller's constructors only take an interface. The actual instantiation of the concrete type will be done in your custom dependency resolver. You will never be creating any new service objects directly in your controllers. Your controllers will look exactly the same as if you were using a DI framework.
4

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.

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; this interface was designed to be used with an IoC container though really and would be a poorer substitute on its own.

1 Comment

I looked into this and started working with it but it seems that I'll just end up writing my own semi DI container, which is a bad idea. I'm going to go with your suggestion of a "poor mans DI" from your comment on my original post. It's still not a very good solution but I think it's the only option given my constraints. Thanks for your help.
0

Does your boss have pointy hair? http://www.dilbert.com/

You could save yourself some time by using http://unitymvc3.codeplex.com/ rather than writing your own custom dependency resolver. It's downloadable via 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. That's assuming your manager believes in unit testing ;-) Good luck!

3 Comments

It looks pretty good, but unfortunately I already suggested Unity as a Microsoft alternative to Spring.
It's very simple to set up and to use (it's specifically for using Unity with MVC3), doesn't require any special configuration, and the learning curve would be minimal for your team members. Good luck though, sounds like you might need it ;-)
Thanks Tony, maybe I'll look at this and try again.
0

management has told me that Spring.Net or any other IOC container is not to be use in our environment.

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.

Leave that company now you can!

Your other option is using the source code of the 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). This way it’s your own local DI container, but fully tested :-)

Good luck.

Comments

0

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, i.e. the dumb way, with no separation of concerns. When version 1 of your project is finish and passes QA (if you have any QA), your boss will probably think he is vindicated. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.