Mar 13, 2012 at 7:43 PM
Edited Mar 13, 2012 at 7:44 PM
In our team we are making extensive use of Unity (and the EntLib). We only use constructor injection and
no attributes. This means that some classes may end up with a lot of arguments in its constructor. As a general rule, we always check for not null like this:
public DesbalanceZonaRxForm(Func<LogSpacForm> logSpacForm, GridExtensionManager gridManager,
Func<DownloadSpacForm> downloadSpacForm,Func<UploadSpacFileForm> upload, DesbalanceZonaRecepcion desbalanceZonaRecepcion,
ISpacService spacService, IProcesarEDI procesarEdi, IMainForm mainForm)
if (procesarEdi == null) throw new ArgumentNullException("procesarEdi");
if (logSpacForm == null) throw new ArgumentNullException("logSpacForm");
if (upload == null) throw new ArgumentNullException("upload");
if (desbalanceZonaRecepcion == null) throw new ArgumentNullException("desbalanceZonaRecepcion");
if (spacService == null) throw new ArgumentNullException("spacService");
if (mainForm == null) throw new ArgumentNullException("IMainForm");
if (downloadSpacForm == null) throw new ArgumentNullException("downloadSpacForm");
this.procesarEdi = procesarEdi;
this.UploadSpacFileFormFactory = upload;
this.logSpacForm = logSpacForm;
this.desbalanceZonaRecepcion = desbalanceZonaRecepcion;
this.spacService = spacService;
this.mainForm = mainForm;
this.downloadSpacFormFactory = downloadSpacForm;
This piece of code is being repeated in all of our classes. I am looking for a better way to implement this.
I have lookup into Interception but have not found any way to implement it.
I think this is happening to a lot of developers, so a unified solution would be great!!!
Unity does not support interception on constructors so you would not be able to implement any automatic constructor validation using Unity. You could look at
code contracts but it is not really centralizing this code but does provide other benefits.
You still would need a way to determine what parameters to validate and what the validations are. The validation application block lets you do this using a configuration file so you could leverage that have Unity inject a validator into the object
to perform the parameter guarding. This is off the top of my head so I'm not sure if it's a good idea (e.g. you could run into trouble in complicated constructor chaining scenarios).
Enterprise Library support engineer
I would suggest that if you have one constructor with that many parameters, your design is probably wrong - you've got too many responsibilities in one spot. There's opportunities to wrap up some of those together into higher level services. Or to split
things out so that the one class isn't using all those dependencies directly.
That thing must be hell to unit test with all those constructor parameters!
As for the null checks, Randy is right, Unity interception can't intercept constructor calls as is, nor would I recommend doing so. Actually, unless I need to get FxCop to shut up I generally don't bother checking for null, I just wait for the null pointer
reference error later down the chain. But that's more a dev style thing.
I follow the First Fail principle http://en.wikipedia.org/wiki/Fail-fast.
I have done a fast search on othor IoC and they do not implement this feature.
I think i will have to stinck with what i have until now
Thanks a lot for aswering, this means a lot for me.