| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Dokkio Sidebar (from the makers of PBworks) is a Chrome extension that eliminates the need for endless browser tabs. You can search all your online stuff without any extra effort. And Sidebar was #1 on Product Hunt! Check out what people are saying by clicking here.

View
 

Creating a Fluent API

Page history last edited by Scott White 12 years, 3 months ago

 

Resources: http://code.google.com/p/nasserter/

 

See Also: http://fluentvalidation.codeplex.com/ , http://nbuilder.org/ , 

 

 

    public static class Something

    {

        public static IConstraint<T> Has<T>(T something)

        {

            return new ConstraintImpl<T>(something);

        }

    }

 

    public class ConstraintImpl<T> : IConstraint<T>

    {

        private readonly T _something;

 

        public ConstraintImpl(T something)

        {

            _something = something;

        }

 

        public IConstraint<T> IsGreaterThan<TGt>(Func<T, TGt> callback, TGt greaterThan)

            where TGt : IComparable<TGt>

        {

            if (callback.Invoke(_something).CompareTo(greaterThan) >= 0) throw new ApplicationException("jkl");

 

            return this;

        }

 

        public IConstraint<T> IsTrue(Predicate<T> callback)

        {

            if (!callback(_something)) throw new ApplicationException("asdf");

 

            return this;

        }

    }

 

    public interface INestedConstraint<T, TParent>

    {

 

    }

 

    public interface IConstraint<T> : IHideObjectMembers

    {

        IConstraint<T> IsGreaterThan<TGt>(Func<T, TGt> callback, TGt greaterThan)

            where TGt : IComparable<TGt>;

 

        IConstraint<T> IsTrue(Predicate<T> x);

    }

 

 

 

            var customer = new Customer {Id = 200};

 

            Something.Has(customer)

                .IsTrue(x => x.Id == 200)

                .IsGreaterThan(x => x.Id, 199);

 

 

Comments (0)

You don't have permission to comment on this page.