Hacker News new | ask | show | jobs
by bob1029 1692 days ago
For C#, the only method chaining we really do today would be for LINQ (very heavy usage) and AspNetCore startup logic.

If you are super addicted to certain language sugar like object initializers, then wrapping some of your business logic (e.g. mappers) with extension methods is not a terrible path. E.g.:

  public Thing MyFavoriteThing => new Thing
  {
    Id = Guid.NewGuid(),
    TheThing = _someLocalProperty.SomeTransform().Trim(),
    MyOtherInitalizedFact = true
  };
The alternative would look something like:

  public Thing MyFavoriteThing()
  {
    var result = new Thing
    {
      Id = Guid.NewGuid(),
      TheThing = SomeTransform(_someLocalProperty),
      MyOtherInitializedFact = true
    };
    
    result.TheThing = Trim(theThing);
    return result;
  }