Beispiel #1
0
        public static void UpdateFrom <T>(this IMvxDBRecord record, T sourceEntity) where T : IMvxDBEntity
        {
            if (sourceEntity == null)
            {
                return;
            }
            var map = MvxDBMapping.Get(sourceEntity.GetType());

            if (map == null || map.PropertiesInfos == null || map.PropertiesInfos.Count == 0)
            {
                return;
            }
            foreach (var property in map.PropertiesInfos)
            {
                var value = property.GetValue(sourceEntity);
                if (value == null)
                {
                    continue;
                }
                if (record[property.Name] != value)
                {
                    record[property.Name] = value;
                }
            }
        }
Beispiel #2
0
        public static IMvxDBRecord GetMvxDBRecord <TEntityType>(this TEntityType entity) where TEntityType : IMvxDBEntity
        {
            var fields = Mvx.Resolve <IMvxDBRecord>();

            if (entity == null)
            {
                return(null);
            }
            var map = MvxDBMapping.Get(entity.GetType());

            if (map == null || map.PropertiesInfos == null || map.PropertiesInfos.Count == 0)
            {
                return(null);
            }
            foreach (var property in map.PropertiesInfos)
            {
                var value = property.GetValue(entity);
                if (value == null)
                {
                    continue;
                }
                fields[property.Name] = value;
            }
            return(fields);
        }
Beispiel #3
0
        public static bool Populate <T>(this IMvxDBRecord record, ref T entityToPopulate) where T : IMvxDBEntity
        {
            var map = MvxDBMapping.Get(typeof(T));

            if (map == null || map.PropertiesInfos == null || map.PropertiesInfos.Count == 0)
            {
                return(false);
            }
            var result = false;
            //Property Key
            var keyValue = map.PropertyInfoKey.GetValue(entityToPopulate) as string;

            if (keyValue != record.Id)
            {
                map.PropertyInfoKey.SetValue(entityToPopulate, record.Id);
                result = true;
            }
            //Others
            foreach (var fieldName in record.FieldNames)
            {
                var property = map.PropertiesInfos.FirstOrDefault(p => p.Name == fieldName);
                if (property != null)
                {
                    var oldValue = property.GetValue(entityToPopulate);
                    var value    = record[fieldName];
                    if (oldValue != value)
                    {
                        property.SetValue(entityToPopulate, value.ConvertValue(property.PropertyType));
                        result = true;
                    }
                }
            }
            return(result);
        }
Beispiel #4
0
        public void InitMapping(Assembly assembly)
        {
            var query = assembly.GetTypes().Where(t => typeof(IMvxDBEntity).IsAssignableFrom(t));

            foreach (var type in query)
            {
                MvxDBMapping.Add(type);
            }
        }
Beispiel #5
0
        public void Delete(T entity, bool autoSync = true)
        {
            var map = MvxDBMapping.Get(typeof(T));

            if (map.PropertyInfoKey == null)
            {
                new KeyNotFoundException(string.Format("Type: {0} does not have MvxDBKey attribute", typeof(T).Name));
            }
            var value = map.PropertyInfoKey.GetValue(entity);

            if (value == null)
            {
                new NullReferenceException(string.Format("Type:{0}: Property key {1} is null", typeof(T).Name, map.PropertyInfoKey.Name));
            }
            Delete(entity, value.ToString(), autoSync);
        }