Framework Design Guidelines For Brussels Users Group

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Framework Design Guidelines For Brussels Users Group - Presentation Transcript

    1. 10 Years of Framework Design Guidelines
       Brad Abrams
      Product Unit Manager
      Microsoft Corporationhttp://blogs.msdn.com/brada
      Twitter: @brada
    2. Happy 10 Year Birthday
    3. Happy 10 Year Birthday
      JoyeuxAnniversaire
    4. Happy 10 Year Birthday
      Proficiat met je verjaardag
    5. Member Design
    6. 10 years ago….
    7. Communicate via leaving artifacts
      Framework Design Artifacts:
      • Properties
      • Methods
      • Events
      • Constructors
    8. Constructors are
      lazy
      public XmlFile(string filename) {this.filename = filename; }
      public class XmlFile { string filename; Stream data; public XmlFile(string filename) { this.data = DownloadData(filename);
      }
      }
      Do minimal work in the constructor
      Be Lazy!
      Only capture the parameters
      9
    9. Properties
      public class ArrayList { public int Count {get;}}
      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
    10. 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 observableside effect
      If order of execution is important
      If the method might not return immediately
      If the member returns an array
    11. Properties and returning arrays
      public Employee[] All {get{}}
      public Employee[] GetAll() {}
      Calling Code
      EmployeeList l = FillList();
      for (int i = 0; i < l.Length; i++){
      if (l.All[i] == x){...}
      }
      if (l.GetAll()[i]== x) {...}
      Moral: Use method if the operation is expensive
    12. Today…
    13. Extension Methods
      namespace MyCompany.StringManipulation {
      public static class StringExtensions{
      public static bool IsNullOrEmpty(this string s){ return String.IsNullOrEmpty(s);
      }
      }
      }

      using MyCompany.StringManipulation;
      string message= “hello world”;
      if(message.IsNullOrEmpty()){
      Console.WriteLine(“EMPTY”);
      }
    14. Extension methods marry the usability offered by object-oriented APIs with the flexibility of functional APIs.
    15. CONSIDER using extension methods to "add" methods to interfaces
      public interface IFoo{
      void Bar(string x, bool y);
      void Bar(string x);
      }
      public static class IFooExtensions{
      public static void Bar(this IFoo foo, string x){
      foo.Bar(x,false);
      }
      }

      IFoo foo = …;
      foo.Bar(“Hi!”);
    16. 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){ … }
      }
      }
    17. AVOID frivolously defining extension methods, especially on types you don’t own
      Might add clutter
      Choose namespaces for sponsor types carefully
      Remember that not all languages support extension methods
      Users will have to use static method call syntax
    18. AVOID defining extension methods on System.Object
      // C# declaration of the extension method
      public static class SomeExtensions{
      static void SomeMethod(this object o){…}
      }
       
      ‘ VB will try to find the method at runtime
      ‘ … but extension methods are resolved at
      ‘ compile time.
      Dim o As Object = …
      o.SomeMethod() ‘ THIS WILL THROW
      ‘ VB users will have to call the method using the regular static method call syntax.
      SomeExtensions.SomeMethod(o)
    19. Type Design
    20. 10 years ago….
    21. Framework Design Theater
      The Main Character:
      Bright young developer
      The Setting:
      Her first big project
      The Setup:
      Create a class that models a car
      Actions required: Start and Drive
    22. Design Pass One: Meets Requirements
      Pass one: meets requirements
    23. Design Pass Two: More than Enough
    24. Design Pass Three: Way too much
      25
    25. Time to Ship…
      Time to cut…
    26. What we ship: Too much and not enough…
      27
    27. V.Next: Worse Yet
      Now we want to add Color and Model, and we know exactly how
      But it is much harder because the design is half done and mostly wrong
    28. The moral
      Do as little as possible now (but no less) to ensure room for extensibility in the future
    29. Abstract and Base classes
      Prefer broad, shallow hierarchies
      Less than or equal to 2 additional levels – Rough rule!
      Contracts and responsibilities are difficult to maintain and explain in deep complex hierarchies
      Consider making base classes not constructible (that is, use abstract classes)
      Make it clear what the class is for
      Provide a protected constructor for subclasses to call
      System.Exception should not have had a public constructor
    30. Virtual Method Example
      public class TheBase : Object {
      public override string ToString() {
      return “Hello from the Base";
      }
      }
      public class Derived : TheBase {
      public override string ToString() {
      return “Hello from Derived";
      }
      }
    31. Virtual Methods
      What is printed out?
      Derived d = new Derived();Console.WriteLine (d.ToString());
      TheBase tb = d;Console.WriteLine (tb.ToString());
      Object o = tb;Console.WriteLine (o.ToString());
    32. Virtual Methods
      They all output “Hello from Derived”. Why?
      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
    33. Overriding: Follow the Contract
      Don’t change the semantics of member
      Follow the contract defined on the base class
      All Virtual members should define a contract
      Don’t require clients to have knowledge of your overriding
      Should you call the base?
    34. Virtual and non-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
      References to base types must work with derived types without knowing the difference
      Must continue to call in the sameorder and frequency
      Cannot increase or decrease range of inputs or output
      Barbara Liskov
    35. Interface Usage
      public interface IComparable { int CompareTo(object obj);}
      No common implementation (the ActiveX problem)
      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
    36. The great proof of madness is the disproportion of one's designs to one's means.Napoleon Bonaparte
    37. Today…
    38. Type Dependency Management
      Careful dependency management is the necessary ingredient to successful evolution of frameworks. Without it, frameworks quickly deteriorate and are forced out of relevance prematurely.
    39. Framework Layering
    40. DO NOThave upward dependencies
      Avoidhorizontal dependencies
      WPF
      XML



      BCL
      Reflection
    41. Libraries , Primitives, Abstractions
    42. 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!
    43. DOkeep 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
    44. DO NOTcreate abstractions unless you know what you are doing
      Definition:
      Abstractions are interfaces or classes with unsealed members that are passed between components.
      Examples
      Stream, IComponent
      Hard to Evolve
      Unfortunately, pressure to evolve
    45. Trends
    46. 10 years ago….
    47. The Secret of Achiving Great Productivty
      A Pit!!
    48. The Pit of Success: in stark contrast to a summit, a peak, or a journey across a desert to find victory through many trials and surprises, we want our customers to simply fall into winning practices by using our platform and frameworks. To the extent that we make it easy to get into trouble we fail.- Rico Mariani
    49. Is using your framework correctly like…
      Climbing a mountain?
      50
    50. Is using your framework correctly like…
      Scaling a peak?
      51
    51. Is using your framework correctly like…
      Running across a desert?
      52
    52. Is using your framework correctly like…
      Falling into a pit?
    53. Make using your framework as easy as falling into a pit – then you have achived great productivity
    54. Today…
    55. Test Driven Development
      Write tests first, design later
      Requires reusable APIs to be testable:
      • Avoid heavy dependencies, consider inversion of control.
      • Consider designing for dependency injection.
    56. Heavy Dependencies and Testability
      // your API
      public class Tracer {
      MessageQueue mq = new MessageQueue(…);
      public void Trace(string message){ mq.Send(message);
      }
      }
      // your customer’s program that is hard to test
      Tracer tracer = new Tracer();
      public void ProcessOrder(Order order){
      tracer.Trace(order.Id);

      }
    57. 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);
      }
      }
    58. Dependency Injection
      // your customer’s program that is easier to test
      Tracer tracer = new Tracer(new FileListener());
      public void ProcessOrder(Order order){
      tracer.Trace(order.Id);

      }
    59. Dependency Injection Containers
      // customer’s program that is even easier to test
      Tracer tracer = container.Resolve<Tracer>();
      public void ProcessOrder(Order order){
      tracer.Trace(order.Id);

      }
      Check outDI Containers (a.k.a. IoC Containers):autofac, Castle Windsor, PicoContainer.NET, Spring.NET, StructureMap, Unity, and others.
    60. Tools
    61. 10 years ago….
    62. Read the manual??
      When you pick up your rental car….
      Push the seat all the way back
      Find an NPR station
      Find the exit
    63. Oh, down to lock…
    64. How to use a key…
    65. Oh, you push the PRESS button…
    66. Who actually needs this data?
    67. Why you don’t read rental car manuals ???
      You know how to drive your car
      All cars work basically the same way
      Your rental car is a car
      Therefore, you can drive your rental car
      That is…
      The Power of Sameness
    68. Naming Conventions
      PascalCasing – Each word starts with an uppercase letter
      camelCasing – First word lower case, others uppercase
      SCREAMING_CAPS – All upper case with underscores
    69. Naming Conventions
      All types and publicly exposed members are PascalCased
      Parameters are camelCased
      public class MemberDoc
      {
      public int CompareTo(object value)
      public string Name { get;}
      }
    70. Hungarian Notation
      Do not use Hungarian notation in publicly exposed APIs and parameter names
      public class CMyClass { int CompareTo (object objValue) {..} string lpstrName {get;} int iValue {get;}
      }
    71. 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
    72. While we are on naming…
      Good naming is hard—it takes time
      Be meaningful but brief
      Use US-English
      Colour vs. Color
      Principle of least surprise
      Look for prior-art
      NumberOfElements vs. Count
    73. FxCop – Keeping the "power of sameness"
      http://blogs.msdn.com/fxcop
    74. Today…
    75. Framework Design Studio
      http://code.msdn.microsoft.com/fds
    76. Dependency Management Tools
      http://code.msdn.microsoft.com/fxarch
      Define Components
      <Group ID=“Component1”>
      <Bin Name=“MyCompany.FeatureA.dll”/>
      <Bin Name=“MyCompany.FeatureB.dll”/>
      </Group>
      Define Rules
      <Allow From=“Component1" To=“WPF"/>
      <Deny To=“XMLDOM”>
      Run and Get Output:
      From group Component1:
      MyCompany.FeatureA.dll should not depend on:
      SomeOtherComponent
      SomeOtherComponent.dll
      Also check out NDepend – a tool for visualizing dependencies.
    77. Summary
      10 years of Framework design..
      Core Principles of Framework design have stayed the same
      There are some significant new advances
      Check out the new book!
      Brad Abrams
      http://blogs.msdn.com/brada
      Twitter: @brada
    78. Q&A
    79. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
      The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
    80. Some images of movie posters sourced from www.imdb.com

    + bradabrada, 2 months ago

    custom

    1887 views, 1 favs, 6 embeds more stats

    Belgium Visual Studio User’s Group on 10 Years of more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1887
      • 1850 on SlideShare
      • 37 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 76
    Most viewed embeds
    • 15 views on http://cafe321.daum.net
    • 13 views on http://thibautvs.com
    • 3 views on http://www.thibautvs.com
    • 3 views on http://mphome.dp.ua
    • 2 views on http://www.anytao.net

    more

    All embeds
    • 15 views on http://cafe321.daum.net
    • 13 views on http://thibautvs.com
    • 3 views on http://www.thibautvs.com
    • 3 views on http://mphome.dp.ua
    • 2 views on http://www.anytao.net
    • 1 views on http://anytao.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags