What fluent interfaces have you made or seen in C# that were very valuable? What was so great about them?

后端 未结 11 1360
-上瘾入骨i
-上瘾入骨i 2021-01-31 05:42

\"Fluent interfaces\" is a fairly hot topic these days. C# 3.0 has some nice features (particularly extension methods) that help you make them.

FYI, a fluent API means

11条回答
  •  感情败类
    2021-01-31 05:48

    SubSonic 2.1 has a decent one for the query API:

    DB.Select()
      .From()
      .Where(User.UserIdColumn).IsEqualTo(1)
      .ExecuteSingle();
    

    tweetsharp makes extensive use of a fluent API too:

    var twitter = FluentTwitter.CreateRequest()
                  .Configuration.CacheUntil(2.Minutes().FromNow())
                  .Statuses().OnPublicTimeline().AsJson();
    

    And Fluent NHibernate is all the rage lately:

    public class CatMap : ClassMap  
    {  
      public CatMap()  
      {  
        Id(x => x.Id);  
        Map(x => x.Name)  
          .WithLengthOf(16)  
          .Not.Nullable();  
        Map(x => x.Sex);  
        References(x => x.Mate);  
        HasMany(x => x.Kittens);  
      }  
    }  
    

    Ninject uses them too, but I couldn't find an example quickly.

提交回复
热议问题