framework design guidelines
DESCRIPTION
Framework Design GuidelinesTRANSCRIPT
![Page 1: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/1.jpg)
![Page 2: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/2.jpg)
Communication Artifacts
Properties Methods Events Constructors Exceptions
![Page 3: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/3.jpg)
Four Keys of Framework Design
![Page 4: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/4.jpg)
Outline
![Page 5: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/5.jpg)
Naming Conventions
![Page 6: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/6.jpg)
Saying It Load
![Page 7: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/7.jpg)
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;}}}
![Page 8: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/8.jpg)
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
![Page 9: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/9.jpg)
Member Design
![Page 10: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/10.jpg)
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
![Page 11: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/11.jpg)
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;}}}
![Page 12: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/12.jpg)
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
![Page 13: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/13.jpg)
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
![Page 14: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/14.jpg)
Extension Methods
![Page 15: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/15.jpg)
CONSIDER using extension methods to "add" methods to interfaces
![Page 16: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/16.jpg)
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){ … }
}
}
![Page 17: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/17.jpg)
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
![Page 18: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/18.jpg)
AVOID defining extension methods on System.Object
![Page 19: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/19.jpg)
Type Design
![Page 20: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/20.jpg)
Start
Run
![Page 21: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/21.jpg)
![Page 22: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/22.jpg)
22
![Page 23: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/23.jpg)
![Page 24: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/24.jpg)
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
![Page 25: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/25.jpg)
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
![Page 26: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/26.jpg)
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?
![Page 27: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/27.jpg)
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
![Page 28: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/28.jpg)
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);}
![Page 29: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/29.jpg)
![Page 30: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/30.jpg)
![Page 31: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/31.jpg)
Libraries , Primitives, Abstractions
![Page 32: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/32.jpg)
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!
![Page 33: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/33.jpg)
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
![Page 34: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/34.jpg)
Definition: Abstractions are interfaces or classes with
unsealed members that are passed between
components. Examples
Stream, IComponent Hard to Evolve Unfortunately, pressure to evolve
![Page 35: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/35.jpg)
Trends
![Page 36: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/36.jpg)
Test Driven Development
Write tests first, design later Requires reusable APIs to be
testable:
![Page 37: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/37.jpg)
Heavy Dependencies & Testability
![Page 38: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/38.jpg)
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);
}
}
![Page 39: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/39.jpg)
Dependency Injection
Check Containers like:Spring.NET, Castle Windsor ,
Structuremap, , Unity …
MEF session tomorrow will talk about it (isA)
![Page 40: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/40.jpg)
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
![Page 41: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/41.jpg)
41
The perilous summit of complexity by example
![Page 42: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/42.jpg)
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?
![Page 43: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/43.jpg)
43
VS7 Era “IO” seems like a reasonable place to start
“IO” seems like a reasonable place to start
![Page 44: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/44.jpg)
44
Why did they make it inaccessible
Backup, and try again…
Why did they make it inaccessible
Backup, and try again…
![Page 45: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/45.jpg)
45
Open.. Looks like a good first step…
Open.. Looks like a good first step…
![Page 46: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/46.jpg)
46
Hmm… OK, what do I do with a FileStream?
Hmm… OK, what do I do with a FileStream?
![Page 47: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/47.jpg)
47
Ok good, synchronous and asynchronous operations.. What the heck is that?
Ok good, synchronous and asynchronous operations.. What the heck is that?
![Page 48: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/48.jpg)
48
I think I am in the wrong place..
I think I am in the wrong place..
![Page 49: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/49.jpg)
49
Back up, let’s try a different type
![Page 50: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/50.jpg)
50
Ahh, ReadLine(), this looks more promising..
Ahh, ReadLine(), this looks more promising..
![Page 51: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/51.jpg)
51
OK, How do you find the end?
OK, How do you find the end?
![Page 52: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/52.jpg)
52
Thanks goodness there was a sample
Thanks goodness there was a sample
![Page 53: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/53.jpg)
53
The pit of success waydevelopers fall
into doing things the right way
![Page 54: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/54.jpg)
54
Just what I need…
Just what I need…
![Page 55: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/55.jpg)
55
Ah, a string[] I know just what to do with that…
Ah, a string[] I know just what to do with that…
![Page 56: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/56.jpg)
56
How simple!How simple!
![Page 57: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/57.jpg)
Tools
![Page 58: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/58.jpg)
Framework Design Tools
![Page 59: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/59.jpg)
![Page 60: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/60.jpg)
![Page 61: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/61.jpg)
![Page 62: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/62.jpg)
![Page 63: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/63.jpg)
![Page 64: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/64.jpg)
![Page 65: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/65.jpg)
65
Make the Make the simple simple things things simple simple and the and the
hard hard things things
possiblepossible
![Page 66: Framework Design Guidelines](https://reader031.vdocuments.us/reader031/viewer/2022013003/555a51a0d8b42ad56a8b4cb1/html5/thumbnails/66.jpg)
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