Archive

Posts Tagged ‘clr’

Delegates

January 25, 2011 1 comment

Imagine we are writing an Employees Collection class which has methods to filter by first name and department. The code looks so similar, can we somehow parameterize it to remove the duplication?

 

using System;
using System.Collections.Generic;

namespace CodingArchitect.Demos.Delegates
{
    public class EmployeesCollection
    {
        private readonly List<Employee> employees =
            new List<Employee>();

        public EmployeesCollection FilterByFirstName(string firstName)
        {
            var matches = new EmployeesCollection();
            foreach (var employee in employees)
            {
                if(string.Equals(employee.FirstName, firstName,
                    StringComparison.InvariantCulture))
                {
                    matches.Add(employee);
                }
            }
            return matches;
        }

        public EmployeesCollection FilterByDepartment(string department)
        {
            var matches = new EmployeesCollection();
            foreach (var employee in employees)
            {
                if (string.Equals(employee.Department, department,
                    StringComparison.InvariantCulture))
                {
                    matches.Add(employee);
                }
            }
            return matches;
        }

        private void Add(Employee employee)
        {
            employees.Add(employee);
        }
    }
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
    }
}

Parameterize Method refactoring immediately comes to mind, only in this case the parameter should be a function which does the actual filtering. Passing functions as parameters is perfectly valid in C#. Now the condition alone can be passed as a delegate to the filter method. I am reusing the Predicate<T> delegate defined in the BCL. This is exactly what the FindAll method on the List<T> type in BCL does.

 

using System;
using System.Collections.Generic;

namespace CodingArchitect.Demos.Delegates
{
    public class EmployeesCollection
    {
        private readonly List<Employee> employees =
            new List<Employee>();

        public EmployeesCollection Filter(Predicate<Employee> condition)
        {
            var matches = new EmployeesCollection();
            foreach (var employee in employees)
            {
                if (condition(employee))
                {
                    matches.Add(employee);
                }
            }
            return matches;
        }

        private void Add(Employee employee)
        {
            employees.Add(employee);
        }
    }
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
    }
}

The condition predicate can now be passed from the call site using a delegate (syntactic sugar like anonymous delegates, lambda expressions make it easier further).

Advertisements

Popular Posts

November 19, 2010 1 comment

Popular posts

 

Threading Study Notes

Multi-Threading Basics – Setting the stage

Multi-Threading Basics – Race Conditions

Multi-Threading Basics – Deadlocks, Livelocks and Starvation

Multi-Threading options in .NET

Thread States in .NET

Atomic access functions – Interlocked family of functions

Critical Sections of code – Monitor

Waiting for something to happen – WaitHandles

A cross process synchronization – Mutex

Waiting for something to happen – Manual and AutoResetEvents

 

 

SOA Study Notes

SOA, The next stage in the evolution

SOA Tenets

SOA Tenets – Policy based negotiation

SOA Tenets – Service Boundaries are Explicit

SOA Tenets – Service are autonomous

SOA Tenets – Contract Exchange

Services vs Web Services

 

 

OOP Study Notes

Four Tenets of OOP

A small design problem

Analysis Model

Design Model – Static Model

Aggregation vs Composition

 

 

CLR Generics Study Notes

CLR Generics – Motivation

CLR Generics – Enter generics

CLR Generics – Terminology

CLR Generics – Constraints

CLR Generics – Other interesting trivia

CLR Generics – Benefits and limitations

CLR Generics – Internals

CLR Generics – Notable BCL Additions

CLR Generics – Comparing with other generics implementations

CLR Generics – Bibliography

 

 

Design Pattern Study Notes

Factory Method Pattern

Abstract Factory Pattern

Strategy Pattern

Decorator Pattern

Composite Pattern

Iterator Pattern

Template Method

Builder Pattern

Singleton Pattern

Mediator Pattern

Observer Pattern

Chain of Responsibility Pattern

Memento Pattern

Command Pattern

Prototype Pattern

State Pattern

Visitor Pattern

Flyweight Pattern

Interpreter Pattern

Facade Pattern

 

Answers to Scott Hanselman’s interview questions

Answers to Scott Hanselman’s interview questions

Scott Hanselman’s interview questions – Everyone who writes code

Scott Hanselman’s interview questions – Mid-Level .NET Developer

Scott Hanselman’s interview questions – Senior Developers/Architects

Scott Hanselman’s interview questions – C# Component Developers

 

 

Architecture Notes

Software Architecture

What does a software architect do?

Quality Attributes

Implementation based organization / decomposition

Layering

Layers vs. Tiers

 

 

CLR Study Notes

Delegates

Value Types and Reference Types

Pass by Value vs. Pass by Reference

‘Boxing’ and Unboxing