Saturday, 4 January 2014

C# Interfaces

 

Interfaces, Abstract Classes, and Concrete Classes

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

image

 

 

Example:

So here, we are going to create generic method to implement solution with the help of Interfaces

 

Concrete Class Implementation:

image

 

 

 

image

 

 

image

 

 

 

image

 

 

 

image

 

 

In regular concrete class, nothing is forcing us to implement in derived class.  In this example, if you comment the code like below example, we could very well compile code without any error.

 

image

 

 

 

image

 

 

 

Abstract Class Implementation:

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

We get the compiler error if we comment the implementation of derived class as below.  Here abstract class is forcing us to implement member of it.

 

image

 

 

 

Interface Implementation:

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

If we try to comment the member of derived class in Interface Scenario.  It will show the compiler error.

 

image

 

 

 

image

 

 

 

 

image

 

 

 

 

image

 

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

 

Using Interfaces to Future-Proof Code

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

Here, our code is still resistance to changes happened in Parent code.  Our code doesn’t have to change if any library change.

image

 

 

Creating Interfaces to Add Extensibility

 

image

 

 

 

image

 

 

Repository Pattern

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

Demo: Implementing Repository Services

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

Demo : SQL and CSV Repository

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

Demo: Eliminating Duplication by using Repository Factory

 

image

 

 

 

image

 

 

image

 

 

 

Explicit Interface Implementation

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

 

Demo : Explicit Interface Implementation

 

image

 

 

image

 

 

 

image

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

Interfaces and Dynamic Loading

 

image

 

 

 

image

 

 

 

image

 

 

image

 

 

image

 

 

 

image

 

 

 

image

 

 

 

Demo: Dynamic Loading

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

Demo: Layering and Fake Repository

 

image

 

 

To create repository unit test object, we need to create below ViewModel class

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

Demo: Unit Testing

 

image

 

 

 

image

 

 

 

image

 

 

 

 

Advanced Topics

 

image

 

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time.

This can be used, for example, as a simple way to load plugins dynamically or to choose stubs or mock objects in test environments vs. real objects in production environments. This software design pattern injects the depended-on element (object or value, etc.) to the destination automatically by knowing the requirement of the destination.

 

 

Demo: Dependency Injection

 

image

 

 

 

image

 

 

 

image

 

 

 

image

 

 

 

 

image

 

Mocking is one of the real time example of Dependency Injection pattern

 

 

 

 

image

 

 

 

image

 

 

Blogger Labels: Interfaces,Abstract,Classes,Concrete,Example,method,solution,Class,Implementation,error,Here,member,Interface,Scenario,Future,Proof,Code,resistance,Parent,library,Repository,Pattern,Demo,Services,Duplication,Factory,Explicit,Dynamic,Fake,unit,ViewModel,Topics,Dependency,Injection,compiler

No comments:

Post a Comment