Information Technology Reference
In-Depth Information
PropertyChanging are no exception. But, it's an important enough fea-
ture that you should prefer supporting those interfaces in any class that
might be the object of data binding in your applications. It is common
enough that it's worth the extra work to create a general solution.
Item 44: Minimize Dynamic Objects in Public APIs
Dynamic objects just don't behave that well in a statically typed system.
The type system sees them as though they were instances of System.Object.
But they are special instances. You can ask them to do work above and
beyond what's defined in System.Object. The compiler generates code that
tries to find and execute whatever members you try to access.
But dynamic objects are pushy. Everything they touch becomes dynamic.
Perform an operation where any one of the parameters is dynamic, and the
result is dynamic. Return a dynamic object from a method, and every-
where that dynamic is used becomes a dynamic object. It's like watching
bread mold grow in a petri dish. Pretty soon, everything is dynamic, and
there's no type safety left anywhere.
Biologists grow cultures in petri dishes, restricting where they can grow.
Yo u n e e d t o d o t h e s a m e w i t h d y n a m i c : D o t h e w o r k w i t h d y n a m i c o b j e c t s
in an isolated environment and return objects that are statically typed as
something other than dynamic. Otherwise, dynamic becomes a bad influ-
ence, and slowly, everything involved in your application will be dynamic.
This is not to imply that dynamic is universally bad. Other items in this
chapter have shown you some of the techniques where dynamic pro-
gramming is an excellent solution. However, dynamic typing and static
typing are very different, with different practices, different idioms, and dif-
ferent strategies. Mixing the two without regard will lead to numerous
errors and inefficiencies. C# is a statically typed language, enabling
dynamic typing in some areas. Therefore, if you're using C#, you should
spend most of your time using static typing and minimize the scope of
the dynamic features. If you want to write programs that are dynamic
through and through, you should consider a language that is dynamic
rather than a static typed language.
If you're going to use dynamic features in your program, try to keep them
out of the public interface to your types. That way, you can use dynamic
typing in a single object (or type) petri dish without having them escape
 
 
Search WWH ::




Custom Search