Beispiel #1
0
        /// <summary>
        /// Asynchronously finds an object based on key after an include has been done.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async static Task <T> FindItemAsync <T>(this IQueryable <T> query, object id)
        {
            var classViewModel = ReflectionRepository.GetClassViewModel(typeof(T));

            if (classViewModel.PrimaryKey.Type.IsString)
            {
                return(await query.Where($@"{classViewModel.PrimaryKey.Name} = ""{id}""").FirstAsync());
            }
            else
            {
                return(await query.Where(string.Format("{0} = {1}", classViewModel.PrimaryKey.Name, id)).FirstAsync());
            }
        }
Beispiel #2
0
 protected BaseApiController()
 {
     // Set up a ViewModel so we can check out this object.
     ClassViewModel = ReflectionRepository.GetClassViewModel(typeof(T), null, ApiName);
     if (typeof(T) == typeof(TDto) || typeof(TDto).Name.EndsWith("DtoGen"))
     {
         DtoViewModel = ClassViewModel;
     }
     else
     {
         DtoViewModel = ReflectionRepository.GetClassViewModel(typeof(TDto), null, ApiName);
     }
 }
Beispiel #3
0
        public void ModelViewAttributes()
        {
            ReflectionRepository.AddContext <AppDbContext>();

            var caseProduct = ReflectionRepository.GetClassViewModel <CaseProduct>();
            var person      = ReflectionRepository.GetClassViewModel <Person>();

            Assert.NotNull(caseProduct);

            Assert.True(person.WillCreateApiController);
            Assert.True(person.WillCreateViewController);

            Assert.False(caseProduct.WillCreateViewController);
            Assert.False(caseProduct.WillCreateViewController);
        }
Beispiel #4
0
        /// <summary>
        /// Includes immediate children, as well as the other side of many-to-many relationships.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public static IQueryable <T> IncludeChildren <T>(this IQueryable <T> query) where T : class, new()
        {
            var model = ReflectionRepository.GetClassViewModel <T>();

            foreach (var prop in model.Properties.Where(f => !f.IsStatic && !f.IsInternalUse && f.PureType.HasClassViewModel && f.PureType.ClassViewModel.HasDbSet && !f.HasNotMapped))
            {
                if (prop.IsManytoManyCollection)
                {
                    query = query.IncludeString(prop.Name + "." + prop.ManyToManyCollectionProperty.Name);
                }
                else
                {
                    query = query.IncludeString(prop.Name);
                }
            }
            return(query);
        }
Beispiel #5
0
        /// <summary>
        /// Gets a list of all the complex types used in the models.
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public IEnumerable <ClassViewModel> ComplexTypes(List <ClassViewModel> models)
        {
            Dictionary <string, ClassViewModel> complexTypes = new Dictionary <string, ClassViewModel>();

            foreach (var model in models)
            {
                foreach (var prop in model.Properties.Where(f => f.IsComplexType))
                {
                    if (!complexTypes.ContainsKey(prop.Name))
                    {
                        var ctModel = ReflectionRepository.GetClassViewModel(prop.Type);
                        complexTypes.Add(prop.Name, ctModel);
                    }
                }
            }

            return(complexTypes.Values);
        }
Beispiel #6
0
        /// <summary>
        /// Includes sub objects from the graph based on IIncludeExternal method on class.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="include"></param>
        /// <returns></returns>
        public static IEnumerable <T> IncludesExternal <T>(this IEnumerable <T> query, string includes = null) where T : class, new()
        {
            T   obj  = new T();
            var objT = obj as IIncludeExternal <T>;

            if (objT != null)
            {
                return(objT.IncludeExternal(query, includes));
            }
            else
            {
                var model = ReflectionRepository.GetClassViewModel <T>();
                foreach (var prop in model.Properties.Where(f => !f.IsInternalUse && f.IsExternal))
                {
                    // TODO: need to figure out how to do this without a <T>
                    //query = query.IncludeExternal(prop);
                }
            }
            return(query);
        }
Beispiel #7
0
        public void ModelView()
        {
            ReflectionRepository.AddContext <AppDbContext>();

            var person = ReflectionRepository.GetClassViewModel <Person>();

            Assert.NotNull(person);

            Assert.True(person.PropertyByName(nameof(Person.Name)).IsListText);
            Assert.NotNull(person.PropertyByName(nameof(Person.LastName)).MinLength);
            Assert.Equal(3, person.PropertyByName(nameof(Person.LastName)).MinLength.Value);

            Assert.NotNull(person.PropertyByName("BirthDate"));
            Assert.NotNull(person.PropertyBySelector <Person, DateTime?>(f => f.BirthDate));
            Assert.NotNull(person.PropertyByName(nameof(Person.BirthDate)));
            Assert.True(person.PropertyByName(nameof(Person.BirthDate)).Type.IsDate);
            Assert.True(person.PropertyByName(nameof(Person.BirthDate)).Type.IsDateTime);
            Assert.False(person.PropertyByName(nameof(Person.BirthDate)).Type.IsDateTimeOffset);

            Assert.True(person.PropertyByName(nameof(Person.BirthDate)).IsDateOnly);
        }
Beispiel #8
0
 public static HtmlString StandardBinding <T>()
 {
     return(StandardBinding(ReflectionRepository.GetClassViewModel <T>()));
 }
Beispiel #9
0
        public void Person()
        {
            var person = ReflectionRepository.GetClassViewModel <Person>();

            Assert.NotNull(person);
        }