Home > Development > Fluent Builder Example

Fluent Builder Example


If you have a class that has a long constructor and want to allow the user to build the object bit by bit use a technique like shown below.

using System;

namespace FluentBuilderDemo
{

    public class Address
    {
        private readonly string _streetAddress;
        private readonly string _suburb;
        private readonly string _postalCode;
        private readonly string _city;
        private readonly string _state;
        private readonly string _country;

        public Address(string streetAddress, string suburb, string postalCode, string city,
            string state, string country)
        {
            _streetAddress = streetAddress;
            _suburb = suburb;
            _postalCode = postalCode;
            _city = city;
            _state = state;
            _country = country;
        }


        public string Country
        {
            get { return _country; }
        }

        public string State
        {
            get { return _state; }
        }

        public string City
        {
            get { return _city; }
        }

        public string PostalCode
        {
            get { return _postalCode; }
        }

        public string Suburb
        {
            get { return _suburb; }
        }

        public string StreetAddress
        {
            get { return _streetAddress; }
        }


        public override string ToString()
        {
            return string.Format("StreetAddress: {0}, Suburb: {1}, PostalCode: {2}, City: {3}, State: {4}, Country: {5}", _streetAddress, _suburb, _postalCode, _city, _state, _country);
        }
    }

    public class AddressBuilder
    {
        private string _streetAddress;
        private string _suburb;
        private string _postalCode;
        private string _city;
        private string _state;
        private string _country;

        public AddressBuilder AssignStreetAddress(string street)
        {
            _streetAddress = street;
            return this;
        }

        public AddressBuilder AssignSuburb(string suburb)
        {
            _suburb = suburb;
            return this;
        }

        public AddressBuilder AssignPostalCode(string code)
        {
            _postalCode = code;
            return this;
        }

        public AddressBuilder AssignCity(string city)
        {
            _city = city;
            return this;
        }

        public AddressBuilder AssignState(string state)
        {
            _state = state;
            return this;
        }

        public AddressBuilder AssignCountry(string country)
        {
            _country = country;
            return this;
        }

        public Address BuildAddres()
        {
            return new Address(_streetAddress,_suburb,_postalCode,_city,_state,_country);
        }
    }

    class Program
    {
        static void Main()
        {
            Address address = new AddressBuilder().AssignStreetAddress("23 Test St")
                                                  .AssignPostalCode("6000")
                                                  .AssignSuburb("Dalkeith")
                                                  .AssignCity("Perth")
                                                  .AssignState("WA")
                                                  .AssignCountry("Australia")
                                                  .BuildAddres();

            Console.WriteLine(address);
            Console.ReadKey();
        }
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: