C:\Fakepath\Combating Software Entropy 2

  • Published on
    08-May-2015

  • View
    1.667

  • Download
    0

DESCRIPTION

Hammad Rajjoub's presentation of Software Design and Architecture during Microsoft Tech Ed Middle East.

Transcript

1. 2. ARC202 - Combating Software Entropy with Design Patterns and PrinciplesHammad RajjoubSolutions Architect @ InfusionMicrosoft MVP3. Cogito, Ergo , SumMicrosoft MVP Connected Systems (5+ yrs)Solutions Architect at InfusionThe coolest company in townDevelop. Design. Venture. Join us!Speaker, Author & Community LeaderI do: Blog + Twitter + PodCastBing me http://www.bing.com/search?q=hammadrajjoub4. Agenda Why is Software Complex? What is bad design? How to Fix it? Summary ReferencesQnA5. Why is Software ComplexWriting new softwareMandated to develop new systemsGenerally from scratch But still mostly relying on existing libraries and frameworksReal-world problems are sometimes complexModifying Existing SoftwareFind that bug and fix itAdd a new exciting featureReview and refactor to a better design6. What is bad design?7. Bad design is...Hard to change!A single change break lots of other codeRigidFragileCant be extended Immobile8. How to fix it?Using design principles and practicesThe Single Responsibility PrincipleThe Open Closed PrincipleLiskov Substitution PrincipleDependency Inversion PrincipleUsing Software Design MetricsAnd yes a whole lot of refactoring 9. Single Responsibility PrincipleNone but Buddha himself must take the responsibility of giving out occult secrets...E. Cobham Brewer 18101897.Dictionary of Phrase and Fable. 1898.SRP10. The Single responsibility principal"A responsibility is a reason to change, a class or module should have one, and only one, reason to change."11. The Single Responsibility PrincipalResponsibility is a Reason for changeEach responsibility is an axis of changeThere should never be more than one reason for a class to changeDijkstras SoC: Separation of ConcernsThis helps us evaluate a class sexposure to change12. BusinessPartnerValidtor ModuleExample:13. BusinessPartnerValidatorBusinessPartnerValidatorDBTradeWhat is wrong here: Changes if DB changes or Business Logic Changes14. Counterparty Validator Iter 1internal class BusinessPartnerValidator{public void AssertValid(Trade t){varsql = "SELECT COUNT(*) FROM BusinessPartner WHERE name=@Name";using (varconn = CreateOpenConnection()){varcmd = new SqlCommand(sql, conn);cmd.Parameters.Add("@Name", SqlDbType.VarChar);cmd.Parameters["@name"].Value = t.BusinessPartnerName;var count = (Int32) cmd.ExecuteScalar();if (count != 1) throw new InvalidBusinessPartyException(t.BusinessPartyName);}}...Where is the business logic? Hidden by database code.15. BusinessPartyValidator Iter 2 internal class BusinessPartnerValidator{private readonlyBusinessPartnerValidatorbusinessPartnersource;public BusinessPartyValidator(BusinessPartnerSourceBusinessPartySource){this.businessPartnerSource = BusinessPartnerSource;}public void AssertValid(Trade t){if (BusinessPartnerSource.FindByName(t.BusinessPartnerSource) == null) throw new InvalidBusinessPartnerException(t.BusinessPartnerName);}}BusinessPartyValidator now has a single responsibility16. RefactoredBusinessPartnerValidatorBusinessPartnerValidatorBusinessPartnerSourceTradeDBWhat's its job?Classes must have an identifiable single responsibility. 17. Open Closed Principle..this is the heart of Object Oriented Programming...OCP18. The Open Closed PrincipleRealise that generally systems outlive their expected timelinesAll software entities should be closed for change and opened for extensionYour design modules should never changeYou should just extend the behaviour19. Liskov Substitution PrincipleThere is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory which states that this has already happened. Douglas Adams (1952 - 2001)20. The Liskov substitution principalFunctions that reference a base class must be able to use objects of derived classes without knowing it."21. Using Inheritance for OCP?RateCalculatorTradeOptionFee22. Trade Rate Calculationpublic decimal CalulateRate(Trade t){if (t is Fee){return 1.0m;}return t.BaseAmount/t.QuotedAmount;}Use of convention rather than design 23. Fragile: sensitive to any change in Trade Fee Hierarchy