public static void Init(TestContext context)
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

            config.TrackThisType <IWhatever>();

            TrackableObjectFactory = config.CreateTrackableObjectFactory();
        }
Exemple #2
0
        public void Initialize()
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

            config.TrackThisTypeRecursive <Class0>();

            _trackableObjectFactory = config.CreateTrackableObjectFactory();
        }
        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 CanConfigureWithNestedTypes()
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

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

            ITrackableObjectFactory factory = config.CreateTrackableObjectFactory();

            DerivedClassWithNestedEnum instance = factory.CreateOf <DerivedClassWithNestedEnum>();

            Assert.IsNotNull(instance);
        }
        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 static void Init(TestContext context)
        {
            Configuration = ObjectChangeTracking.CreateConfiguration()
                            .TrackThisType <A>(t => t.IncludeProperties(a => a.Text, a => a.B))
                            .TrackThisType <B>(t => t.IncludeProperties(b => b.Text, b => b.C))
                            .TrackThisType <C>(t => t.IncludeProperties(c => c.Text, c => c.ListOfD))
                            .TrackThisType <D>(t => t.IncludeProperty(d => d.Text))
                            .TrackThisType <E>(t => t.IncludeProperties(e => e.Text, e => e.Number))
                            .TrackThisType <Customer>(t => t.IncludeProperty(c => c.ContactInfo))
                            .TrackThisType <Contact>(t => t.IncludeProperty(c => c.Name))
                            .TrackThisType <EnhancedContact>(t => t.IncludeProperty(c => c.Default))
                            .TrackThisType <ClassWithReadOnlyPropertyThrowingException>()
                            .TrackThisType <ClassSettingPropertiesDuringConstructionTime>(t => t.IncludeProperty(c => c.Text));

            TrackableObjectFactory = Configuration.CreateTrackableObjectFactory();
        }
        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);
        }
        public void CanConfigureNonTrackablePropertiesFromBaseClasses()
        {
            IObjectChangeTrackingConfiguration config = ObjectChangeTracking.CreateConfiguration();

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

            ITrackableObjectFactory trackableObjectFactory      = config.CreateTrackableObjectFactory();
            DerivedClassOfBaseClassWithNonTrackableProperty obj = trackableObjectFactory.CreateOf <DerivedClassOfBaseClassWithNonTrackableProperty>();

            IObjectChangeTracker changeTracker = obj.GetChangeTracker();

            Assert.AreEqual(1, changeTracker.Count());
            Assert.IsTrue(changeTracker.First().PropertyName == "Text2");
        }
        // 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);
            }
        }
 public CollectionPropertyInterceptor(IObjectChangeTrackingConfiguration configuration, ITrackableObjectFactory trackableObjectFactory)
 {
     Configuration          = configuration;
     TrackableObjectFactory = trackableObjectFactory;
 }
Exemple #11
0
 public DynamicObjectInterceptor(IObjectChangeTrackingConfiguration configuration, ITrackableObjectFactory trackableObjectFactory)
 {
     Configuration          = configuration;
     TrackableObjectFactory = trackableObjectFactory;
 }