1. 2011
    Jul
    28

    Method Chaining in C#

    Posted By


    c# method chaining

    Method chaining is a very common technique for invoking multiple method calls in object-oriented programming languages. The idea for this is very simple each method returns an object that allows the calls to be chained together into a single statement. This is effectively used in a number of JavaScript libraries, a good example is jQuery JavaScript Library.

    jQuery:

    
    
    $('#myelement').attr('class', 'editable').html('This is an added text');
    
    

    An important advantage of using method chaining in C# is to retrieve large amounts of data from databases. Method chaining allows you to create reusable sub queries that can be adapted to the different filters and conditional clauses.

    Say we have a database of employees in a company FakeComp
    and it has full-time employees and contract employees, the contract employees
    are from 2 agencies TempAngency1 and TempAgency2
    and you like to know which employees are “Females” from TempAgency2
    that work with FakeComp.

    C#:

    
    
    MyRepository myRepository = new MyRepository();
    IQueryable‹Employee› allTempAgency2Females = myRepository.FindAllEmployees().FindAllEmployeesByTempAgency('TempAgency2').FindAllEmployeesByGender('female');
    
    

    The best way I found of implementing this is to create methods from a static class that allows for method chaining. That way you can choose which method you would like to be included or excluded from your method chaining.

    C#:

    
    
    public class MyRepository : IMyRepository
    {
            private readonly DataContext _dataContext = new DataContext();
            public MyRepository()
            {
                new DataContext();
            }
    
            public MyRepository(DataContext dataContext)
            {
                _dataContext = dataContext;
            }
    
            public IQueryable‹Employee› FindAllEmployees()
            {
                return _dataContext.Employees;
            }
    }
    
    public static class MyRepositoryExtensions
    {
            public static IQueryable‹Employee› FindAllEmployeesByTempAgency(this IQueryable‹Employee› employees, string agency)
            {
                return from employee in employees where employee.Agency.Name.ToLower().Equals(agency.ToLower()) select employee;
            }
    
            public static IQueryable‹Employee› FindAllEmployeesByGender(this IQueryable‹Employee› employees, string gender)
            {
                return from employee in employees where employee.Gender.Name.ToLower().Equals(gender.ToLower()) select employee;
            }
      }
      
    

  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.