Fluent interfaces and inheritance in C#

后端 未结 7 1739
野性不改
野性不改 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条回答
  •  Happy的楠姐
    2020-11-30 21:41

    Logically you need to configure stuff from most specific (customer) to least specific (person) or otherwise it is even hard to read it despite the fluent interface. Following this rule in most cases you won't need get into trouble. If however for any reason you still need to mix it you can use intermediate emphasizing statements like

    static class Customers
    {
       public static Customer AsCustomer(this Person person)
       {
           return (Customer)person;
       }
    }
    
    customer.WIthLastName("Bob").AsCustomer().WithId(10);
    

提交回复
热议问题