Framework Design Guidelines
-
Upload
mohamed-meligy -
Category
Technology
-
view
3.106 -
download
3
description
Transcript of Framework Design Guidelines
Communication Artifacts
Properties Methods Events Constructors Exceptions
Four Keys of Framework Design
Outline
Naming Conventions
Saying It Load
Hungarian Notation
Do not use Hungarian notation in publicly exposed APIs and parameter names
public class CMyClass {public class CMyClass { int CompareTo (object objValue) {..} int CompareTo (object objValue) {..} string lpstrName {get;} string lpstrName {get;} int iValue {get;} int iValue {get;}}}
On Abbreviations, acronym, initialism and the like…
Avoid them! They are a classic JLT (jargon loaded term) OK to use them once they become words
Html, Xaml, etc
Don’t just spell them out Use a meaningful name
Abbreviations of more than 2 letters are cased as words, otherwise ALLUPPER IO vs. Html
Member Design
10
Constructors are
Do minimal work in the constructor Be Lazy! Only capture the parameters
public class XmlFile {public class XmlFile { string filename; string filename; Stream data; Stream data; public XmlFile(string filename) { public XmlFile(string filename) { this.data = DownloadData(filename); this.data = DownloadData(filename); }}}}
lazylazy
Properties
Property getters should be simple and therefore unlikely to throw exceptions
Properties should not have dependencies on each other Setting one property should not affect other
properties Properties should be settable in any
order
public class ArrayList {public class ArrayList { public int Count {get;} public int Count {get;}}}
Properties versus Methods
Use a Property: If the member logical attribute of the
type Use a method:
If the operation is a conversion, such as ToString()
If the getter has an observable side effect
If order of execution is important If the method might not return
immediately If the member returns an array
EmployeeList l = FillList();EmployeeList l = FillList();for (int i = 0; i < l.Length; i++){for (int i = 0; i < l.Length; i++){ if (l.All[i] == x){...}if (l.All[i] == x){...}}}
public Employee[] All {get{}}public Employee[] All {get{}}
Moral: Use method if the operation is expensive Moral: Use method if the operation is expensive
Calling CodeCalling Code
Properties and returning arrays
Extension Methods
CONSIDER using extension methods to "add" methods to interfaces
CONSIDER using extension methods to manage dependencies
Uri uri = “ftp://some.ftp.uri”.ToUri();
// higher level assembly (not mscorlib)
namespace System.Net {
public static class StringExtensions{
public static Uri ToUri(this string s){ … }
}
}
AVOID frivolously defining extension methods, especially on types you don’t own
Might add clutter (Chaos) Choose namespaces for sponsor types
carefully Remember that not all languages
support extension methods Users will have to use static method call
syntax
AVOID defining extension methods on System.Object
Type Design
Start
Run
22
Abstract and Base classes Prefer broad, shallow hierarchies
Less than or equal to 2 additional levels
Contracts and responsibilities are difficult to maintain and explain in deep complex hierarchies
Use abstract classes Make it clear what the class is for Provide a protected constructor for
subclasses to call
Virtual Methods
Method call virtualizes at runtime The static type doesn’t matter This is the danger and power of
virtual methods Danger: Owner of base classes cannot
control what subclasses do Power: Base class does not have to
change as new subclasses are created
Overriding Methods
All Virtual members should define a contract Don’t change the semantics of member
Don’t require clients to have knowledge of your overriding
Should you call the base?
References to base types must work with derived types without knowing the difference
To Virtual Or Not Virtual … Use non-virtual members unless you have specifically designed for specialization Have a concrete scenario in mind Write the code! Follow the Liskov Substitution Principle
Must continue to call in the same order and frequency
Cannot increase or decrease range of inputs or output
Barbara Liskov
Interfaces
No common implementation Challenging to version over releases The smaller, more focused the interface
the better 1-2 members are best But interfaces can be defined in terms
of other simpler interfaces
public interface IComparable { int CompareTo(object obj);}
Libraries , Primitives, Abstractions
CONSIDER placing library types higher on the dependency stack Definition:
Library types are types that are not passed between components
Examples EventLog, Debug,
Easy to Evolve Leave old in, add new one Beware of duplication!
DO keep primitives policy free (i.e. simple) Definition:
Primitive types are types that are passed between components and have very restricted extensibility (i.e. no subtype can override any members)
Examples Int32, String, Uri.
Hard to Evolve Little need to Evolve Typically in lower layers
Definition: Abstractions are interfaces or classes with
unsealed members that are passed between
components. Examples
Stream, IComponent Hard to Evolve Unfortunately, pressure to evolve
Trends
Test Driven Development
Write tests first, design later Requires reusable APIs to be
testable:
Heavy Dependencies & Testability
Inversion of Control// your better API
public abstract class TraceListener {
public abstract void Trace(string message);
}
public class Tracer {
TraceListener listener;
public Tracer(TraceListener listener){
this.listener = listener;
}
public void Trace(string message){
listener.Trace(message);
}
}
Dependency Injection
Check Containers like:Spring.NET, Castle Windsor ,
Structuremap, , Unity …
MEF session tomorrow will talk about it (isA)
The Power of Sameness
Influence of expectations Naming conventions and common suffixes\
prefixes Habits win out over the special cases
Common Exception pattern With great power comes great
responsibility Method overloading
Meet developers where they are constructors and properties pattern teaches
us to meet developers’ expectations
41
The perilous summit of complexity by example
42
How do I read How do I read all the lines all the lines from a file?from a file?
How do I read How do I read all the lines all the lines from a file?from a file?
43
VS7 Era “IO” seems like a reasonable place to start
“IO” seems like a reasonable place to start
44
Why did they make it inaccessible
Backup, and try again…
Why did they make it inaccessible
Backup, and try again…
45
Open.. Looks like a good first step…
Open.. Looks like a good first step…
46
Hmm… OK, what do I do with a FileStream?
Hmm… OK, what do I do with a FileStream?
47
Ok good, synchronous and asynchronous operations.. What the heck is that?
Ok good, synchronous and asynchronous operations.. What the heck is that?
48
I think I am in the wrong place..
I think I am in the wrong place..
49
Back up, let’s try a different type
50
Ahh, ReadLine(), this looks more promising..
Ahh, ReadLine(), this looks more promising..
51
OK, How do you find the end?
OK, How do you find the end?
52
Thanks goodness there was a sample
Thanks goodness there was a sample
53
The pit of success waydevelopers fall
into doing things the right way
54
Just what I need…
Just what I need…
55
Ah, a string[] I know just what to do with that…
Ah, a string[] I know just what to do with that…
56
How simple!How simple!
Tools
Framework Design Tools
65
Make the Make the simple simple things things simple simple and the and the
hard hard things things
possiblepossible
References http://blogs.msdn.com/brada (Thanks)
http://channel9.msdn.com/pdc2008/PC58 http://tinyurl.com/brad-guidelines
http://blogs.msdn.com/kcwalina Krzysztof Cwalina
http://tinyurl.com/msdn-guidelines Design Guidelines for Developing Class Libraries
[Off-topic] http://tinyurl.com/patterns-enterprise
Patterns of Enterprise Application Architecture [SOLID] http://derickbailey.com
Derick Bailey