示例#1
0
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsExpressionTrees.ExpressionListRemoveElementWithGivenKeys``1(System.Object[])"]/*'/>
        public static Expression <Func <T, bool> > ExpressionListRemoveElementWithGivenKeys <T>(params object[] objs)
        {
            var        param = Expression.Parameter(typeof(T));
            Expression body  = Expression.IsFalse(Expression.Equal(
                                                      Expression.Property(
                                                          param,
                                                          typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0])
                                                          ),
                                                      Expression.Constant(
                                                          objs[0],
                                                          typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]).PropertyType
                                                          )
                                                      ));

            for (int i = 1; i < objs.Length; i++)
            {
                body = Expression.Or(
                    body,
                    Expression.IsFalse(Expression.Equal(
                                           Expression.Property(param, typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i])),
                                           Expression.Constant(
                                               objs[i],
                                               typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]).PropertyType
                                               )
                                           ))
                    );
            }
            // t => t.key1 != value1 || t.key2 != value2 ...
            return(Expression.Lambda <Func <T, bool> >(body, param));
        }
 ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.ListWherePropKeysAre``2(System.Collections.Generic.List{``1},System.String,System.Object[])"]/*'/>
 private static List <Q> ListWherePropKeysAre <T, Q>(List <Q> req, string propname, object[] objs)
 {
     GenericToolsTypeAnalysis.CheckIfObjectIsKey <T>(objs);
     if (typeof(T).IsSubclassOf(typeof(BaseEntity)))
     {
         int?id = GenericToolsTypeAnalysis.ObjectsToId <T>(objs);
         req = req.Where(
             GenericToolsExpressionTrees.ExpressionPropPropEquals <Q>(
                 typeof(Q).GetProperty(propname),
                 typeof(T).GetProperty("Id"),
                 id
                 ).Compile()
             ).ToList();
     }
     else
     {
         int i = 0;
         foreach (object obj in objs)
         {
             req = req.Where(
                 GenericToolsExpressionTrees.ExpressionPropPropEquals <Q>(
                     typeof(Q).GetProperty(propname),
                     typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]),
                     obj
                     ).Compile()
                 ).ToList();
             i++;
         }
     }
     return(req);
 }
 ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.QueryWhereKeysAre``1(System.Linq.IQueryable{``0},System.Object[])"]/*'/>
 public static IQueryable <T> QueryWhereKeysAre <T>(IQueryable <T> req, params object[] objs)
 {
     GenericToolsTypeAnalysis.CheckIfObjectIsKey <T>(objs);
     if (typeof(T).IsSubclassOf(typeof(BaseEntity)))
     {
         int?id = GenericToolsTypeAnalysis.ObjectsToId <T>(objs);
         req = req.Where(
             GenericToolsExpressionTrees.PropertyEquals <T>(
                 typeof(T).GetProperty("Id"),
                 id
                 )
             );
     }
     else
     {
         int i = 0;
         foreach (object obj in objs)
         {
             req = req.Where(
                 GenericToolsExpressionTrees.PropertyEquals <T>(
                     typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]),
                     obj
                     )
                 );
             i++;
         }
     }
     return(req);
 }
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsCRUD.PrepareSave``1(``0)"]/*'/>
        public static object[] PrepareSave <T>(T t)
        {
            IEnumerable <Type>        TypesForWhichTHasManyProperties = GenericToolsTypeAnalysis.GetTypesForWhichTHasManyProperties <T>();
            Dictionary <string, Type> dynamicDBTypes     = GenericToolsTypeAnalysis.DynamicDBTypes <T>();
            Dictionary <string, Type> dynamicDBListTypes = GenericToolsTypeAnalysis.DynamicDBListTypes <T>();

            object[] res = new object[dynamicDBTypes.Count + dynamicDBListTypes.Count];
            var      i   = 0;

            foreach (PropertyInfo prop in typeof(T).GetProperties())
            {
                if (GenericToolsTypeAnalysis.DynamicDBTypes <T>().Keys.Contains(prop.Name))
                {
                    if (TypesForWhichTHasManyProperties.Contains(prop.PropertyType))
                    {
                        if (prop.GetValue(t) == null)
                        {
                            res[i] = new PropToNull(prop.Name);
                        }
                        else
                        {
                            res[i] = prop.GetValue(t);
                        }
                    }
                    else
                    {
                        res[i] = prop.GetValue(t);
                    }
                }
                else
                {
                    if (GenericToolsTypeAnalysis.DynamicDBListTypes <T>().Keys.Contains(prop.Name))
                    {
                        if (TypesForWhichTHasManyProperties.Contains(GenericToolsTypeAnalysis.DynamicDBListTypes <T>()[prop.Name]))
                        {
                            if (prop.GetValue(t) == null ||
                                (prop.GetValue(t) as IList).Count == 0)
                            {
                                res[i] = new PropToNull(prop.Name);
                            }
                            else
                            {
                                res[i] = prop.GetValue(t);
                            }
                        }
                        else
                        {
                            res[i] = prop.GetValue(t);
                        }
                    }
                    else
                    {
                        i--;
                    }
                }
                i++;
            }
            return(res);
        }
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.QueryTIncludeTracked``1(System.Data.Entity.DbContext)"]/*'/>
        public static IQueryable <T> QueryTIncludeTracked <T>(DbContext dbContext) where T : class
        {
            IQueryable <T> req = dbContext.Set <T>().AsQueryable();

            foreach (string name in GenericToolsTypeAnalysis.DynamicDBListTypes <T>().Keys.ToList())
            {
                req = req.Include(name);
            }
            foreach (string name in GenericToolsTypeAnalysis.DynamicDBTypes <T>().Keys.ToList())
            {
                req = req.Include(name);
            }
            return(req);
        }
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.QueryDefaultOrderBy``1(System.Linq.IQueryable{``0})"]/*'/>
        public static IQueryable <T> QueryDefaultOrderBy <T>(IQueryable <T> req)
        {
            string defaultPropertyName;

            if (typeof(BaseEntity).IsAssignableFrom(typeof(T)))
            {
                defaultPropertyName = "Id";
            }
            else
            {
                defaultPropertyName = GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0];
            }

            return(QueryOrderByKey(req, defaultPropertyName));
        }
 ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsCRUD.PrepareDelete``1(System.Data.Entity.DbContext,System.Object[])"]/*'/>
 public static void PrepareDelete <T>(DbContext context, params object[] objs)
 {
     foreach (Type type in GenericToolsTypeAnalysis.GetTypesInRelationWithTHavingTPropertyTAndTNotHavingProperty <T>())
     {
         GenericToolsCRUDPrep.DeleteOtherPropInRelationWithTHavingTPropertyTAndTNotHavingProperty <T>(context, type, objs);
     }
     foreach (Type type in GenericToolsTypeAnalysis.GetTypesInRelationWithTHavingRequiredTProperty <T>())
     {
         GenericToolsCRUDPrep.DeleteOtherPropInRelationWithTHavingRequiredTProperty <T>(context, type, objs);
     }
     foreach (Type type in GenericToolsTypeAnalysis.GetTypesForWhichTHasManyProperties <T>())
     {
         GenericToolsCRUDPrep.DeleteOtherPropInSeveralRelationshipsWithT <T>(context, type, objs);
     }
 }
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.ListRemoveElementWithGivenKeys``1(System.Collections.Generic.List{``0},System.Object[])"]/*'/>
        public static List <T> ListRemoveElementWithGivenKeys <T>(List <T> req, params object[] objs)
        {
            GenericToolsTypeAnalysis.CheckIfObjectIsKey <T>(objs);
            Func <T, bool> func;

            if (typeof(BaseEntity).IsAssignableFrom(typeof(T)))
            {
                int id = (int)objs[0];
                func = GenericToolsExpressionTrees.ExpressionListRemoveElementWithGivenId <T>(id).Compile();
            }
            else
            {
                func = GenericToolsExpressionTrees.ExpressionListRemoveElementWithGivenKeys <T>(objs).Compile();
            }
            return(req.Where(func).ToList());
        }
 ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsCRUD.PrepareUpdateOne``1(System.Data.Entity.DbContext,``0,System.String)"]/*'/>
 public static void PrepareUpdateOne <T>(DbContext context, T t, string propertyName)
 {
     foreach (Type type in GenericToolsTypeAnalysis.GetTypesInRelationWithTHavingRequiredTProperty <T>())
     {
         Dictionary <string, string> propnames = GenericToolsTypeAnalysis.PropNamesForRelationWithTWithRequired(typeof(T), type);
         if (propnames.Count() != 0)
         {
             for (int j = 0; j < propnames.Count(); j++)
             {
                 if (propnames.Keys.ToList()[j] == propertyName)
                 {
                     GenericToolsCRUDPrep.UpdateOtherPropInRelationWithTHavingRequiredTProperty(context, type, t, propnames.Keys.ToList()[j], propnames[propnames.Keys.ToList()[j]]);
                 }
             }
         }
     }
 }
示例#10
0
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsExpressionTrees.ExpressionWhereKeysAre``1(System.Object[])"]/*'/>
        public static Expression <Func <T, bool> > ExpressionWhereKeysAre <T>(params object[] objs)
        {
            var        param = Expression.Parameter(typeof(T));
            Expression body;

            if (typeof(BaseEntity).IsAssignableFrom(typeof(T)))
            {
                int id = (int)objs[0];
                body = Expression.Equal(Expression.Property(
                                            param,
                                            typeof(T).GetProperty("Id")
                                            ),
                                        Expression.Constant(
                                            id,
                                            typeof(T).GetProperty("Id").PropertyType
                                            ));
            }
            else
            {
                body = Expression.Equal(Expression.Property(
                                            param,
                                            typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0])
                                            ),
                                        Expression.Constant(
                                            objs[0],
                                            typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]).PropertyType
                                            ));
                for (int i = 1; i < objs.Length; i++)
                {
                    body = Expression.And(body,
                                          Expression.Equal(Expression.Property(
                                                               param,
                                                               typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i])
                                                               ),
                                                           Expression.Constant(
                                                               objs[i],
                                                               typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]).PropertyType)
                                                           ));
                }
            }
            return(Expression.Lambda <Func <T, bool> >(body, param));
        }
示例#11
0
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsExpressionTrees.ExpressionListWhereOtherTypePropListNotContains``2(``0,System.String,System.Int32)"]/*'/>
        public static Expression <Func <Q, bool> > ExpressionListWhereOtherTypePropListNotContains <T, Q>(T newItem, string tPropName, int nbr = 1)
        {
            var        param      = Expression.Parameter(typeof(Q));
            var        newItemcst = Expression.Constant(newItem, typeof(T));
            Expression body       = Expression.Property(
                newItemcst,
                typeof(T).GetProperty(tPropName)
                );

            var        param2 = Expression.Parameter(typeof(Q));
            Expression exp2;

            if (typeof(BaseEntity).IsAssignableFrom(typeof(Q)))
            {
                exp2 = Expression.Equal(
                    Expression.Property(
                        param2,
                        typeof(Q).GetProperty("Id")
                        ),
                    Expression.Property(
                        param,
                        typeof(Q).GetProperty("Id")
                        )
                    );
            }
            else
            {
                exp2 = Expression.Equal(
                    Expression.Property(
                        param2,
                        typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[0])
                        ),
                    Expression.Property(
                        param,
                        typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[0])
                        )
                    );
                for (int i = 1; i < nbr; i++)
                {
                    exp2 = Expression.And(
                        exp2,
                        Expression.Equal(
                            Expression.Property(
                                param2,
                                typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[i])
                                ),
                            Expression.Property(
                                param,
                                typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[i])
                                )
                            )
                        );
                }
            }

            MethodInfo methodWhere = typeof(Enumerable).GetMethods()
                                     .Where(m =>
                                            m.Name == "Where"
                                            )
                                     .ToList()[0]
                                     .MakeGenericMethod(typeof(Q));


            body = Expression.Call(
                methodWhere,
                body,
                Expression.Lambda <Func <Q, bool> >(exp2, param2)
                );

            MethodInfo methodCount = typeof(Enumerable).GetMethods()
                                     .Where(m =>
                                            m.Name == "Count" &&
                                            m.GetParameters().Length == 1
                                            )
                                     .Single()
                                     .MakeGenericMethod(typeof(Q));

            body = Expression.Call(methodCount, body);

            body = Expression.Equal(body, Expression.Constant(1));

            body = Expression.Not(body);

            return(Expression.Lambda <Func <Q, bool> >(body, param));
        }
示例#12
0
        ///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsCRUD.PrepareUpdate``1(System.Data.Entity.DbContext,``0)"]/*'/>
        public static object[] PrepareUpdate <T>(DbContext context, T t)
        {
            foreach (Type type in GenericToolsTypeAnalysis.GetTypesInRelationWithTHavingRequiredTProperty <T>())
            {
                Dictionary <string, string> propnames = GenericToolsTypeAnalysis.PropNamesForRelationWithTWithRequired(typeof(T), type);
                if (propnames.Count() != 0)
                {
                    for (int j = 0; j < propnames.Count(); j++)
                    {
                        GenericToolsCRUDPrep.UpdateOtherPropInRelationWithTHavingRequiredTProperty <T>(context, type, t, propnames.Keys.ToList()[j], propnames[propnames.Keys.ToList()[j]]);
                    }
                }
            }
            IEnumerable <Type>        TypesForWhichTHasManyProperties = GenericToolsTypeAnalysis.GetTypesForWhichTHasManyProperties <T>();
            Dictionary <string, Type> dynamicDBTypes     = GenericToolsTypeAnalysis.DynamicDBTypes <T>();
            Dictionary <string, Type> dynamicDBListTypes = GenericToolsTypeAnalysis.DynamicDBListTypes <T>();

            object[] res = new object[dynamicDBTypes.Count + dynamicDBListTypes.Count];
            var      i   = 0;

            foreach (PropertyInfo prop in typeof(T).GetProperties())
            {
                if (GenericToolsTypeAnalysis.DynamicDBTypes <T>().Keys.Contains(prop.Name))
                {
                    if (TypesForWhichTHasManyProperties.Contains(prop.PropertyType))
                    {
                        if (prop.GetValue(t) == null)
                        {
                            res[i] = new PropToNull(prop.Name);
                        }
                        else
                        {
                            res[i] = prop.GetValue(t);
                        }
                    }
                    else
                    {
                        res[i] = prop.GetValue(t);
                    }
                }
                else
                {
                    if (GenericToolsTypeAnalysis.DynamicDBListTypes <T>().Keys.Contains(prop.Name))
                    {
                        if (TypesForWhichTHasManyProperties.Contains(GenericToolsTypeAnalysis.DynamicDBListTypes <T>()[prop.Name]))
                        {
                            if (prop.GetValue(t) == null ||
                                (prop.GetValue(t) as IList).Count == 0)
                            {
                                res[i] = new PropToNull(prop.Name);
                            }
                            else
                            {
                                res[i] = prop.GetValue(t);
                            }
                        }
                        else
                        {
                            res[i] = prop.GetValue(t);
                        }
                    }
                    else
                    {
                        i--;
                    }
                }
                i++;
            }
            return(res);
        }