///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Sets calculated property source.
        /// </summary>
        /// <param name="propertyName">
        ///  (Optional) Name of the property.
        /// </param>
        ///-------------------------------------------------------------------------------------------------
        protected void SetCalculatedPropertySource([CallerMemberName] string propertyName = null)
        {
            var domain = this.DomainModel; // Check domain model

            var tracker = Session.Current as ISupportsCalculatedPropertiesTracking;

            if (tracker != null)
            {
                var calculatedProperty = tracker.CurrentTracker;
                if (calculatedProperty != null)
                {
                    CalculatedProperty sourceProperty = null;
                    _calculatedProperties.Value.TryGetValue(propertyName, out sourceProperty);

                    if (sourceProperty == null)
                    {
                        sourceProperty = new CalculatedProperty(propertyName);
                        _calculatedProperties.Value.Add(propertyName, sourceProperty);
                    }


                    sourceProperty.AddTarget(calculatedProperty);
                }
            }
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Calculated property.
        /// </summary>
        /// <typeparam name="T">
        ///  Generic type parameter.
        /// </typeparam>
        /// <param name="calculation">
        ///  The calculation.
        /// </param>
        /// <param name="propertyName">
        ///  Name of the property.
        /// </param>
        /// <returns>
        ///  A T.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        protected T CalculatedProperty <T>(Func <T> calculation, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
        {
            Contract.RequiresNotEmpty(propertyName, "propertyName");
            Contract.Requires(calculation, "calculation");


            if (!(this is INotifyPropertyChanged) || (_schema.Schema.Behavior & DomainBehavior.Observable) != DomainBehavior.Observable)
            {
                return(calculation());
            }

            SetCalculatedPropertySource(propertyName);

            var session = EnsuresRunInSession(true);

            try
            {
                var tracker = (session ?? Session.Current) as ISupportsCalculatedPropertiesTracking;
                Debug.Assert(tracker != null);

                CalculatedProperty calculatedProperty = null;
                _calculatedProperties.Value.TryGetValue(propertyName, out calculatedProperty);

                if (calculatedProperty == null)
                {
                    calculatedProperty = new CalculatedProperty(propertyName);
                    _calculatedProperties.Value.Add(propertyName, calculatedProperty);
                }
                if (calculatedProperty.Handler == null)
                {
                    calculatedProperty.Handler = () => ((IPropertyChangedNotifier)this).NotifyPropertyChanged(propertyName);
                }

                using (tracker.PushCalculatedPropertyTracker(calculatedProperty))
                {
                    return(calculatedProperty.GetResult(calculation));
                }
            }
            finally
            {
                if (session != null)
                {
                    session.AcceptChanges();
                    session.Dispose();
                }
            }
        }
Beispiel #3
0
        IDisposable ISupportsCalculatedPropertiesTracking.PushCalculatedPropertyTracker(CalculatedProperty tracker)
        {
            var ctx = SessionDataContext;

            if (ctx == null)
            {
                return(Disposables.Empty);
            }

            if (ctx.Trackers == null)
            {
                ctx.Trackers = new Stack <CalculatedProperty>();
            }

            ctx.Trackers.Push(tracker);


            return(Disposables.ExecuteOnDispose(() => SessionDataContext.Trackers.Pop()));
        }
Beispiel #4
0
 internal void AddTarget(CalculatedProperty calculatedProperty)
 {
     _targets.Add(calculatedProperty);
 }