Fluent interfaces and inheritance in C#

后端 未结 7 1713
野性不改
野性不改 2020-11-30 21:09

I\'ll show a problem by example. There is a base class with fluent interface:

class FluentPerson
{
    private string _FirstName = String.Empty;
    private          


        
相关标签:
7条回答
  • 2020-11-30 21:59

    I know this is now an old question, but I wanted to share my thoughts about this with you.

    What about separating fluency, which is a kind of mechanism, and your classes, when you can ? This would leave your classes pure.

    What about something like this ?

    The classes

        public class Person
        {
            public string FirstName { get; set; }
            public string LastName {get; set;}
    
            public override string ToString()
            {
                return $"First name: {FirstName} last name: {LastName}";
            }
        }
    
        public class Customer : Person
        {
            public string AccountNumber { get; set; }
            public long Id { get; set; }
    
            public override string ToString()
            {
                return base.ToString() + $" account number: {AccountNumber} id: {Id}");
            }
        }
    

    A class that adds some fluent mechanism

        public class FluentCustomer 
        {
            private Customer Customer { get; }
    
            public FluentCustomer() : this(new Customer())
            {
            }
    
            private FluentCustomer(Customer customer)
            {
                Customer = customer;
            }
    
            public FluentCustomer WithAccountNumber(string accountNumber)
            {
                Customer.AccountNumber = accountNumber;
                return this;
            }
    
            public FluentCustomer WithId(long id)
            {
                Customer.Id = id;
                return this;
            }
    
            public FluentCustomer WithFirstName(string firstName)
            {
                Customer.FirstName = firstName;
                return this;
            }
    
            public FluentCustomer WithLastName(string lastName)
            {
                Customer.LastName = lastName;
                return this;
            }
    
            public static implicit operator Customer(FluentCustomer fc)
            {
                return fc.Customer;
            }
    
            public static implicit operator FluentCustomer(Customer customer)
            {
                return new FluentCustomer(customer);
            }
        }
    

    An extension method to switch to fluent mode

        public static class CustomerExtensions 
        {
    
            public static FluentCustomer Fluent(this Customer customer)
            {
                return customer;
            }
        }
    

    The same example as in question

    
            Customer customer = new Customer().Fluent()
                                .WithAccountNumber("000")
                                .WithFirstName("John")
                                .WithLastName("Smith")
                                .WithId(123);
    
    
    0 讨论(0)
提交回复
热议问题