using System;
Sharing my knowledge, experience using this Blog on various Microsoft Technologies like WPF, C#, XAML, LINQ, WCF, ASP.Net, And database like SQL Server, Oracle And ORM technologies like NHibernate.
Tuesday, 12 April 2022
Custome Service Class to connect wcf service
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 request. A 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 observing. Observer 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 |
|
IStrategy |
|
Strategy |
|
|
|
|
|
|
|
|
|
Strategy Pattern Advantages.
- A more extensible, object oriented and dynamic implementation.
- Easily add new strategies without affecting existing ones.
- Clean approach with single responsibility in mind.
Strategy Pattern Disadvantage.
- Make sure you don't make your applications more complex than then they have to be!
Strategy Pattern Example.
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.
Custome Service Class to connect wcf service
using System; using System.ServiceModel; namespace Custom.Service.Client { // Borrowed from: http://old.iserviceoriented.com/blog/p...
-
The Major components of WPF Architecture are Presentation Framework, Presentation Core and Media Integration Layer. The architecture divid...
-
Summary/Definitions: All the below interfaces are parts of the System.Collections .Net assembly. IEnumerable: Exposes the enu...
-
As per standard development process we are hosting application after new Iteration (Aglie Methodology) or module development. For client t...