예제 #1
0
        public static bool Populate <T>(this IDbRecord record, ref T entityToPopulate) where T : IDbEntity
        {
            var map = DbMapping.Get(typeof(T));

            if (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);
        }
예제 #2
0
        public static void UpdateFrom <T>(this IDbRecord record, T sourceEntity) where T : IDbEntity
        {
            if (sourceEntity == null)
            {
                return;
            }
            var map = DbMapping.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;
                }
            }
        }
예제 #3
0
        public void InitMapping(params Assembly[] assemblies)
        {
            DbMapping.Clear();

            foreach (var assembly in assemblies)
            {
                var query = assembly.DefinedTypes.Where(t => typeof(IDbEntity).GetTypeInfo().IsAssignableFrom(t));
                foreach (var type in query)
                {
                    DbMapping.Add(type.AsType());
                }
            }
        }
예제 #4
0
        public void Delete(T entity, bool autoSync = true)
        {
            var map = DbMapping.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);
        }