コード例 #1
0
ファイル: Tracker.cs プロジェクト: anakic/Jot
        /// <summary>
        /// Gets or creates a tracking configuration for the specified type. Objects of the
        /// specified type will be tracked according to the settings that are defined in the
        /// configuration object.
        /// </summary>
        public TrackingConfiguration Configure(Type t)
        {
            TrackingConfiguration configuration;

            if (_typeConfigurations.ContainsKey(t))
            {
                // if a config for this exact type exists return it
                configuration = _typeConfigurations[t];
            }
            else
            {
                // todo: we should make a config for each base type recursively, in case at a later point we add config for a base type
                // tbd : should configurations delegate work to base classes, rather than copying their config data?
                // if a config for this exact type does not exist, copy from base type's config or create a blank one
                var baseConfig = FindConfiguration(t);
                if (baseConfig != null)
                {
                    configuration = new TrackingConfiguration(baseConfig, t, _logger);
                }
                else
                {
                    configuration = new TrackingConfiguration(this, t, _logger);
                }
                _typeConfigurations[t] = configuration;
            }
            return(configuration);
        }
コード例 #2
0
ファイル: Tracker.cs プロジェクト: anakic/Jot
        /// <summary>
        /// Gets or creates a tracking configuration for the target object.
        /// </summary>
        public TrackingConfiguration Configure(object target)
        {
            TrackingConfiguration config;

            if (_configurationsDict.TryGetValue(target, out TrackingConfiguration cfg))
            {
                config = cfg;
            }
            else
            {
                config = Configure(target.GetType());

                // if the object or the caller want to customize the config for this type, copy the config so they don't mess with the config for the type
                if (target is ITrackingAware)
                {
                    config = new TrackingConfiguration(config, target.GetType(), _logger);

                    // allow the object to adjust the configuration
                    if (target is ITrackingAware ita)
                    {
                        ita.ConfigureTracking(config);
                    }
                }

                _configurationsDict.Add(target, config);
            }
            return(config);
        }
コード例 #3
0
ファイル: Tracker.cs プロジェクト: dragonfavors/Jot
        public TrackingConfiguration <T> Configure <T>()
        {
            TrackingConfiguration <T> configuration;

            if (_typeConfigurations.ContainsKey(typeof(T)))
            {
                // if a config for this exact type exists return it
                configuration = (TrackingConfiguration <T>)_typeConfigurations[typeof(T)];
            }
            else
            {
                // if a config for this exact type does not exist, copy from base type's config or create a blank one
                var baseConfig = FindConfiguration(typeof(T));
                if (baseConfig != null)
                {
                    configuration = new TrackingConfiguration <T>(baseConfig);
                }
                else
                {
                    configuration = new TrackingConfiguration <T>(this);
                }
                _typeConfigurations[typeof(T)] = configuration;
            }
            return(configuration);
        }
コード例 #4
0
ファイル: Tracker.cs プロジェクト: dragonfavors/Jot
        public void Track <T>(T target)
        {
            if (!_configurationsDict.TryGetValue(target, out _))
            {
                // find a configuration for this type of the nearest base type, or create a new one
                ITrackingConfigurationInternal config = Configure <T>();

                // if the object or the caller want to customize the config for this type, copy the config so they don't mess with the config for the type
                if (target is ITrackingAware <T> )
                {
                    config = new TrackingConfiguration <T>(config);

                    // allow the object to adjust the configuration
                    if (target is ITrackingAware <T> ita)
                    {
                        ita?.ConfigureTracking((TrackingConfiguration <T>)config);
                    }
                }

                // keep track of the object
                _trackedObjects.Add(new WeakReference(target));
                _configurationsDict.Add(target, config);

                // apply any previously stored data
                config.Apply(target);
                // listen to persist trigger events
                config.StartTracking(target);
            }
        }
コード例 #5
0
        // todo: allow caller to configure
        public void Track <T>(T target, Action <T> configure = null, bool inheritConfig = true)
        {
            if (!_configurationsDict.TryGetValue(target, out _))
            {
                // find a configuration for this type of the nearest base type
                // Note: Do not use T as the type. The caller might pass the target as a base type
                TrackingConfiguration config = FindConfiguration(target.GetType()) ?? new TrackingConfiguration <T>(this);

                // if the object or the caller want to customize the config for this type, copy the config so they don't mess with the config for the type
                if (target is ITrackingAware <T> )
                {
                    config = new TrackingConfiguration <T>(config);

                    // allow the object to adjust the configuration
                    if (target is ITrackingAware <T> ita)
                    {
                        ita?.ConfigureTracking((TrackingConfiguration <T>)config);
                    }
                }

                // keep track of the object
                _trackedObjects.Add(new WeakReference(target));
                _configurationsDict.Add(target, config);

                // apply any previously stored data
                config.Apply(target);
                // listen to persist trigger events
                config.StartTracking(target);
            }
        }
コード例 #6
0
ファイル: StateTracker.cs プロジェクト: ncvanleeuwen/Jot
        /// <summary>
        /// Creates or retrieves the tracking configuration for the speficied object.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public TrackingConfiguration Configure(object target)
        {
            TrackingConfiguration config = FindExistingConfig(target);

            if (config == null)
            {
                _configurations.Add(config = new TrackingConfiguration(target, this));
            }
            return(config);
        }
コード例 #7
0
ファイル: Tracker.cs プロジェクト: anakic/Jot
        // this is internal to allow TrackingConfiguration to call it so
        // we can avoid the extra lookup (finding the configuration)
        internal void Track(object target, TrackingConfiguration config)
        {
            // apply any previously stored data
            config.Apply(target);

            // listen for persist requests
            config.StartTracking(target);

            // add to list of objects to track
            _trackedObjects.Add(new WeakReference(target));
        }
コード例 #8
0
ファイル: StateTracker.cs プロジェクト: vnvizitiu/Jot
        /// <summary>
        /// Gets or creates a configuration object what will control how the target object is going to be tracked (which properties, when to persist, when to apply, validation).
        /// For a given target object, always returns the same configuration instance.
        /// </summary>
        /// <param name="target">The object whose properties your want to track.</param>
        /// <returns>The tracking configuration object.</returns>
        public TrackingConfiguration Configure(object target)
        {
            TrackingConfiguration config = FindExistingConfig(target);

            if (config == null)
            {
                config = new TrackingConfiguration(target, this);
                var initializer = FindInitializer(target.GetType());
                initializer.InitializeConfiguration(config);
                _trackedObjects.Add(new WeakReference(target));
                _configurationsDict.Add(target, config);
            }
            return(config);
        }
コード例 #9
0
        /// <summary>
        /// Creates or retrieves the tracking configuration for the speficied object.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="identifier"></param>
        /// <returns></returns>
        public TrackingConfiguration Configure(object target, string identifier)
        {
            TrackingConfiguration config = FindExistingConfig(target);

            if (config == null)
            {
                config = new TrackingConfiguration(target, this);
                var initializer = FindInitializer(target.GetType());
                initializer.InitializeConfiguration(config);

                //if the identifier was specified explicitly, it has priority over what the initializer set as the key
                if (identifier != null)
                {
                    config.Key = identifier;
                }

                config.CompleteInitialization();

                _trackedObjects.Add(new WeakReference(target));
                _configurationsDict.Add(target, config);
            }
            return(config);
        }
コード例 #10
0
 /// <summary>
 /// Creates a new instance of TrackingOperationEventArgs.
 /// </summary>
 /// <param name="configuration">The TrackingConfiguration object that initiated the tracking operation.</param>
 /// <param name="property">The property that is being persisted or applied to.</param>
 /// <param name="value">The value that is being persited or applied.</param>
 public TrackingOperationEventArgs(TrackingConfiguration configuration, string property, object value)
 {
     Configuration = configuration;
     Property      = property;
     Value         = value;
 }