Object-oriented design principles

C# is a general-purpose language that can work in a managed environment. It is the .NET Common Language Runtime (CLR) that handles the most tricky logic for us, such as the lifecycle of variables and their removal from memory, process isolation, thread abstraction, variably safe typing, and so on.

Although we will assume that we will use C# only in object-oriented programming (OOP), the language itself supports other paradigms as well.

Note

In terms of class design, the following tenets are at the basis of OOP:

  • Encapsulation: Any class can hide its core logic in external items.
  • Inheritance: Any class can expand the capability of a mother class, by adding more specific properties or adding/changing logics.
  • Polymorphism: Any object, if extended by inheritance, when compared to other objects of the same parent family, may produce different business results by applying the eventually changed logic as allowed by inheritance, creating what names a polymorphic behavior.

Object orientation is all about abstracting the real world in multiple business-like items that represent any business-related entity, such as a house in the real-estate business domain or a customer in the invoicing business domain. Entities may not only be people or companies. Anything that can be represented in detail with finite properties (data) and the ability to use some logic on inner data or that can interact with other entities is definitely a living entity in terms of OOP. This living entity is called an object. An entity is a part of a family of objects that has the same logic but has different related inner data, called a class. Therefore, an object is an instance of a class.

Understanding OOP will greatly simplify the understanding of how to subdivide the whole application in multiple modules/layers/tiers. Do the dictates from OOP give all the answers? Actually, no. Another group of OOP design principles made by Michael Feathers and Robert C. Martin in 2000, named SOLID, explains in detail on how to actually program for OOP. Understanding those other principles will give us knowledge about all architecture decisions.

The word SOLID is an acronym of five principles. Let's briefly look at them.

The single responsibility principle

A single class must have a single responsibility, something like abstracting a single business entity or a single communication protocol. It does not matter how easy creating such an entity is or how much coding such an entity needs. This is because if a single class tries to abstract multiple entities, it would be unable to actually abstract any of those entities proposed. This does not mean that multiple classes abstract entities that are more specific. For example, applying an inheritance principle does not inherit a single and a more abstract class (the mother class).

The open-closed principle

This principle states that a class must be open for extension and closed for modification. This is an important principle regarding teamwork and code lifetime management. If a class never changes its contract (public shape) but still grows up in functionality, it then maintains compatibility with the older versions. Later in this chapter, in the Common designs and architectures section, we will see how this principle is also applicable in specific high-distributed architectures such as the n-tier or Service Oriented Architectures.

The Liskov substitution principle

This principle states that wherever an object is used, a subtype must be used in the same place, without ever changing any code and without having any different behavior in the application. This tenet extends the OOP inheritance principle by giving us a direct test case to validate how inheritance is applied to our class hierarchy.

The interface segregation principle

This principle states that a role interface must be created, based on what a client (the caller of the methods of any object) needs with no more logic (methods) or properties than effectively required. These interfaces help software decoupling between modules, module management, and development because it splits the contract (the shared need of a client in the form of a C# interface) from the concrete implementation. Like creating a facade, this principle helps to simplify the shape of the object to its clients and helps move concrete logic from different classes, if needed, without (actually decoupling) any change in the client usage.

The dependency inversion principle

This principle extends the preceding interface segregation principle by stating that better decoupling and management of software modules happens by inverting references between objects. Without this principle, multiple modules calling each other at the same or from different levels will make up the software. Although abstraction is practically nothing at some level, such as when developing a module with, for instance, classes for low-level network communication, for most other modules, abstraction is significantly useful for reducing coupling. For instance, with a strait dependency, a desktop application using this module will contain a UX module that calls business logic in one module and that in turn calls another system by using another module (A -> B -> C).

By inverting the dependency, we will find that module A asks for another module (without knowing the concrete name/implementation) that extends any interface that A needs. The same occurs between module B and the unknown module C, when B asks for a module to implement its needs (module C). A practical implementation of this scenario is the plugin pattern or the Inversion of Control (IoC) with the Dependency Injection (DI) pattern.

By respecting this principle, the improved decoupling is visible. Less visible is the little drawback this solution has. If used extensively, this solution could create some performance issues, such as added latency and higher resource usage that came from data-mapping usage. Later in this chapter, we will see a case study on Dependency Injection.

Note

Further information about SOLID can be found at:

http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.130.199