예제 #1
0
        /// <summary>
        /// Get one item in the 1-to-1 relationship.
        /// </summary>
        /// <typeparam name="TRelation">The type of the item in the 1-to-1 relationship.</typeparam>
        /// <param name="filter">Named filter.</param>
        /// <returns>The 1-to-1 relationship.</returns>
        protected virtual TRelation GetOne <TRelation>(string filter)
        {
            var repo = EntityRegistration.GetRepository <TRelation>();
            var item = repo.First(filter);

            return(item);
        }
        /// <summary>
        /// Gets the specified entity and loads the 1-to-Many TRelation. Defaults the foreign key to typeof(TRelation).Name + Id.
        /// Defaults the Property name to typeof(TRelation)"s".
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TRelation">The type of the relation.</typeparam>
        /// <param name="repo">The repo.</param>
        /// <param name="id">The id.</param>
        /// <param name="foreignKey">The foreign key.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public static T Get <T, TRelation>(this IRepository <T> repo, int id, string foreignKey, int pageIndex, int pageSize) where T : class where TRelation : class
        {
            T entity = repo.Get(id);

            if (entity == default(T))
            {
                return(entity);
            }

            // e.g. "CategoryId = id");
            var relationRepo            = EntityRegistration.GetRepository <TRelation>();
            IList <TRelation> relations = relationRepo.Find("where " + foreignKey + " = " + id);

            if (relations == null)
            {
                return(entity);
            }

            // e.g. Category.
            string propName = typeof(TRelation).Name + "s";

            typeof(T).GetProperty(propName).SetValue(entity, relations, null);
            return(entity);
        }