1

I have a situation where i am currently using constructor dependency injection for a small module that fits inside a larger web framework.

Its working fine but now there is a new class being introduced that requires 2 objects passed to it. But, 1 of the objects requires a lot of work to get set up - essentially it invovles around 4 method calls which create other objects in order to get it into a working state ready to be passed to my object.

My dilemna is that constructor injection is no use due to the work involved, but introducing a ioc container is way overboard, especially for this 1 off use case.

So, how should this be handled? Is there some sort of solution that sits in the middle of these two options?

asked Nov 10, 2013 at 0:36
1

3 Answers 3

2

You've effectively got (削除) four (削除ここまで) five choices:

  • Poor Man's DI (create objects manually and pass to constructors)
  • IoC container
  • Factory method
  • Abstract factory
  • Builder (thanks, Mark Seemann!)

I usually start off with an IoC container, but I do a lot of DI. (I've seen too many tightly-coupled codebases.)

If you don't want to introduce an IoC container, I'd lean towards Poor Man's DI.

If you're working in any object-oriented language (not just C#), I recommend reading the book Dependency Injection in .NET. It covers the patterns and anti-patterns in detail.

answered Nov 10, 2013 at 1:57
Sign up to request clarification or add additional context in comments.

Comments

1

1 of the objects requires a lot of work to get set up - essentially it invovles around 4 method calls which create other objects in order to get it into a working state ready to be passed to my object.

OK, then create the object and pass the completely initialized object to the constructor, in which it needs to go.

Creating that object sounds like a job for a Builder or Factory.

answered Nov 10, 2013 at 13:43

Comments

1

My dilemna is that constructor injection is no use due to the work involved,

I prefer Constructor Injection and see no reasons to avoid it.

Using modern IoC frameworks you can specify creation logic that involves "a lot of work to get set up" via factory/ factory method.

No matter how many steps are needed to build an instance of IMyService, you can simply use a constructor dependency to inject it.

Castle Windsor

container.AddFacility<FactorySupportFacility>()
 .Register(
 Component.For<IMyFactory>().ImplementedBy<MyFactory>(),
 Component.For<IMyService>()
 .UsingFactoryMethod(k => k.Resolve<IMyFactory>().Create())
 );

Unity

var container = new UnityContainer();
container.RegisterType<IMyFactory, MyFactory>();
container.RegisterType<IMyService>(
 new InjectionFactory(c => c.Resolve<IMyFactory>().Create()));
answered Nov 10, 2013 at 15:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.