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

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Creating a Fluent API

Page history last edited by Scott White 13 years, 12 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.