Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# builder pattern fluent example

public class Person
    {
        public string Name { get; set; }
        public List<Job> Jobs { get; set; }
        public List<Phone> Phones { get; set; }
    }

    public class Phone
    {
        public string Number { get; set; }
        public string Usage { get; set; }
    }

    public class Job
    {
        public string CompanyName { get; set; }
        public int Salary { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var p = PersonBuilder
                .Create()
                    .WithName("My Name")
                    .HavingPhone("222-222-2222")
                        .WithUsage("CELL")
                    .HavingJob()
                        .WithCompanyName("First Company")
                        .WithSalary(100)
                    .HavingJob()
                        .WithCompanyName("Second Company")
                        .WithSalary(200)
                .Build();

            Console.WriteLine(JsonConvert.SerializeObject(p));
        }
    }

    public class PersonBuilder : IJobBuilder
    {
        protected Person Person;
        public PersonBuilder() { Person = new Person(); }
        public static PersonBuilder Create() => new PersonBuilder();
        public PersonBuilder WithName(string name)
        {
            Person.Name = name;
            return this;
        }

        public PersonBuilder HavingPhone(string phoneNumber)
        {
            // Need instance of phone
            return this;
        }

        public PersonBuilder WithUsage(string phoneUsage)
        {
            // Need instance of phone
            return this;
        }

        public IJobBuilder HavingJob()
        {
            // Need to create a job here and return it so that IJobBuilder methods work on specific instance right?
            return this;
        }

        public Person Build() => Person;

        public IJobBuilder WithCompanyName(string companyName)
        {
            // How do I set the company name if I don't have the job instance here
            job.CompanyName = companyName;
            return this;
        }

        public IJobBuilder WithSalary(int amount)
        {
            // How do I set the salary if I don't have a specific job instance here
            job.Salary = amount;
            return this;
        }
    }

    public interface IJobBuilder
    {
        IJobBuilder WithCompanyName(string companyName);
        IJobBuilder WithSalary(int salary);
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: check if object has parent unity 
Csharp :: JavaScriptSerializer() and convert to base64 
Csharp :: unity vector3 initialization 
Csharp :: httpclient getstringasync 
Csharp :: c# array to label 
Csharp :: indexing an array 
Csharp :: c# copy bidimensional array 
Csharp :: c# get out of for loop 
Csharp :: c# decimal to fixed 2 
Csharp :: check if list contains any empty element in c# 
Csharp :: c# read excel file columns using epplus 
Csharp :: how to instantiate and delete unity 
Csharp :: the underlying connection was closed nuget 
Csharp :: linq convert list to another list 
Csharp :: .net return result encoding as utf8 
Csharp :: how to show a first item in a combobox in c# winforms 
Csharp :: c# catch two exceptions in one block 
Csharp :: c# HttpResponseMessage postResponse = client.PostAsync 
Csharp :: get xml from url 
Csharp :: math in c# 
Csharp :: raycasting unity 
Csharp :: how to use var in c# 
Csharp :: calculate textbox value c# 
Csharp :: mongodb custom IIdGenerator 
Csharp :: disable button netbeans 
Csharp :: populate array from an XML file 
Csharp :: c# Least prime factor of numbers till n 
Csharp :: how to assign 2d physics material through script 
Csharp :: // Force WPF to render UI changes immediately with this magic line of code... 
Csharp :: array of objects c# 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =