Click here to Skip to main content
16,012,223 members
Home / Discussions / C#
   

C#

 
Question"economics" of casting to an interface in a parameter declaration of a method ? Pin
BillWoodruff6-Nov-11 20:41
professionalBillWoodruff6-Nov-11 20:41 
AnswerRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
harold aptroot6-Nov-11 21:37
harold aptroot6-Nov-11 21:37 
AnswerRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
Luc Pattyn6-Nov-11 22:56
sitebuilderLuc Pattyn6-Nov-11 22:56 
AnswerRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
PIEBALDconsult7-Nov-11 1:49
mvePIEBALDconsult7-Nov-11 1:49 
AnswerRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM697-Nov-11 11:11
professionalDaveyM697-Nov-11 11:11 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
BillWoodruff7-Nov-11 19:03
professionalBillWoodruff7-Nov-11 19:03 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM697-Nov-11 21:36
professionalDaveyM697-Nov-11 21:36 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
Pete O'Hanlon7-Nov-11 23:19
mvePete O'Hanlon7-Nov-11 23:19 
The issue you are describing is one of the classic issues surrounding IoC, and whether or not to use constructor injection to manage dependencies. At first, this seems to be an attractive option because it is a quick and easy way to add dependencies between objects. What BillWoodruff is effectively describing is the foundation of an IoC solution whereby you use interfaces to allow you to decouple the implementation from the consumption of the functionality. This is a classic situation especially when combined with Test Driven Development whereby you can easily inject mock functionality.

So, how about constructor injection? Why wouldn't you want to use it? Well, in the sample that Dave shows here, there's nothing inherently wrong with passing the interface into the constructor. What happens, though, when you want to add another interface that your class needs to consume? Well if you are using constructor injection, you need to either add another constructor or you need to modify the existing one and all the places that it's called in the code. The more complex a system becomes, the bigger this problem will get.

Now, one way to tackle this is to use property injection instead. In other words, your class adds properties that have the appropriate type and anytime the functionality is needed, the property is used. This is a simple and effective way to get around the scenario described above. But does this solve the question posed by Dave? Well, yes and no.

If you simply need a means to communicate between one object and another, then passing a concrete instance or an interface around is sheer overkill. Whenever I see somebody recommend this, it makes the very core of me want to reach out and slap them repeatedly. So, why do I think this? Well, have a think about what would happen if you started to play around with the values of that object inside your consuming object. Now think about what happens if you start to do this inside threaded code. Now think about the fact that all you really needed to do was let the original object know that it needs to update the text in the UI thread. In this type of situation, the much simpler and safer method is to simply have an event that is subscribed to, and let that raise the changes.

Anyhoo - those are just some rambling musings designed to get people thinking. Hope it helps.

Forgive your enemies - it messes with their heads

"Mind bleach! Send me mind bleach!" - Nagy Vilmos


My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM698-Nov-11 6:45
professionalDaveyM698-Nov-11 6:45 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
BobJanova8-Nov-11 0:14
BobJanova8-Nov-11 0:14 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM698-Nov-11 6:57
professionalDaveyM698-Nov-11 6:57 
AnswerNot so simple (Re: "economics" of casting to an interface in a parameter declaration of a method?) Pin
Sergey Alexandrovich Kryukov8-Nov-11 14:58
mvaSergey Alexandrovich Kryukov8-Nov-11 14:58 
QuestionOmar Gameel, Apriori Algorithm Please Help..!!! Pin
Member 78679836-Nov-11 11:29
Member 78679836-Nov-11 11:29 
GeneralRe: Omar Gameel, Apriori Algorithm Please Help..!!! Pin
harold aptroot6-Nov-11 11:41
harold aptroot6-Nov-11 11:41 
AnswerRe: Omar Gameel, Apriori Algorithm Please Help..!!! Pin
Dalek Dave6-Nov-11 12:54
professionalDalek Dave6-Nov-11 12:54 
Questionauto check DataGridViewCheckBoxColumn Pin
Danzy836-Nov-11 10:52
Danzy836-Nov-11 10:52 
AnswerRe: auto check DataGridViewCheckBoxColumn Pin
Dalek Dave6-Nov-11 12:57
professionalDalek Dave6-Nov-11 12:57 
AnswerRe: auto check DataGridViewCheckBoxColumn Pin
Dr.Walt Fair, PE6-Nov-11 16:59
professionalDr.Walt Fair, PE6-Nov-11 16:59 
AnswerRe: auto check DataGridViewCheckBoxColumn Pin
purnananda behera7-Nov-11 19:00
purnananda behera7-Nov-11 19:00 
QuestionHow to read an Autocad file with c# Pin
JUSTLOVE16-Nov-11 10:04
JUSTLOVE16-Nov-11 10:04 
AnswerRe: How to read an Autocad file with c# Pin
Richard MacCutchan6-Nov-11 10:19
mveRichard MacCutchan6-Nov-11 10:19 
GeneralRe: How to read an Autocad file with c# Pin
Dalek Dave6-Nov-11 12:57
professionalDalek Dave6-Nov-11 12:57 
GeneralRe: How to read an Autocad file with c# Pin
Richard MacCutchan6-Nov-11 22:28
mveRichard MacCutchan6-Nov-11 22:28 
AnswerRe: How to read an Autocad file with c# Pin
Luc Pattyn6-Nov-11 22:58
sitebuilderLuc Pattyn6-Nov-11 22:58 
GeneralRe: How to read an Autocad file with c# Pin
Richard MacCutchan6-Nov-11 23:27
mveRichard MacCutchan6-Nov-11 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.