Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
farhad-taran / README.md
Last active April 16, 2020 22:08
TDD : Test Driven Development

Test driven development (TDD) is an advanced technique which is an evolution of the test first programming concept in extreme programming.

In TDD the programmer first writes the test for a particular routine before actually writing the routine itself. naturally the test will fail because there is no working code that satisfies the test conditions.
the programmer then writes just enough code to pass the test(Adhering to SOLID, YAGNI and KISS principles).
after the test passes successfully the programmer then returns to the production code in order to refactor it by Changing the code to remove duplication and to improve the design while ensuring that all tests still pass.

this cycle is called Red, Green, Refactor and is done repetitively until the piece of software being developed is complete.

@farhad-taran
farhad-taran / README.md
Last active April 16, 2020 23:18
Using Unit Testing to Enforce Separation of Concerns

Using Unit Testing to Enforce Separation of Concerns

Separation of Concerns is a design principle which states that distinct parts of a software system should be separated and should not overlap in terms of features and functionality.

In technical terms this can be achieved by grouping common functionality into separate projects but that in itself is not enough, any hard reference to other projects that violate the separation of concerns should be avoided and this can be tested and enforced using an integration test.

As an example in the sample solution below we have four projects. to truly have separated concerns in the following solution the presentation layer should only reference the domain services layer and it should not have any hard references to the data access layer.

public interface IMapper<out T>
{
T Map(SqlMapper.GridReader reader);
}
public abstract class MapperBase<T> : IMapper<T>
{
public abstract T Map(SqlMapper.GridReader reader);
public T map(dynamic item)
@farhad-taran
farhad-taran / Compare values attribute
Created March 24, 2015 13:06
Compare values attribute
/// <summary>
/// Specifies that the field must compare favourably with the named field, if objects to check are not of the same type
/// false will be return
/// </summary>
[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class CompareAttribute : ValidationAttribute
{
/// <summary>
/// The other property to compare to
/// </summary>
using System.Web;
using System.Web.Optimization;
namespace WebApplication4
{
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
public static class KernelExtensions
{
/// <summary>
/// Binds an open generic type to its implementation and adds all its defined decorators
/// </summary>
/// <param name="kernel">Ninject Container</param>
/// <param name="openGenericType">Open generic Type</param>
/// <param name="assembly">Assembly to scan for the open generic type implementation</param>
/// <param name="decoratorTypes">Types of the decorators. Order matters. Order is from the most outer decorator to the inner decorator</param>
public static void BindManyOpenGenericsWithDecorators(this IKernel kernel, Type openGenericType, Assembly assembly, params Type[] decoratorTypes)
public static class KernelExtensions
{
/// <summary>
/// Binds an open generic type to its implementation and adds all its defined decorators
/// </summary>
/// <param name="kernel">Ninject Container</param>
/// <param name="openGenericType">Open generic Type</param>
/// <param name="assembly">Assembly to scan for the open generic type implementation</param>
/// <param name="decoratorTypes">Types of the decorators. Order matters. Order is from the most outer decorator to the inner decorator</param>
public static void BindManyOpenGenericsWithDecorators(this IKernel kernel, Type openGenericType, Assembly assembly, params Type[] decoratorTypes)
public static class KernelExtensions
{
/// <summary>
/// Binds an open generic type to its implementation and adds all its defined decorators
/// </summary>
/// <param name="kernel">Ninject Container</param>
/// <param name="openGenericType">Open generic Type</param>
/// <param name="assembly">Assembly to scan for the open generic type implementation</param>
/// <param name="decoratorTypes">Types of the decorators. Order matters. Order is from the most outer decorator to the inner decorator</param>
public static void BindManyOpenGenericsWithDecorators(this IKernel kernel, Type openGenericType, Assembly assembly, params Type[] decoratorTypes)
[TestClass]
public class WhenInitializingCompositionRoot
{
public void CheckTypes(params Type[] typesToCheck)
{
var settings = new Mock<ISettings>();
settings.Setup(x => x.GetSetting(SettingsKeys.ElasticsearchConnectionString)).Returns("http://dummy.com");
settings.Setup(x => x.GetSetting(SettingsKeys.PaymentsAzureStorageConnectionString)).Returns("DefaultEndpointsProtocol=https;AccountName=fakeaccount;AccountKey=0tsE60UFQfQEZIqCpYJQmN2p11vkfOdEp6a8PSWjJML25v1VJkH5MnpLO1s0iZau7KZAJ0ODa+jRjZGlLjUOfg==");
settings.Setup(x => x.GetSetting(SettingsKeys.NotificationsStorageConnectionString)).Returns("DefaultEndpointsProtocol=https;AccountName=fakeaccount;AccountKey=0tsE60UFQfQEZIqCpYJQmN2p11vkfOdEp6a8PSWjJML25v1VJkH5MnpLO1s0iZau7KZAJ0ODa+jRjZGlLjUOfg==");
public interface IQuery<TResult>
{
}
public interface IQueryHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
TResult Handle(TQuery query);
}
public interface IQueryProcessor