/// <summary>
        /// Determines if a given type is configured to be change-tracked.
        /// </summary>
        /// <param name="someType">The whole type to check</param>
        /// <returns><literal>true</literal> if it can be tracked, <literal>false</literal> if it can't be tracked</returns>
        public bool CanTrackType(Type someType)
        {
            Contract.Requires(() => someType != null, "Given type cannot be null");

            lock (_syncLock)
                return(TrackableTypes.Any(t => t.Type == someType.GetActualTypeIfTrackable()));
        }
        /// <summary>
        /// Gets a configured trackable type by type, or returns null if it's not already configured.
        /// </summary>
        /// <param name="type">The whole type to get its tracking configuration</param>
        /// <returns>The configured trackable type by type, or returns null if it's not already configured</returns>
        public ITrackableType GetTrackableType(Type type)
        {
            Contract.Requires(() => type != null, "Given type cannot be null");

            lock (_syncLock)
                return(TrackableTypes.SingleOrDefault(t => t.Type == type));
        }
示例#3
0
        public T AddObjectToTracking <T>(T item)
            where T : IObjectId
        {
            if (item != null)
            {
                var type = item.GetType();
                if (TrackableTypes.ContainsKey(type))
                {
                    var key = item.GetHashCode();

                    if (Trackers.ContainsKey(item.Id))
                    {
                        return((T)Trackers[item.Id].LiveObj);
                    }
                    else
                    {
                        this.Set(type).AddToLoaded(item);
                        var currentTracker = new Tracker <T>(item);
                        this.Trackers.Add(item.Id, currentTracker);
                        return(item);
                    }
                }
            }
            return(default(T));
        }
        public IObjectChangeTrackingConfiguration TrackThisTypeRecursive(Type rootType, Action <IConfigurableTrackableType> configure = null, TypeSearchSettings searchSettings = null)
        {
            TrackableType trackableRoot = new TrackableType(this, rootType);

            ConfigureWithAttributes(trackableRoot);
            List <TrackableType> trackableTypes = null;

            searchSettings = searchSettings ?? DefaultSearchSettings;

            if (searchSettings.Filter == null)
            {
                searchSettings.Filter = t => t.GetTypeInfo().Assembly == rootType.GetTypeInfo().Assembly;
            }

            if (searchSettings.Mode == TypeSearchMode.AttributeConfigurationOnly)
            {
                Func <Type, bool> initialFilter = searchSettings.Filter;
                searchSettings.Filter = t => initialFilter(t) && t.GetTypeInfo().GetCustomAttribute <ChangeTrackableAttribute>() != null;
            }

            trackableTypes = new List <TrackableType>
                             (
                rootType.GetAllPropertyTypesRecursive(p => p.PropertyType.GetTypeInfo().IsClass&& searchSettings.Filter(p.PropertyType)).Select
                (
                    t =>
            {
                TrackableType trackableType = new TrackableType(this, t);
                ConfigureWithAttributes(trackableType);
                configure?.Invoke(trackableType);

                return(trackableType);
            }
                )
                             );

            trackableTypes.Insert(0, new TrackableType(this, rootType));

            foreach (ITrackableType trackableType in trackableTypes)
            {
                if (!trackableType.Type.GetTypeInfo().IsInterface)
                {
                    TrackableTypes.Add(trackableType);
                }
                else
                {
                    TrackableInterfaceTypes.Add(trackableType);
                }
            }

            return(this);
        }
        public IObjectChangeTrackingConfiguration TrackThisType(Type type, Action <IConfigurableTrackableType> configure = null)
        {
            TrackableType trackableType = new TrackableType(this, type);

            ConfigureWithAttributes(trackableType);
            configure?.Invoke(trackableType);

            if (!trackableType.Type.GetTypeInfo().IsInterface)
            {
                TrackableTypes.Add(trackableType);
            }
            else
            {
                TrackableInterfaceTypes.Add(trackableType);
            }

            return(this);
        }
        /// <summary>
        /// Configures which types will support change tracking.
        /// </summary>
        /// <param name="types">The types to track its changes</param>
        public void TrackTheseTypes(params ITrackableType[] types)
        {
            Contract.Requires(() => types != null && types.Length > 0 && types.All(t => t != null), "Given types cannot be null");

            lock (_syncLock)
            {
                foreach (ITrackableType type in types)
                {
                    if (!type.Type.GetTypeInfo().IsInterface)
                    {
                        Contract.Assert(() => TrackableTypes.Add(type), "Type can only be configured to be tracked once");
                    }
                    else
                    {
                        IConfigurableTrackableType trackableType = type as IConfigurableTrackableType;
                        trackableType.IncludeProperties(type.Type.GetProperties());

                        Contract.Assert(() => TrackableInterfaceTypes.Add(type), "Interface type can only be configured to be tracked once");
                    }
                }
            }
        }