public void CanConfigureAbstractClass()
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

            config.TrackTypesFromAssembly(typeof(ConfigurationTest).GetTypeInfo().Assembly, searchSettings: new TypeSearchSettings
            {
                Mode = TypeSearchMode.AttributeConfigurationOnly,
                /*Filter = t => t == typeof(AbstractClass) || t == typeof(DerivesAbstractClass),*/
                Recursive = true
            });

            ITrackableObjectFactory factory = config.CreateTrackableObjectFactory();
            var x = factory.CreateFrom(new DerivesAbstractClass());
        }
        public void CanConfigureClassWithMultipleConstructors()
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

            config.TrackTypesFromAssembly(typeof(ConfigurationTest).GetTypeInfo().Assembly, searchSettings: new TypeSearchSettings
            {
                Mode   = TypeSearchMode.AttributeConfigurationOnly,
                Filter = t => t == typeof(WithMultipleConstructors)
            });

            ITrackableObjectFactory factory = config.CreateTrackableObjectFactory();

            WithMultipleConstructors instance = factory.CreateFrom(new WithMultipleConstructors());

            Assert.IsNotNull(instance);
        }
        public void CanConfigureClassWithReadOnlyProperties()
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

            config.TrackTypesFromAssembly(typeof(ConfigurationTest).GetTypeInfo().Assembly, searchSettings: new TypeSearchSettings
            {
                Mode   = TypeSearchMode.AttributeConfigurationOnly,
                Filter = t => t == typeof(ClassWithReadOnlyProperties)
            });

            ITrackableObjectFactory factory = config.CreateTrackableObjectFactory();

            ClassWithReadOnlyProperties instance = factory.CreateFrom(new ClassWithReadOnlyProperties());

            instance.Text = "hey";

            IObjectChangeTracker tracker = instance.GetChangeTracker();

            Assert.AreEqual(1, tracker.ChangedProperties.Count);
            Assert.AreEqual(1, ((ObjectChangeTracker)tracker).PropertyTrackings.Count);
        }
        // TODO: Remove?
        /// <summary>
        /// Turns all objects into change-trackable ones found in the given sequence.
        /// </summary>
        /// <typeparam name="T">The type of objects</typeparam>
        /// <param name="enumerable">The sequence of objects to turn into change-trackable ones</param>
        /// <param name="parentObjectProperty">The collection property representing the association to some object</param>
        /// <param name="parentObject">The parent object that owns the association of the collection</param>
        /// <returns>The already converted objects into change-trackable ones</returns>
        public static IEnumerable <T> MakeAllTrackable <T>(this IEnumerable <T> enumerable, IObjectChangeTrackingConfiguration configuration, ITrackableObjectFactory trackableObjectFactory, PropertyInfo parentObjectProperty, IChangeTrackableObject parentObject)
            where T : class
        {
            Contract.Requires(() => enumerable != null, "Given enumerable must be a non-null reference to turn its objects into trackable ones");
            Contract.Requires(() => parentObjectProperty != null, "A reference to property which holds the enumerable is mandatory");
            Contract.Requires(() => parentObject != null, "The instance of the object where the property holding the enumerable is declared is mandatory");
            Contract.Requires(() => parentObjectProperty.DeclaringType.GetActualTypeIfTrackable().IsAssignableFrom(parentObject.GetActualTypeIfTrackable()), "Given property holding the enumerable must be declared on the given parent object type");

            if (enumerable.Count() > 0 &&
                configuration.CanTrackType(enumerable.First().GetType()))
            {
                List <T> result = new List <T>();

                foreach (T item in enumerable)
                {
                    IChangeTrackableObject trackableObject = item as IChangeTrackableObject;

                    if (trackableObject == null)
                    {
                        trackableObject = (IChangeTrackableObject)trackableObjectFactory.CreateFrom(item);

                        trackableObject.PropertyChanged += (sender, e) =>
                                                           parentObject.RaisePropertyChanged(parentObject, parentObjectProperty.Name);
                    }

                    result.Add((T)trackableObject);
                }

                Contract.Assert(() => result.Count == enumerable.Count(), "New sequence with objects turned into trackable ones must match the count of source sequence");

                return(result);
            }
            else
            {
                return(enumerable);
            }
        }
Exemple #5
0
        public void PerfTest(int n, int m)
        {
            var class0 = new Class0
            {
                Class1 = new Class1
                {
                    Class2s = new HashSet <Class2>(
                        Enumerable.Range(0, m).Select(i => new Class2 {
                        Number = i
                    })
                        )
                }
            };

            var proxyList = new List <Class0>();

            for (int i = 0; i < n; ++i)
            {
                var proxy = _trackableObjectFactory.CreateFrom(class0);
                proxyList.Add(proxy);
            }

            Assert.AreEqual(proxyList.Count, n);
        }