Esempio n. 1
0
        public void PropertiesCache_GetPropertiesOf_ShouldCacheProperties()
        {
            var stopwatch1 = new Stopwatch();

            stopwatch1.Start();
            for (var i = 0; i < 1000000; i++)
            {
                typeof(TestClass2).GetProperties();
            }
            stopwatch1.Stop();

            var cache2     = new PropertiesCache();
            var stopwatch2 = new Stopwatch();

            stopwatch2.Start();
            for (var i = 0; i < 1000000; i++)
            {
                cache2.GetPropertiesOf <TestClass2>();
            }
            stopwatch2.Stop();

            Console.WriteLine(stopwatch1.Elapsed);
            Console.WriteLine(stopwatch2.Elapsed);
            Assert.IsTrue(stopwatch1.Elapsed > stopwatch2.Elapsed, $"Uncached: {stopwatch1.Elapsed} vs. cached: {stopwatch2.Elapsed}.");
        }
Esempio n. 2
0
        /// <summary>
        /// Populates the auto-populated columns of the entity and returns a list of attributes that were created / updated
        /// </summary>
        private static void PopulateAutoPopulatedAttributes <T>(LocalCrmDatabaseOrganizationService service, T entity, bool isCreate) where T : Entity
        {
            var properties = PropertiesCache.For <T>();
            var info       = service.Info;
            var name       = GetFullName(info,
                                         entity.GetAttributeValue <string>(SystemUser.Fields.FirstName),
                                         entity.GetAttributeValue <string>(SystemUser.Fields.MiddleName),
                                         entity.GetAttributeValue <string>(SystemUser.Fields.LastName));

            // TODO: Need to add logic to see if an update to the Full Name is being Performed
            ConditionallyAddValue(entity, properties, SystemUser.Fields.FullName, name, !string.IsNullOrWhiteSpace(name));

            AutoPopulateContactFields(entity, properties);
            AutoPopulateOpportunityFields(service, entity, isCreate);

            if (isCreate)
            {
                SetAttributeId(entity);
                SetStatusToActiveForCreate(entity);
                SetOwnerForCreate(service, entity, properties);
                ConditionallyAddValue(entity, properties, Email.Fields.CreatedBy, info.User, info.User.GetIdOrDefault() != Guid.Empty);
                ConditionallyAddValue(entity, properties, Email.Fields.CreatedOnBehalfBy, info.UserOnBehalfOf, info.UserOnBehalfOf.GetIdOrDefault() != Guid.Empty);
                ConditionallyAddValue(entity, properties, Email.Fields.CreatedOn, entity.Contains(Email.Fields.OverriddenCreatedOn) ? entity[Email.Fields.OverriddenCreatedOn] : DateTime.UtcNow);
                ConditionallyAddValue(entity, properties, Email.Fields.OwningBusinessUnit, info.BusinessUnit, !entity.Contains(Email.Fields.OwningBusinessUnit) && info.BusinessUnit.GetIdOrDefault() != Guid.Empty);
            }
            else if (entity.Contains(Email.Fields.OwnerId))
            {
                UpdateOwningFieldsBasedOnOwner(service, entity, properties);
            }

            PopulateModifiedAttributes(service.Info, entity, properties);
        }
Esempio n. 3
0
        public void PropertiesCache_GetPropertiesOf_ShouldReturnCorrectNumberOfProperties()
        {
            var cache = new PropertiesCache();

            var properties = cache.GetPropertiesOf <TestClass1>();

            Assert.AreEqual(3, properties.Length);
        }
Esempio n. 4
0
        private static void UpdateActivityPointer <T>(LocalCrmDatabaseOrganizationService service, T entity) where T : Entity
        {
            if (entity.LogicalName == ActivityPointer.EntityLogicalName || !PropertiesCache.For <T>().IsActivityType)
            {
                return; // Type is already an activity pointer, no need to re-update
            }

            service.Update(GetActivityPointerForActivityEntity(entity));
        }
Esempio n. 5
0
        public void PropertiesCache_GetPropertiesOf_ShouldReturnCorrectProperties()
        {
            var cache = new PropertiesCache();

            var properties = cache.GetPropertiesOf <TestClass1>();

            Assert.IsTrue(properties.Any(p => p.Name == "Property1"));
            Assert.IsTrue(properties.Any(p => p.Name == "Property2"));
            Assert.IsTrue(properties.Any(p => p.Name == "Property3"));
        }
Esempio n. 6
0
        private static void CreateActivityParties <T>(LocalCrmDatabaseOrganizationService service, T entity) where T : Entity
        {
            if (entity.LogicalName == ActivityParty.EntityLogicalName ||
                entity.LogicalName == ActivityPointer.EntityLogicalName ||
                !PropertiesCache.For <T>().IsActivityType)
            {
                return;
            }

            CreateActivityPartiesFromPartyLists(service, entity);
            CreateActivityPartiesFromSingleFields(service, entity);
        }
Esempio n. 7
0
        public void PropertiesCache_GetPropertiesOf_ResultShouldEqualTypeGetProperties()
        {
            var cache = new PropertiesCache();

            var cachedProperties = cache.GetPropertiesOf <TestClass1>();
            var typeProperties   = typeof(TestClass1).GetProperties();

            Assert.AreEqual(typeProperties.Length, cachedProperties.Length);
            foreach (var typeProperty in typeProperties)
            {
                Assert.IsTrue(cachedProperties.Contains(typeProperty));
            }
        }
Esempio n. 8
0
        protected IEnumerable <T> Search <T>(string searchText, int page, int pageSize, object condition = null) where T : new ()
        {
            var ConditionDictionary = null != condition?InitConditionSearchFields(condition) : new Dictionary <string, string>();

            var fullTextSearchFields = from propName in PropertiesCache.Select(t => t.Key)
                                       where !ConditionDictionary.ContainsKey(propName)
                                       select propName;

            using (var luceneDB = new LuceneDBSearcher(GetType()))
            {
                return(luceneDB.Search <T>(searchText, fullTextSearchFields, page, pageSize, ConditionDictionary));
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Creates the activity pointer if the Entity is an Activity Type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="service">The service.</param>
        /// <param name="entity">The entity.</param>
        private static void CreateActivityPointer <T>(LocalCrmDatabaseOrganizationService service, T entity) where T : Entity
        {
            if (EntityHelper.GetEntityLogicalName <T>() == ActivityPointer.EntityLogicalName)
            {
                return; // Type is already an activity pointer, no need to recreated
            }

            if (!PropertiesCache.For <T>().IsActivityType)
            {
                return; // Type is not an activity type
            }

            // Copy over matching values and create
            service.Create(GetActivityPointerForActivityEntity(entity));
        }
Esempio n. 10
0
        private static Entity GetActivityPointerForActivityEntity <T>(T entity) where T : Entity
        {
            var pointerFields = typeof(ActivityPointer.Fields).GetFields();
            var pointer       = new Entity(ActivityPointer.EntityLogicalName)
            {
                Id = entity.Id
            };

            foreach (var att in pointerFields.Where(p => PropertiesCache.For <T>().ContainsProperty(p.Name))
                     .Select(field => field.GetRawConstantValue().ToString()).Where(entity.Contains))
            {
                pointer[att] = entity[att];
            }

            return(pointer);
        }
Esempio n. 11
0
        public void PropertiesCache_GetPropertiesOf_TypeIsNull_ShouldThrowArgumentNullException()
        {
            var cache = new PropertiesCache();

            cache.GetPropertiesOf(null !);
        }
Esempio n. 12
0
        private static void DeleteActivityPointer <T>(LocalCrmDatabaseOrganizationService service, Guid id) where T : Entity
        {
            if (EntityHelper.GetEntityLogicalName <T>() == ActivityPointer.EntityLogicalName || !PropertiesCache.For <T>().IsActivityType)
            {
                return; // Type is already an activity pointer, no need to redelete, or type is not an activity type
            }

            service.Delete(ActivityPointer.EntityLogicalName, id);
        }
Esempio n. 13
0
 public static void ClearCache(Type[]?_)
 {
     PropertiesCache.Clear();
     VisiblePropertiesCache.Clear();
 }
Esempio n. 14
0
 /// <summary>
 /// 获取支持 <see cref="RedisValue"/>包装的运行时属性
 /// 支持 <see cref="Type.IsValueType"/> 和 <see cref="T:string"/>
 /// </summary>
 /// <remarks>
 /// 支持类型:
 /// - string
 /// - DateTime
 /// - int
 /// - uint
 /// - double
 /// - byte[]
 /// - bool
 /// - long
 /// - ulong
 /// - float
 /// - ReadOnlyMemory&gt;byte&lt;
 /// - Memory&gt;byte&lt;
 /// - RedisValue
 /// </remarks>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 private static PropertyInfo[] GetProperties <T> ()
 {
     return(PropertiesCache <T> .GetProperties());
 }
Esempio n. 15
0
 public static IDictionary <string, object> ToDictionary(this object item)
 {
     return(PropertiesCache.GetProperties(item).ToDictionary(prop => prop.Name, prop => prop.GetValue(item, null)));
 }