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

后端 未结 11 1209
-上瘾入骨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:51

    The Criteria API in NHibernate has a nice fluent interface which allows you to do cool stuff like this:

    Session.CreateCriteria(typeof(Entity))
        .Add(Restrictions.Eq("EntityId", entityId))
        .CreateAlias("Address", "Address")
        .Add(Restrictions.Le("Address.StartDate", effectiveDate))
        .Add(Restrictions.Disjunction()
            .Add(Restrictions.IsNull("Address.EndDate"))
            .Add(Restrictions.Ge("Address.EndDate", effectiveDate)))
        .UniqueResult();
    

提交回复
热议问题