Пример #1
0
 public Subscription(
     IObserver <BindingResultChange <TResult> > observer,
     IRootBindingPrototype <TGraph, TResult> prototype,
     TGraph graph)
 {
     _observer = observer;
     _binding  = prototype.Clone();
     _binding.ResultChanged += OnBindingResultChanged;
     _binding.Bind(graph);
 }
Пример #2
0
        private static void Main()
        {
            var person = new Person();

            // We can create a prototype root binding from the expression:

            IRootBindingPrototype <Person, bool> prototype =
                Binding.CreatePrototype((Person x) => x.Name == null | x.Name.First.Length <= 3);

            // From the prototype, we can clone new instances of the root binding:

            IRootBinding <Person, bool> binding = prototype.Clone();

            // The root binding provides a very simple API:

            binding.ResultChanged += (object _, BindingResultChange <bool> e) => Console.WriteLine(e);
            binding.Bind(person);

            person.Name = new Name("1234"); // True
            person.Name = new Name("1");    // False
            person.Name = null;             // True

            binding.Unbind();

            // We can also create an observable from an expression:

            var d = person.Observe(x => x.Age == null | (x.Age > 33 & x.Age < 35))
                    .Subscribe(x => Console.WriteLine(x));

            person.Age = 34;   // True
            person.Age = 36;   // False
            person.Age = null; // True

            d.Dispose();

            // We can also create an accessor func:

            Func <Person, BindingResult <bool> > accessor = prototype.ToFunc();

            Console.WriteLine(accessor(null));   // False
            Console.WriteLine(accessor(person)); // True
        }
Пример #3
0
        /// <summary>
        /// <para>Creates a function which returns the binding result when applied to the argument.</para>
        /// <para>The returned function is _not_ thread-safe.</para>
        /// </summary>
        /// <typeparam name="TGraph">The type of the object graph.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="prototype">The binding prototype.</param>
        /// <returns>An acessor function which returns the binding result (which is not thread-safe).</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="prototype"/> is <see langword="null"/>.
        /// </exception>
        public static Func <TGraph, BindingResult <TResult> > ToFunc <TGraph, TResult>(
            this IRootBindingPrototype <TGraph, TResult> prototype)
        {
            Requires.NotNull(prototype, nameof(prototype));

            var binding = prototype.Clone(BindingMode.OneTime);

            return(graph =>
            {
                try
                {
                    binding.Bind(graph);
                    return binding.Result;
                }
                finally
                {
                    binding.Unbind();
                }
            });
        }