2009 Dotnet Information Day: More effective c#

55
www.devcoach.com PRESENTED BY Daniel Fisher CONTACT US daniel.fisher@devcoac h.com d c EFFECTIVE, LOOSLY COUPLED Writing better code with C#

Transcript of 2009 Dotnet Information Day: More effective c#

Page 1: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

PRESENTED BYDaniel Fisher

CONTACT [email protected]

dcEFFECTIVE, LOOSLY COUPLED

Writing better code with C#

Page 2: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 3: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

LENNYBACON.COMDaniel Fisher | CTO & Software Architect

CCD mit MCP, MCTS, MCPD…[email protected]

Mit-Gründer und Geschäftsführer von devcoach.comwww.devcoach.com

Mit-Gründer und Vorstand dergemeinnützigen www.just community.de e.V.

Veranstalter des größten Entwickler & IT-Pro Community Events in Deutschland: www.nrwconf.de

Mit-Gründer und Leiter derINETA Usergroup Düsseldorf

www.NetUG-NiederRhein.de

Mitglied im Microsoft Community Leader & Insider Program (CLIP)Connected Systems Advisory Board

Expertengruppe für WCF, WF & BizTalk

Page 4: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

DEVCOACH.COM• Leistungen

– Architektur-Beratung• Strukturierter und effizienter zu einer wartbaren Anwendung.

– Prozessoptimierung• BPM• FDD, TDD, MSF Agile & SCRUM

– Software-Entwicklung• Team-out-of-the-box (Near-shoring)• Objektmodelle und Datenzugriff• Kommunikations-Infrastrukturen• Identitäts- und Berechtigungsmodelle• Web 2.0 und Rich Internet Applikation

– Coaching & Training• Technologien schneller verstehen und richtig einsetzen.

• Technologien– Microsoft Windows & .NET Framework

• ASP.NET, WCF, WF, WPF, Silverlight & Geneva

• Kunden– Versicherung, Finanzindustrie, Mittelstand, Handel, Kommunikation,

Softwarehersteller u.v.a.• Bundesamt für Sicherheit in der Informationstechnologie, Microsoft,

Dresdner Bank…

Project Experience

Technology Know-how

devcoach®

Page 5: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

AGENDA

• Loose Koppelung• Konzepte• Code

Page 6: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

LOOSE KOPPELUNG

Page 7: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 8: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 9: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 10: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 11: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

WHAT IS BAD DESIGN?

"That's not the way I would have done it..."

Well, that is not a valid measure for the quality of the design! This statement is purely based on personal preferences.

Page 12: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

WHAT IS BAD DESIGN?• Rigid: – it's hard to change a part of the system without affection

too many other parts of the system

• Fragile: – when making a change, unexpected parts of the system

break

• Immobile: – it is hard to reuse it in another application because it cannot

be disentangled from the current application.

Page 13: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

THE "OBJECT-STYLE"

Page 14: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 15: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 16: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

PetSearch

PetSearchResultList

PetSearchResult

Page 17: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

QUESTION?• Are you telling me that every screen the app

has, there is a different object?

• No, but …– Define your objects like they are "viewed" in the

application.

Page 18: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

KONZEPTE

Page 19: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

DESIGN GOALS• There are several design goals– Reusability– Maintainability– Performance – Scalability– …

• Make decisions on the goals of you application!

Page 20: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

WHY MAINTAINABILITY?• The most expensive and time-consuming work of software

development is the work of finding bugs and fixing them. – In the U.S. the number of bugs … averages five per function point. – The cost of finding and repairing these defects averages about 35

percent of the total development cost of building the application. – The schedule time required is about 30 percent of project development

schedules.

• Defect repair costs and schedules are often larger than coding costs and schedules.

Capers Jones, Estimating Software Costshttp://www.amazon.com/Estimating-Software-Costs-Capers-Jones/dp/0071483004/

Page 21: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

COMPOSITION OVER INHERITANCE

Person

Employee

Person Employee

Page 22: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

COMPOSITION OVER INHERITANCE

Person

Employee

Person Contract

Company

Page 23: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

FRAGILE BASE CLASS• A fundamental architectural problem problem

of OOP• Base classes (superclasses) are considered

"fragile" – Modifications to a base class may cause derived

classes to malfunction. – The programmer cannot determine whether a base

class change is safe simply by examining in isolation the methods of the base class.

• Solutions: "sealed" and "non-virtual"http://en.wikipedia.org/wiki/Fragile_base_class

Page 24: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

COMPOSITION ADVANTAGES• More flexibility • Much more resilient to change– When used properly :-)

• Where inheritance represents the relationship with the most coupling between two classes, composition allows us to freely decouple two (or more) classes.

Page 25: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

INHERITANCE SAMPLE

public class SqlRoleProvider{ …}public class CachedSqlRoleProvider : SqlRoleProvider{ public override string[] GetAllRoles() { //TODO: Get from Cache and return var roles = base.GetAllRoles()

//TODO: Fill Cache return roles; }}

Page 26: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

SOLID DESING…• Single Responsibility Principle • Open Closed Principle• Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle

Page 27: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

SINGLE RESPONSIBILITY PRINCIPLE• A class should have one, and only one, reason to

change.

• "Separation of concerns"

Page 28: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 29: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

OPEN CLOSE PRINCIPLE• You should be able to extend a classes behavior,

without modifying it.– Keep public interfaces reduced to the neccessary…• All member fields private.• No Global Variables -- Ever.

Page 30: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 31: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

LISKOV SUBSTITUTION PRINCIPLE• Derived classes must be substitutable for their

base classes.

Page 32: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

IMAGINE FOLLOWING

public class Rectangle{

public void SetWidth(double w) { itsWidth=w; }public void SetHeight(double h) { itsHeight=w; }public double GetHeight() { return itsHeight; }public double GetWidth() { return itsWidth; }private double itsWidth;private double itsHeight;

};

Page 33: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

NOW THIS …

public class Square : Rectangle{ new void SetWidth(double w) { base.SetWidth(w); base.SetHeight(w); } new void SetHeight(double h) { base.SetHeight(h); base.SetWidth(h); }}

Page 34: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

WHAT WILL HAPPEN HERE?

public void f(Rectangle r){ r.SetWidth(32); }

Page 35: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

AND HERE?

void f<TObj>(TObj r) where T: Rectangle{ r.SetWidth(32); }

Page 36: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

ENABLE EXTENSION…

public class Rectangle{

public void virtual SetWidth(double w) { itsWidth=w; }

public void virtual SetHeight(double h) { itsHeight=w;}

public double GetHeight() { return itsHeight; }public double GetWidth() { return itsWidth; }private double itsWidth;private double itsHeight;

};

Page 37: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 38: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

INTERFACE SEGREGATION PRINCIPLE

• Make fine grained interfaces that are client specific.

Page 39: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

IMAGINE FOLLOWING

public interface IRectangle{

void SetWidth(double w);void SetHeight(double h);double GetHeight();double GetWidth();

};

Page 40: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

VS. THAT

public interface IRectangle{

double GetHeight();double GetWidth();

};

public interface IEditableRectangle{

void SetWidth(double w);void SetHeight(double h);

};

Page 41: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 42: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

DEPENDENCY INVERSION PRINCIPLE

• Depend on abstractions, not on concretions.

Page 43: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

IMAGINE FOLLOWING

public class Lamp{ public void TurnOff(); public void TurnOn();};

public class Button{ private Lamp _lamp; public Button(Lamp lamp) { _lamp = lamp; } public void Detect() { if (GetPhysicalState()) _lamp.TurnOn(); return; _lamp.TurnOff(); } public bool GetPhysicalState(){ return Clicked;}};

Page 44: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

VS. THAT

public class ButtonClient{ public void TurnOff(); public void TurnOn();}public class Lamp : ButtonClient{ //The button client allows us to re-use the button...}public class Button{ private ButtonClient _client; public Button(ButtonClient client) { _client = client; } public void Detect() { if (GetPhysicalState()) _client.TurnOn(); return; _client.TurnOff(); } public bool GetPhysicalState();};

Page 45: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Page 46: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

INVERSION OF CONTROL

• Constructor Injection• Method Injection• Property Injection• Configuration Injection– Dependency Object Container

Page 47: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

MICROSOFT AND IOC...

public interface IServiceProvider { object GetService(Type serviceType); }

public interface IServiceLocator : IServiceProvider { GetAllInstances<TService>(); IEnumerable<object> GetAllInstances(Type serviceType); TService GetInstance<TService>(); TService GetInstance<TService>(string key); object GetInstance(Type serviceType); object GetInstance(Type serviceType, string key); }

Page 48: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

SIMPLE DESIGN

“Simplicity is more complicated than you think. But it’s well worth it.”

• Satisfy requirements– No Less– No more

• Shape units

Page 49: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

DIVIDE AND CONQUER

„recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. “

„ The solutions to the sub-problems are then combined to give a solution to the original problem. “

Page 50: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

CREATE ASSEMBLIES WISELY• More assemblies = more physical stuff– Build, configuration, Deployment, File IO…

• Who will pay that?

• But necessary for:– Product Separation?– Different Liceses models?

Page 51: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

SIMPLE DESIGN CRITERIA

• The code …– Fulfills the requirements.• YAGNI

– Has the smallest number of classes.• FxCop Rule: At least 3 members per class.

– Has the smallest number of methods• Keep your methods maintainable.

– At maximum 50 lines– At least 80 chars– …

Page 52: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Demo!

Page 53: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Thank you!

Page 54: 2009 Dotnet Information Day: More effective c#

www.devcoach.com

Q & A