http://mrbool.com/10-tips-about-sql-server-that-every-developer-should-know/4982
OOP vs !OOP
Advantages of OOP
1. Well-designed oop code EASY TO REUSE. Shared modules can be distributed as libraries which can be used in other applications.
2. INCAPSULATION. In well-designed OOP code in order to use OOP objects clients dont’t need to know how objects works internally they work with declared interfaces/contracts only.
3. MAINTAINABILITY. Well-designed code is easy to maintain it is easy to substitute one module with another without breaking functionality.
4. EASY TO WRITE LARGE APPLICATIONS.
Disadvantages of OOP
1. Performance. OOP have to deal with lots of objects in memory
2. LONG LEARNING CURVE.
CLR multithreading, Part 1
A process is an inner container which has its own virtual address space
Libraries of code are mapped into the address spaces
A thread is path of execution through code within a single process.
For managed threads within an AppDomain
Each thread has it’s own callstack and copy of cpu registers
Advantages:
1. Better cpu utilization when a process runs on multiple core cpu or multiple processors machine.
2. Better support for asynchronous I/O operations. Performing CPU bound operations while waiting to complete I/O bound operations
3. Better UI responsiveness
Disadvantages:
Context switching overhead
Added program complexity:
1. More lines of code
2. Hard to maintain (readability)
3. Hard to debug
4. Hard to test
CLR thread class name
System.Threading.Thread
Thread life time:
Execution continues until thread returns from its entry point.
1. As a result of a standard method return.
2. As result of unhandled exception
1. As an exception caused by the running thread
2. As a result of calling Abort(), Interrupt() methods.
Coordinated thread shut-down
1. User-defined mechanism
2. Requesting thread waits:
2.1 Polling IsAlive
2.2. Calling Thread.Join
Thread Pool
CLR provides per process thread pool
1. Whenever thread is created a few microseconds goes on creating a thread stack and for allocating a memory. Thread pooll cuts overhead by sharing and recycling threads.
2. CLRs pool manager add thread or removes thread from a thread pool on demand.
3. IsBackground property is set to false
When not to use a thread pool
1. When we need foreground thread
2. When we need to set priority to the thread
3. When we run a long running task
4. When we need to identify a thread
Delegates and Async I/O
1. Delegates BeginInvoke queue a request method to the ThreaPool
private int AddMethod(int x)
{
return (unchecked)i++;
}
private delegate string OperationMethod(int x);
OperationMethod test = Method;
test.BeginInvoke(0, null, null);
2.
2.1 In case of Async I/O when we call I/O methods such as BeginRead, BeginWrite these methods calls goes to I/O completion port and these methods do not block the thread.
2.2. When Async I/O methods complete only then this call is queued to the ThreadPool.
Git ignore
Interview books
ASP.NET Internals
lifecycle-of-an-aspnet-mvc-5-application
Decorator pattern
Decorator design pattern allows to attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Client
[TestMethod] public void DecoratorTest() { IComponent concreteComponent = new ConcreteComponent1(); IComponent concreteDecorator1 = new ComponentDecorator1(concreteComponent); IComponent concreteDecorator2 = new ComponentDecorator2(concreteDecorator1); Assert.AreEqual(concreteDecorator1.Operation(), "ComponentDecorator1 + " + concreteComponent.Operation()); Assert.AreEqual(concreteDecorator2.Operation(), "ComponentDecorator2 + " + ((ComponentDecorator2)concreteDecorator2).ExtendedOperation() + " + ComponentDecorator1 + " + concreteComponent.Operation()); }
The Component defines the interface for components that can have dynamically attached responsibilities.
/// <summary> /// Declares the interface for components that can have dynamically attached responsibilities. /// </summary> public interface IComponent { string Operation(); }
The ConcreteComponent implements the Component’s interface to which responsibilities can be attached dynamically.
/// <summary> /// Implements the Component's interface to which responsibilities can be attached dynamically. /// </summary> public class ConcreteComponent1 : IComponent { public string Operation() { return "ConcreteComponent1"; } }
The Decorator maintains a reference to the Component’s object and defines the interface that conforms the Component’s interface
/// <summary> /// Maintance a reference to the Component's object and define an interface that conforms the component's interface /// </summary> public abstract class AbstractComponentDecorator : IComponent { private IComponent _refToConcreteComponent; protected AbstractComponentDecorator(IComponent concreteComponent) { _refToConcreteComponent = concreteComponent; } public virtual string Operation() { return _refToConcreteComponent.Operation(); } }
The ComponentDecorator adds responsibilities to an attached component.
/// <summary> /// Adds responsibilites to a component /// </summary> public class ComponentDecorator1 : AbstractComponentDecorator { public ComponentDecorator1(IComponent concreteComponent) : base(concreteComponent) { } public override string Operation() { return "ComponentDecorator1 + " + base.Operation(); } }
Force Git to overwrite local files on pull
Command design pattern
Command design pattern encapsulates request as an object thereby allowing parameterize other objects with different request, log and queue requests, and support undoable operations.
Test client:
[TestFixture] public class CommandPatternTest { private Calculator _calculator; private Invoker<int> _invoker; [SetUp] public void SetUp() { //Arrange _calculator = new Calculator(); _invoker = new Invoker<int>(); } [TestCase(1, 2, 3)] public void InvokedAddCommand_OnValidParams_ReturnsExpectedResult(int a, int b, int expectedResult) { //Arrange var addCommand = new AddCommand<int>(_calculator, a, b); //Act var actualResult = _invoker.SetAndExecuteCommand(addCommand); //Assert Assert.AreEqual(expectedResult, actualResult); } [TestCase(3, 2, 1)] public void InvokedSubtractCommand_OnValidParams_ReturnsExpectedResult(int a, int b, int expectedResult) { //Arrange var subtractCommand = new SubtractCommand<int>(_calculator, a, b); //Act var actualResult = _invoker.SetAndExecuteCommand(subtractCommand); Assert.AreEqual(expectedResult, actualResult); } [TestCase(1, 2, 3)] public void InvokedUndo_ReturnsExpectedResult(int a, int b, int expectedResult) { //Arrange var addCommand = new AddCommand<int>(_calculator, 1, 2); _invoker.SetAndExecuteCommand(addCommand); _invoker.SetAndExecuteCommand(addCommand); //Act var actualResult = _invoker.Undo(); //Assert Assert.AreEqual(expectedResult, actualResult); } }
Invoker:
////// The Invoker holds a command and calls command's execute method /// /// public class Invoker where T : struct, IConvertible, IComparable { private Stack<ICalculatorCommand> _commands = new Stack<ICalculatorCommand>(); public void SetCommand(ICalculatorCommand command) { _commands.Push(command); } public T SetAndExecuteCommand(ICalculatorCommand command) { _commands.Push(command); return command.Calculate(); } public T Execute() { return _commands.Peek().Calculate(); } public T Undo() { if (!_commands.Any()) throw new InvalidOperationException(); _commands.Pop(); return _commands.Peek().Calculate(); } }
Receiver:
public enum Operand { Add = '+', Subtract = '-' } ////// A commands receiver, performs real actions /// public class Calculator { public dynamic PerformCalculation(Operand operand, dynamic arg1, dynamic arg2) { switch (operand) { case Operand.Add: return arg1 + arg2; case Operand.Subtract: return arg1 - arg2; } return null; } }
Abstract command:
public interface ICalculatorCommand where T : struct, IConvertible, IComparable { T Calculate(); }
Concrete commands
public class AddCommand : ICalculatorCommand where T : struct, IConvertible, IComparable { private Calculator _calculator; private T _arg1, _arg2; public AddCommand(Calculator calculator, T arg1, T arg2) { _calculator = calculator; _arg1 = arg1; _arg2 = arg2; } public T Calculate() { return (T)_calculator.PerformCalculation(Operand.Add, _arg1, _arg2); } }
public class SubtractCommand : ICalculatorCommand where T : struct, IConvertible, IComparable { private Calculator _calculator; private T _arg1, _arg2; public SubtractCommand(Calculator calculator, T arg1, T arg2) { _calculator = calculator; _arg1 = arg1; _arg2 = arg2; } public T Calculate() { return (T)_calculator.PerformCalculation(Operand.Subtract, _arg1, _arg2); } }