Tags
In My Opinion
My opinion in nutshell
Using delegate in CSharp example
What is Delegate? It is a special type in cSharp that reference method. You can subscribe methods with delegate and invoke them. What is Syntax? It is similar to method syntax that has delegate keyword. In order to subscribe a method to a delegate, both of them should have same “Input” and “Return” types. delegate syntax Public delegate void SomeDelegation(string message); Example We have a program that sends a message to the email and cell phone of our customers.
Value Type Vs Reference Type in cSharp example
What is Reference type ? When we have reference type the value inside the object is pointing to the address in the memory. Multiple variable can point to the same address in the memory . Also it is possible to change the memory of the address that variable pointing to it in its lifetime. Object, Classes and some variable types such as String are reference type. Example If you have someDto classes
Unity IoC container example
What is Unity It is an IoC container framework from Microsoft. You can download it from Nuget or codeplex . What we have ? We have a printer class that accepts printer type and do printing according to the passed type. print class public class Print { private readonly ITypeOfPrinter _typeOfPrinter; public Print(ITypeOfPrinter typeOfPrinter) { _typeOfPrinter = typeOfPrinter; } public string print() { return _typeOfPrinter.Printing(); } } The passed type can be Black and white or colorfull.
Using Structuremap IoC framework example
What is Structuremap? It is one of the oldest IoC container frameworks which created by Jeremy Miller around June 2004. It is a stable open source framework that used in many projects. You can download it from Nuget . What we have ? We have a printer class that accepts printer type and do printing according to the passed type. print class public class Print { private readonly ITypeOfPrinter _typeOfPrinter; public Print(ITypeOfPrinter typeOfPrinter) { _typeOfPrinter = typeOfPrinter; } public string print() { return _typeOfPrinter.
Building IoC container cSharp example
What is IoC? IoC is referring to inversion of control which means, instead of asking our supper class match themselves with subclasses , ask subclasses to adopt themselves with super class by implementing their interface. For instance forcing all electrical devices to use the apple port to charge themselves rather than building different charger for devices. Some definition DIP: Dependency Inversion principal refers to the idea of inversion of control
Csharp example of Singleton design pattern
What is Singleton design pattern? The idea of singleton pattern is to have only one instance of a class or in other words have one instance of class when only need it . The class is responsible to make only one instance of itself. We can use this pattern when the class does not have parameter in its constructor and making objects of the class is expensive. Example What We have?
CSharp example of Bridge design pattern
What is Bridge pattern? It help us to decouple interface from its implementation therefor they can be vary independently by adding one more higher layer abstraction to our interface and inject it to our concrete class. Therefore our concrete class can use the new higher level of abstraction through our current interface without implementing it . What we have We have ErrorHandler class that accept exceptions and email it to appropriate persons to chase them.
CSharp example of Visitor design pattern
What is the Visitor design pattern? Give a structure some new functionality without change its structure. In fact we will have some master class that has the all logic and we will be using its knowledge in an abstracted way. Example What we have DTOs We have Printer DTO which consist of different Types of printers. Each printer has its own properties some of them are unique and some of them are common.
CSharp example of visitor pattern for ElseIf condition
We can use the Visitor design pattern to get round the elseif or switch case conditions and push our method to the single responsible behavior. This design pattern can apply if the conditions are different in types and not values. Example We have a Factory method that decide of the the passing class type.Class one and two are abstracted in the ICondition interface. public class ElseIf { public string Factory(ICondition condition) { string status=string.
CSharp example of Adapter design pattern
What is it? It adapts your client code interface with a library interface. Sometimes we call adapters as wrappers. This design pattern can help us to achieve open/close principal in SOLID design. Example Class Library In the code below, we have the class library that get the machine detail and then print it in a special format. public class Machine : IMachine { private readonly IPrinter _printer; public string Output; public Machine(IPrinter printer) { _printer = printer; } public void PrintMachineName(IMachineDetail machineDetail) { Output = _printer.
←
Newer Posts
Older Posts
→