Friday, October 9, 2015

the [DefaultConstructor] attribute for StructureMap

...will allow you to specify the constructor for a controller which StructureMap tries to hydrate with dependencies. Without this specification StructureMap will just pick the constructor with the most parameters, which may not be desired. This attribute goes at the constructor. A different, hacky way to approach this to put something like this in AssemblyInfo.cs in the Properties folder of a .NET project:

[assembly: InternalsVisibleTo("MyTests")]

 
 

...to expose the UI project's internal constructors to other projects (in this example they would be exposed to a test project) as needed. Admittedly, this may not be needed at all depending on what is dependent on what. Anyhow, you would, in this hack, slap internal on controller constructors that are longer in terms of number of parameters than the constructor you want to serve as a StructureMap default (which would get the public accessibility). My superior suggests the conditional open up of internal is the closest thing C# has to the concept of a friend in C++.

No comments:

Post a Comment