Esempio n. 1
0
        /// <summary>
        /// Gets the properties to treate durring an update action.
        /// </summary>
        /// <param name="typeParentObj">The type parent object.</param>
        /// <param name="param">The parameter.</param>
        /// <param name="addCollection">if set to <c>true</c> [add collection].</param>
        /// <param name="addRequiered">if set to <c>true</c> [add requiered].</param>
        /// <param name="excludeSimple">if set to <c>true</c> [exclude simple].</param>
        /// <returns>The list of properties to treate</returns>
        public static List <PropertyInfo> GetPropertiesToTreate(Type typeParentObj, GenericRepositoryParmeter param, bool addCollection = false, bool addRequiered = false, bool excludeSimple = false)
        {
            List <PropertyInfo> lstProp    = typeParentObj.GetProperties().ToList();
            List <PropertyInfo> retLstProp = new List <PropertyInfo>();

            if (param != null)
            {
                if (param.Values2Update != null)
                {
                    foreach (string propname in param.Values2Update)
                    {
                        retLstProp.Add(lstProp.Single(p => p.Name == propname));
                    }

                    if (addCollection)
                    {
                        foreach (PropertyInfo prop in lstProp)
                        {
                            if (prop.PropertyType.Name == "ICollection`1")
                            {
                                retLstProp.Add(prop);
                            }
                        }
                    }
                }
                else if (param.Values2Exclude != null)
                {
                    foreach (PropertyInfo prop in lstProp)
                    {
                        if (!param.Values2Exclude.Contains(prop.Name))
                        {
                            retLstProp.Add(prop);
                        }
                    }
                }
                else
                {
                    retLstProp = lstProp;
                }
            }
            else
            {
                retLstProp = lstProp;
            }

            return(retLstProp);
        }
Esempio n. 2
0
        public static T AddIfNotExistsOrModifyReference <T, ProjectDBContext, ProjectDBContainer>(this DbSet <T> dbSet, ProjectDBContainer dbContainer, T entity, Expression <Func <T, bool> > predicate, GenericRepositoryParmeter param)
            where T : ObjectRemap, new()
            where ProjectDBContext : DbContext, new()
            where ProjectDBContainer : TDBContainer <ProjectDBContext>, new()
        {
            var exists = dbSet.Any(predicate);
            T   ret    = null;

            if (!exists)
            {
                ret = dbSet.Add(entity);
            }
            else
            {
                IQueryable <T> list = dbSet.Where(predicate);
                if (list.Count() == 1)
                {
                    T item = list.First();
                    TGenericRepository <T, ProjectDBContext, ProjectDBContainer> repository = new TGenericRepository <T, ProjectDBContext, ProjectDBContainer>(dbContainer);
                    repository.RemapReferences(item, entity, param);
                }
                else
                {
                    throw new Exception("PB multi value in Initializer.");
                }
            }

            return(ret);
        }