示例#1
0
        /// <summary>
        /// Creates an observable sequence from a binding prototype.
        /// </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>
        /// <param name="graph">The object graph to bind to.</param>
        /// <returns>An observable sequence that produces a result when the binding result changes.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="graph"/> or <paramref name="prototype"/> is <see langword="null"/>.
        /// </exception>
        public static IObservable <BindingResultChange <TResult> > Observe <TGraph, TResult>(
            this TGraph graph,
            IRootBindingPrototype <TGraph, TResult> prototype)
        {
            Requires.NotNullAllowStructs(graph, nameof(graph));
            Requires.NotNull(prototype, nameof(prototype));

            return(new BindingObservable <TGraph, TResult>(prototype, graph));
        }
示例#2
0
 public Subscription(
     IObserver <BindingResultChange <TResult> > observer,
     IRootBindingPrototype <TGraph, TResult> prototype,
     TGraph graph)
 {
     _observer = observer;
     _binding  = prototype.Clone();
     _binding.ResultChanged += OnBindingResultChanged;
     _binding.Bind(graph);
 }
示例#3
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
        }
示例#4
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();
                }
            });
        }
示例#5
0
 public BindingObservable(IRootBindingPrototype <TGraph, TResult> prototype, TGraph graph)
 {
     _prototype = prototype;
     _graph     = graph;
 }