Пример #1
0
        private static IRequiredDataResponse VerifyPreConditions <T>(Entity entity, CRUD.CrudTypes crudFlags)
        {
            Ensure.ArgumentNotNull(nameof(entity), entity);

            var response = new RequiredDataResponse {
                IsError = !VerifyEntity <T>(crudFlags)
            };

            if (!response.IsError)
            {
                return(VerifyEntityProperties(entity, crudFlags));
            }

            response.Message = GetMessageForEntity <T>(crudFlags);
            return(response);
        }
Пример #2
0
        //TODO: Break this up into smaller methods
        private static IRequiredDataResponse VerifyEntityPropertiesForCreate(Entity entity)
        {
            Ensure.ArgumentNotNull(nameof(entity), entity);

            var response      = new RequiredDataResponse();
            var entityType    = entity.GetType();
            var propertyEntry = entityType.PropertyNamesWithAttribute <RequiredForCreateAttribute>(Properties.IsEnabled, true);

            foreach (var entry in propertyEntry)
            {
                var propertyValue = entityType.GetProperty(entry.Key)?.GetValue(entity, null);
                if (propertyValue == null)
                {
                    response.IsError = true;
                    response.MissingPropertyNames.Add(entry.Key);
                }
                else
                {
                    // check if property is an object that has required fields.
                    if (propertyValue is Entity)
                    {
                        // check the returned list
                        var items       = entry.Value;
                        var subItemList = new Collection <string>();
                        var isSubError  = false;
                        foreach (var item in items)
                        {
                            var subItemValue    = entityType.GetProperty(item)?.GetValue(propertyValue, null);
                            var isNullOrDefault = false;
                            // first check nullable, for any type
                            var propertyType = entityType.GetProperty(item)?.PropertyType;
                            if (propertyType != null)
                            {
                                if (propertyType == typeof(string) ||
                                    propertyType.IsGenericType &&
                                    propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                {
                                    isNullOrDefault = (subItemValue == null);
                                }
                                else
                                {
                                    var objectType = Activator.CreateInstance(propertyType);
                                    isNullOrDefault = objectType.Equals(subItemValue);
                                }
                            }
                            if (isNullOrDefault)
                            {
                                isSubError = true;
                                subItemList.Add(item);
                            }
                        }
                        if (isSubError)
                        {
                            response.IsError = true;
                            response.MissingSubPropertyNames.Add(entry.Key, subItemList);
                        }
                    }
                }
            }
            return(response);
        }