Pages

Sunday, July 31, 2016

Interface Redefined

Interface is a very common term that we often comes across while writing code, reading article or discussing design aspect etc. but many times we are not knowing the True Power of it. Here, I’ve tried to redefine it so as to make it understand better.

A concise definition of Interface can be termed as “Interface is a Contract for a set of Functionality in Abstraction so as to preserve or enhance the Maintainability, Readability, Extensibility and Testability of an application through Inheritance and Loose Coupling.”

Let’s see how Interface is required to implement SOLID design principle.
  • S: Single Responsibility Principle (SRP) – Not relevant for Interface, only for Class.
  • O: Open closed Principle (OSP) – Can be achieved by implementing Interface which allows inheriting classes to add more functionality without changing the Interface provided features.
  • L: Liskov substitution Principle (LSP) – Interface implementation ensures that child classes are replaceable without any interventions.
  • I: Interface Segregation Principle (ISP) – An Interface should have granular level of functionality so that no client should be enforced to implement a functionality which is not required.
  • D: Dependency Inversion Principle (DIP) – Interface implementation ensure that high-level module/classes does not depend on low-level module/classes.

Let’s break all the letters in I-N-T-E-R-F-A-C-E to find its best representation. I could figure out as:
I - Inheritance (supported multiple)
N - Negligible (Loose) Coupling
T - Testability
E - Extensibility
R - Readability
F - Functionality
A - Abstraction
C - Contract
E - Easy Maintenance




More detail on these is provided below.

I – Inheritance (supported multiple)
Inheritance is one of the vital OOPS concepts. Interface helps us achieve multiple Inheritance through multiple Interface implementation which is otherwise not supported in .Net Framework. Even if there is a method with same in name in two Interfaces we can implement both without conflict through Explicit Interface implementation.

N – Negligible (Loose) Coupling
Interface implementation helps us achieve Loose Coupling in our code as we are not dependent on any concrete type implementation and referencing to an interface only. We are free to change the underlying implemented class in any stage of SDLC (Software Development Life Cycle) with minimal changes (only at IoC Type registration).

Interface helps us write Testable code, we can easily “plug and play” by replacing external Libraries or Dependencies with Fake objects. Often we face difficulties to test our code because it is associated with or using some external Library or Dependency which we don’t want to test if we are focused on Unit level testing. Inducing Fake objects with minimum required output to pass the test is a “Life Saving” trick in many scenarios. Interface plays a key role in TDD implementation.

E – Extensibility
Interface supports Extensibility by allowing the concrete class to extend its features by implementing any number of methods beyond what is defined in Interface contract.

R – Readability
Interface enhances Readability of code by replacing the unorganized, cluttered code in concrete class with a well-formed “Clean Code”.

F – Functionality
Interface defines a set of functionality which has to be implemented by implementation class. An interface can contain events, indexers, methods, and properties declaration which has to be implemented by class inheriting to it.

A – Abstraction
Abstraction is one of the vital OOPS concepts. Interface helps us achieve abstraction by defining the related set functionality in an Interface.

C – Contract
Interface helps us defining a contract to ensure that we are following a single standard and there is no deviation which can in turn result in a disaster while working on mid or Enterprise level applications. Even if a new or novice Developer is joining a team, he would be bound to follow the contract for any implementation. It forces to implement code deliberately.

E – Easy Maintenance
Interface helps us achieve highest Maintainability of our code through testable, loosely coupled, extensible and clean code.

Please refer the Repository Pattern implementation code at my Github repository which shows how can plug and play various repositories without having a single line code change at our client (UI) side.

Please feel free to post your comments or feedback.

Sunday, July 25, 2010

Data Pagination

To display multiple records on a page/window we used to bind them with some grid control. Although it works fine for small data but it can become a nightmare if we have to display a huge data in a grid, for example millions of records.

The smart way to deal with this problem is get the only records which can be displayed in a grid at time. In other words we may have to fetch records as per the page size of the grid. To do this we need some pagination query which should be able to fetch in provided range from the resultset of a query.

Here is a simple example for the same as
Select * From (
Select row_number() OVER (Order by ContactId) as ResultNum, *
From AdventureWorks.Person.Contact) as NumberResult
Where ResultNum Between 100 and 200

In the example above, we will fetch the records in the range of 100 to 200 only.

There may be scanario that we have to fetch paging records with certain condition Title='Mr.' In that scenario we can provide a where clause in the inner query.

This is really a cool stuff.

Wpf Binding

Most of the time we struggles to bind our controls with the data properly. It is very useful to have some samples or cheatsheets handy with us.

These are the links to some of the good cheat sheets in pdf format by NBD and Blueboxes

Whenever you stuck in wpf bindings have a look at these cool cheat sheets.