Ejemplo n.º 1
0
        public static void AssertRequiredPropertiesExist <Tentity, Tinput>(Func <DbContext> dbContextFactory, bool longRunningContext = false)
            where Tentity : class, new()
        {
            var check = RequiredPropertyCheckCache.FirstOrDefault(x => x.Item1 == typeof(Tentity) && x.Item2 == typeof(Tinput));

            if (check == null)
            {
                var context = dbContextFactory.Invoke();

                if (CollectMappingStatements)
                {
                    var    src       = typeof(Tinput).FullName.Replace("+", ".");
                    var    dst       = typeof(Tentity).FullName.Replace("+", ".");
                    string statement = string.Format("//Required Property Assertion for {0} ({1}) -> {2} ({3})\r\n    {4};\r\n",
                                                     src,
                                                     "ViewModel",
                                                     dst,
                                                     "Entity",
                                                     "global::FelineSoft.CatFlap.CatFlap.AssertRequiredPropertiesExist<global::" + dst + ",global::" + src + ">(() => new global::" + context.GetType().FullName.Replace("+", ".") + "())");
                    AssertionStatements.Add(statement);
                }

                var properties      = GetRequiredPropertyNames <Tentity>(context);
                var propertiesCopy  = properties.Clone();
                var inputProperties = typeof(Tinput).GetProperties().Select(x =>
                {
                    var attr = x.GetCustomAttributes(typeof(MapToAttribute), false)
                               .Cast <MapToAttribute>()
                               .FirstOrDefault(y => y.EntityType == typeof(Tentity));
                    if (attr == null)
                    {
                        return(x.Name);
                    }
                    else
                    {
                        return(attr.PropertyName);
                    }
                });

                var map = AutoMapper.Mapper.FindTypeMapFor <Tinput, Tentity>();
                foreach (var propMap in map.GetPropertyMaps())
                {
                    if (propertiesCopy.Any(x => x == propMap.DestinationProperty.MemberInfo.Name))
                    {
                        propertiesCopy.Remove(propMap.DestinationProperty.MemberInfo.Name);
                    }
                }

                var complies = propertiesCopy.Count == 0;

                //var complies = inputProperties.Where(x => properties.Contains(x)).Distinct().Count() == properties.Count();
                if (!complies)
                {
                    RequiredPropertyCheckCache.Add(new Tuple <Type, Type, bool, string>(typeof(Tentity), typeof(Tinput), false, properties.CommaList()));
                    throw new ApplicationException(string.Format("Required properties for {0} do not exist on {1}.\r\n\r\nRequired properties are: {2}", typeof(Tentity).FullName, typeof(Tinput).FullName, properties.CommaList()));
                }
                else
                {
                    RequiredPropertyCheckCache.Add(new Tuple <Type, Type, bool, string>(typeof(Tentity), typeof(Tinput), true, properties.CommaList()));
                }
                if (!longRunningContext)
                {
                    context.Dispose();
                }
            }
            else if (!check.Item3)
            {
                throw new ApplicationException(string.Format("Required properties for {0} do not exist on {1}.\r\n\r\nRequired properties are: {2}", typeof(Tentity).FullName, typeof(Tinput).FullName, check.Item4));
            }
        }
Ejemplo n.º 2
0
        public static void AssertKeysExist <Tentity, Tinput>(Func <DbContext> dbContextFactory, bool longRunningContext = false)
            where Tentity : class, new()
        {
            var check = PrimaryKeyCheckCache.FirstOrDefault(x => x.Item1 == typeof(Tentity) && x.Item2 == typeof(Tinput));

            if (check == null)
            {
                var context = dbContextFactory.Invoke();

                if (CollectMappingStatements)
                {
                    var    src       = typeof(Tinput).FullName.Replace("+", ".");
                    var    dst       = typeof(Tentity).FullName.Replace("+", ".");
                    string statement = string.Format("//Primary Key Assertion for {0} ({1}) -> {2} ({3})\r\n    {4};\r\n",
                                                     src,
                                                     "ViewModel",
                                                     dst,
                                                     "Entity",
                                                     "global::FelineSoft.CatFlap.CatFlap.AssertKeysExist<global::" + dst + ",global::" + src + ">(() => new global::" + context.GetType().FullName.Replace("+", ".") + "())");
                    AssertionStatements.Add(statement);
                }

                var keys = GetPrimaryKeyNames <Tentity>(context);

                var properties = typeof(Tinput).GetProperties().Select(x =>
                {
                    var attr = x.GetCustomAttributes(typeof(MapToAttribute), false)
                               .Cast <MapToAttribute>()
                               .FirstOrDefault(y => y.EntityType == typeof(Tentity));
                    if (attr == null)
                    {
                        return(x.Name);
                    }
                    else
                    {
                        return(attr.PropertyName);
                    }
                });
                var complies = properties.Where(x => keys.Contains(x)).Distinct().Count() == keys.Count();
                if (!complies)
                {
                    PrimaryKeyCheckCache.Add(new Tuple <Type, Type, bool, string>(typeof(Tentity), typeof(Tinput), false, keys.CommaList()));

                    throw new ApplicationException(string.Format("Primary keys for {0} do not exist on {1}.\r\n\r\nRequired primary key properties are: {2}", typeof(Tentity).FullName, typeof(Tinput).FullName, keys.CommaList()));
                }
                else
                {
                    PrimaryKeyCheckCache.Add(new Tuple <Type, Type, bool, string>(typeof(Tentity), typeof(Tinput), true, keys.CommaList()));
                }

                if (!longRunningContext)
                {
                    context.Dispose();
                }
            }
            else
            {
                if (!check.Item3)
                {
                    throw new ApplicationException(string.Format("Primary keys for {0} do not exist on {1}.\r\n\r\nRequired primary key properties are: {2}", typeof(Tentity).FullName, typeof(Tinput).FullName, check.Item4));
                }
            }
        }