Thursday 26 August 2021

Repository Pattern

Repository pattern is one of the Data Access Pattern, which is used to access the data from database or other data source. A repository pattern encapsulates the data access so the consumer no longer has to know about the underlying data structure.

Problem that repository pattern solves

  • The controller / UI is tightly coupled with data access layer , if we are using data access from controller / UI.
  • It is difficult to write a test for the controller / UI without side effects
  • Hard to extends entities with domain specific behaviour.

Benefits of repository pattern

  • The consumer is now separated (decoupled) from the data access
  • Easy to write a test without side-effects.
  • Modify and extend entities before they are passed on to the consumer.
  • A sharable abstraction resulting in less duplication of code.
  • Improved maintainability.

Sunday 8 August 2021

Chain of Responsibility Pattern

Chain of Responsibility pattern comes under behavioral design patterns category, this pattern lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

Iterator Pattern

Iterator pattern comes under behavioral design patterns category, this pattern let you traverse elements of collection without exposing its underlying implementation. This pattern also known as Cursor.

Command Pattern

Command pattern comes under behavioral design patterns category, this pattern turns a request into a stand-alone object that contains all information about the requestA request is wrapped under an object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and passes the command to the corresponding object which executes the command.

Mediator Pattern

Mediator pattern comes under behavioral design patterns category, this pattern lets you reduce chaotic dependencies between objects. The pattern restricts direct communication between the objects and forces them to collaborate only via mediator object. Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling. 

Memento Pattern

Memento pattern comes under behavioral design patterns category, this pattern lets you save and restore the previous state of an object without revealing the details of its implementation.

Observer Pattern

Observer pattern comes under behavioral design patterns category, this pattern lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observingObserver pattern is used when there is one-to-many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.  This pattern also known as Publisher-Subscribe or Dependents.

State Pattern

State pattern comes under behavioral design patterns category, this pattern an object alter its behavior when its internal state changes.

Strategy Pattern

Strategy pattern comes under behavioral design patterns category, this pattern defines skeleton of an algorithm in the superclass but lets subclasses override specific steps of algorithm without changing its structures. This pattern also known as policy pattern.

Strategy Pattern Characteristics.

 

 

 

 

 

 

 

 

Context

Has a reference to a strategy and invoke it

 

IStrategy

Defines the interface for the given strategy

 

Strategy

A concrete implementation of the strategy

 

 

 

 

 

 

 

 


Strategy Pattern Advantages.

  1. A more extensible, object oriented and dynamic implementation.
  2. Easily add new strategies without affecting existing ones.
  3. Clean approach with single responsibility in mind.

Strategy Pattern Disadvantage.

  1. Make sure you don't make your applications more complex than then they have to be!

Strategy Pattern Example.

Shipping of e-commerce product via various courier service provider, where we can implement strategy pattern well. Shipping of product via postal service, FedEx, DTDC, Bluedart, Delhivery. Where shipping strategy is abstract or super class and Postal Service, FedEx, DTDC, BlueDart, Delhivery are sub class and has its own implementation. 

Template Method Pattern

Template method pattern comes under behavioral design patterns category, this pattern defines skeleton of an algorithm in the superclass but lets subclasses override specific steps of algorithm without changing its structures.

Visitor Pattern

Visitor pattern comes under behavioral design patterns category, this pattern lets you separate algorithms from the objects on which they operate. In this pattern we use a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of element can vary as and when visitor vary.

Flyweight Pattern

Flyweight pattern comes under structural design patterns category, this pattern is primarily used to reduce number of objects created and to decrease memory footprint and increase performance. This pattern also allows more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object.

Proxy Pattern

Proxy pattern comes under structural design patterns category, this pattern provides a substitute or placeholder for another object. This pattern provides the control for accessing the original object, allowing you to perform something either before or after the request gets through to the original object.

Facade Pattern

Facade pattern comes under structural design patterns category, this pattern provides a simplified interface to a library or, a framework, or any other complex subsystem.

Decorator Pattern

 Decorator pattern comes under structural design patterns category, this pattern lets you attach new behaviors to object by placing these objects inside special wrapper objects that contain the behaviors. This pattern allows to attach a flexible additional behavior/responsibilities to an object dynamically.

Composite Pattern

Composite pattern comes under structural design patterns category, this pattern allows you to compose objects in term of a tree structure to represent part as well as whole hierarchy. This pattern allows you to work with these tree structures as if they were individual objects.

Bridge Pattern

Bridge pattern comes under structural design patterns category, this pattern allows you to split a large class or a set of closely related classes into two separate hierarchies (Abstraction and implementation) and can be vary independently of each other. This pattern is also known as Body or Handle.

Adapter Pattern

Adapter pattern comes under structural design patterns category, this pattern allows objects with incompatible interfaces to collaborate. This pattern converts the interface of a class into another interface that a client wants.

Singleton Pattern

Singleton Pattern comes under creational design patterns category, this pattern lets you define a class that has only one instance, while providing a global access to this created instance. As singleton is a class designed to only ever have one instance.

Singleton instance example

  • Access to file system.
  • Access to shared network resource
  • Expensive one-time configuration

Singleton Structure

  • Singleton instance which is private
  • Singleton public method/function to access singleton object.
  • Private constructor
  • Other methods and properties

Singleton Features

  • At any time, only 0 or 1 instance of Singleton class exists in the application.
  • Singleton classes are created without parameters.
  • Assume lazy instantiation as the default.
  • A single, private, parameter-less constructor and class should be sealed.
  • A private static field holds the only reference to the instance.
  • A public static method provides access to this field.

Singleton Implementation in C#

  • Naïve Singleton - Not thread safe but provide performance
  • Thread Safe Singleton - Thread safe will slow down performance. Double check thread safe singleton  pattern used to optimize the performance.
  • Leveraging Static Constructors - C# static constructors only run once per app domain. Are called  when any static member of a type is referenced. Make sure you use an explicit static constructor to avoid issue with c# compiler and before field init. (https://csharpindepth.com/articles/BeforeFieldInit)
  • Lazy<T> - Lazy<T> was introduced in .Net 4, provides built-in support for lazy initialization. Specify a means of creating Type. 

Singleton Pattern is also consider as Antipattern.

  • Difficult to test due to shared state.
  • Doesn't follow Separation of Concerns.
  • Doesn't follow single responsibility
  • Doesn't follow DRY
  • Better alternative exist.

Singletons vs. Static Classes

Singleton

  • Can implement interfaces
  • Can be passed as an argument
  • Can be assigned to variables
  • Support polymorphism
  • Can have state
  • Can be serialized

Static Classes

  • No interfaces
  • Cannot be passed as arguments
  • Cannot be assigned
  • Purely procedural
  • Can only access global state
  • No support for serialization

Singletons Behavior Using Containers (IoC)

  • You can use dependency injection tool/library to inject dependency.
  • Container manages instance lifetime
  • .Net core has inbuilt support for dependency injection using service provide.

Prototype Pattern

Prototype Pattern comes under creational design patterns category, this pattern lets you copy/cloning existing object instead of creating new one and also be customized as per the requirement.

Builder Pattern

Builder Pattern comes under creational design patterns category, this pattern lets you construct complex objects step by step approach.

Abstract Factory Pattern

Abstract Factory Pattern comes under creational design patterns category, this pattern lets you create families of related or dependent objects without specifying their concrete class.


Factory Method Pattern

Factory method pattern comes under creational design patterns category, this pattern provides one of the best ways to create an object. This pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Custome Service Class to connect wcf service

  using  System; using  System.ServiceModel; namespace Custom.Service.Client {      // Borrowed from: http://old.iserviceoriented.com/blog/p...