0
votes

I'm thinking of defining a facade for part of an application which is returned by an IoC container, in my case StructureMap. The facade has a constructor with no arguments that sets a default implementations, and another one so I can define custom implementations of the different subsystem objects the facade manage.

However, since StructureMap is trying to make an instance of the facade calling the constructor with the most arguments, it fails if no implementation of the other subsystems is registered.

My question is: Is there any way of telling StructureMap to try use the following constructor in line if it cannot use the main one? Or must I tell StructureMap which constructor to select?

2
Maybe a bit of sample code will help with some visualization. - IAbstract
If possible, always have a single constructor with arguments. Don't make a default constructor that initializes the type. This way your type is still tightly coupled with concrete dependencies, which makes it hard to swap. Let StructureMap do its job. - Steven
Having a single constructor with arguments, although still being handful, would mostly beat the purpose of having the facade in this case. About adding a bit of sample, I don't know if that is really needed, after all, my question doesn't require much of code. Having a class with multiple constructors, StructureMap will choose the first one with the greater number if arguments, what I want to know is if StructureMap can somehow use the other constructors as fallbacks in case of exception. - Neverbirth

2 Answers

1
votes

To my knowledge it's not possible to have StructureMap trying to resolve an instance and to fall back to alternate ctors if not successful with the greediest.

I think that it would be a good idea to wire up the facade's dependendencies using StructureMap as well and specify defaults there. Perhaps it's possible to use a Null object implementation as default for the subsystems that might be missing.

1
votes

You can specify a delegate that will create your type as follows:

container.Configure(r => r.For<IFacade>().Use(() => new RealFacade()));