More fluent C# / .NET

后端 未结 21 1182
名媛妹妹
名媛妹妹 2020-12-23 22:51

A co-worker of mine came up with this and I wonder what others think? Personally, I find it interesting but wonder if it is too big a departure? Code examples below. Extensi

相关标签:
21条回答
  • 2020-12-23 23:54

    In certain circumstances thoughtfully constructed fluent interfaces can be very useful. First, because the developer is presented with a limited number of options they are (typically) easy to use correctly and difficult to use incorrectly. Second, because of the sentence like structure they can be a nice clean way to declare your intentions, especially when building complex objects.

    I have found fluent interfaces to be very useful when developing test code in which it is often necessary to build lots of domain objects with slight variations. I have also used them successfully as a way to introduce the decorator pattern and to eliminate excessive method overloading.

    If anyone is interested in learning more about fluent interfaces, I suggest checking out this work in progress by Martin Fowler.

    0 讨论(0)
  • 2020-12-23 23:55

    I am more comfortable with the first version. It takes less time to read and understand. I agree that the extension methods are also fine if you are familiar with it and also familiar with the With method, but what’s the benefit of it in this case?

    0 讨论(0)
  • 2020-12-23 23:55

    I'd say stick with the first version. What you've posted is too clever to be immediately useful to someone reading the code.

    You could even go a step further and do away with that "var":

    DropDownList ddl = (DropDownList) Page.FindControl("ddlName");
    
    0 讨论(0)
提交回复
热议问题