Ejemplo n.º 1
0
        /// <summary>
        /// 依据列表数据的关联Id,获取关联的记录并填充到相应的属性值。
        /// </summary>
        public static void FillProperty <TSource, TDTO>(this IReadOnlyCollection <TSource> list, Expression <Func <TSource, TDTO> > expressionProp,
                                                        Func <TSource, long> funcPropId, Func <IReadOnlyCollection <long>, List <TDTO> > qryPropObj) where TDTO : IGeneralEntity
        {
            if (list.IsNullOrEmpty())
            {
                return;
            }

            var objList = qryPropObj(list.Select(funcPropId).ToList());

            if (objList.IsNullOrEmpty())
            {
                return;
            }

            var setter = TypeExtend.GetPropertySetter(expressionProp);

            foreach (var item in list)
            {
                var obj = objList.ById(funcPropId(item));
                if (obj != null)
                {
                    setter(item, obj);
                }
            }
        }
Ejemplo n.º 2
0
        /*internal static PropertyMeta<TEnt, TProp> By(Expression<Func<TEnt, TProp>> expGet)
         * {
         *  var prop = ((MemberExpression) expGet.Body).Member as PropertyInfo;
         *  return new PropertyMeta<TEnt, TProp>(prop)
         *  {
         *      _getter = expGet.Compile()
         *  };
         * }*/

        public PropertyMeta(PropertyInfo prop, PropMetaAttribute attr)
        {
            Name    = prop.Name;
            AttrSet = attr;

            Getter = TypeExtend.GetPropertyGetter <TEnt, TProp>(prop);
            Setter = TypeExtend.GetPropertySetter <TEnt, TProp>(prop);
        }
Ejemplo n.º 3
0
        public override void Update(SysModule item, bool saveImmediately = true)
        {
            var editModel = CurrentDbSet.Find(item.MId);

            TypeExtend <SysModule> .CopyTo(item, editModel);

            if (saveImmediately)
            {
                SaveChanges();
            }
        }
Ejemplo n.º 4
0
        TEntity ConvertImgPathToPhysicalPath(TEntity entity)
        {
            var result = Activator.CreateInstance <TEntity>();

            TypeExtend <TEntity> .CopyTo(entity, result, true);

            properties.Where(filter).ToList().ForEach(property =>
            {
                property.SetValue(result, floderPath + property.GetValue(entity, null).ToString());
            });
            return(result);
        }
Ejemplo n.º 5
0
        public virtual void Update(T item, bool saveImmediately = true)
        {
            var type       = typeof(T);
            var propertyId = type.GetProperty("Id");
            var Id         = propertyId.GetValue(item);
            var editModel  = CurrentDbSet.Find(Id);

            TypeExtend <T> .CopyTo(item, editModel);

            if (saveImmediately)
            {
                SaveChanges();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 查找API
        /// </summary>
        /// <param name="type"></param>
        private void FindApi(Type type)
        {
            var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance
                                          | BindingFlags.Public
                                          | BindingFlags.NonPublic);

            foreach (var method in methods)
            {
                var route = method.GetCustomAttribute <RouteAttribute>();
                if (route == null)
                {
                    continue;
                }
                var info = new ApiActionInfo
                {
                    FunctionName = method.Name,
                    RouteName    = route.Name
                };
                var pars = method.GetParameters();
                if (method.GetParameters().Length == 0)
                {
                    info.HaseArgument = false;
                    info.Action       = TypeExtend.CreateFunc <IApiResult>(type.GetTypeInfo(), method.Name, method.ReturnType.GetTypeInfo());
                }
                else if (method.GetParameters().Length == 1)
                {
                    info.HaseArgument   = true;
                    info.ArgumentAction = TypeExtend.CreateFunc <IApiArgument, IApiResult>(type.GetTypeInfo(), method.Name, pars[0].ParameterType.GetTypeInfo(), method.ReturnType.GetTypeInfo());
                }
                else
                {
                    continue;
                }
                var accessOption = method.GetCustomAttribute <ApiAccessOptionFilterAttribute>();
                if (accessOption != null)
                {
                    info.AccessOption = accessOption.Option;
                }
                ApiItems.Add(info.RouteName, info);
            }
        }
Ejemplo n.º 7
0
        public JsonResult GetList(GridPager pager, string queryStr)
        {
            TEntity[] list;
            if (!string.IsNullOrEmpty(queryStr))
            {
                list = TypeExtend <TEntity> .SearchListByString(Service.Get().OrderBy("Id").ToList(), queryStr)
                       .Skip((pager.page - 1) * pager.rows).Take(pager.rows).ToArray();
            }
            else
            {
                list = Service.Get().OrderBy("Id").Skip((pager.page - 1) * pager.rows).Take(pager.rows).ToArray();
            }

            var json = new
            {
                total = pager.totalRows,
                rows  = ConvertImgPathToPhysicalPath(list)
            };

            return(Json(json, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 查找API
        /// </summary>
        /// <param name="type"></param>
        /// <param name="onlyDoc"></param>
        private void FindApi(Type type, bool onlyDoc)
        {
            StationDocument station;
            var             sa = type.GetCustomAttribute <StationAttribute>();

            if (sa != null)
            {
                if (!StationInfo.TryGetValue(sa.Name, out station))
                {
                    StationInfo.Add(sa.Name, station = new StationDocument
                    {
                        Name = sa.Name
                    });
                }
            }
            else
            {
                station = DefStation;
            }
            var xdoc = XmlMember.Find(type);
            //station.Copy(XmlMember.Find(type));
            string routeHead = null;
            var    attrib    = type.GetCustomAttribute <RouteAttribute>();

            if (attrib != null)
            {
                routeHead = attrib.Name;
            }

            if (string.IsNullOrWhiteSpace(routeHead))
            {
                routeHead = null;
            }
            else
            {
                routeHead = routeHead.Trim(' ', '\t', '\r', '\n', '/') + "/";
            }

            var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance
                                          | BindingFlags.Public
                                          | BindingFlags.NonPublic);

            foreach (var method in methods)
            {
                if (method.GetParameters().Length > 1)
                {
                    ZeroTrace.WriteError("ApiDiscover", "argument size must 0 or 1", station.Name, type.Name, method.Name);
                    continue;
                }
                var route = method.GetCustomAttribute <RouteAttribute>();
                if (route == null && !method.IsPublic)
                {
                    ZeroTrace.WriteError("ApiDiscover", "exclude", station.Name, type.Name, method.Name);
                    continue;
                }
                var name = route?.Name == null
                    ? $"{routeHead}{method.Name}"
                    : $"{routeHead}{route.Name.Trim(' ', '\t', '\r', '\n', '/')}";
                var accessOption = method.GetCustomAttribute <ApiAccessOptionFilterAttribute>();
                var ca           = method.GetAttribute <CategoryAttribute>();
                var api          = new ApiActionInfo
                {
                    Name         = method.Name,
                    RouteName    = name,
                    Category     = ca?.Category ?? xdoc.Caption,
                    Controller   = type.FullName,
                    AccessOption = accessOption != null ? accessOption.Option : ApiAccessOption.Public | ApiAccessOption.Anymouse | ApiAccessOption.ArgumentCanNil,
                    ResultInfo   = ReadEntity(method.ReturnType, "result")
                };
                station.Aips.Add(api.RouteName, api);
                var doc = XmlMember.Find(type, method.Name, "M");
                api.Copy(doc);

                var arg = method.GetParameters().FirstOrDefault();
                api.HaseArgument = arg != null;
                //动态生成并编译
                if (api.HaseArgument)
                {
                    api.ArgumentInfo      = ReadEntity(arg.ParameterType, "argument") ?? new TypeDocument();
                    api.ArgumentInfo.Name = arg.Name;
                    if (doc != null)
                    {
                        api.ArgumentInfo.Caption = doc.Arguments.Values.FirstOrDefault();
                    }

                    if (!onlyDoc)
                    {
                        api.ArgumenType    = arg.ParameterType;
                        api.ArgumentAction = TypeExtend.CreateFunc <IApiArgument, IApiResult>(type.GetTypeInfo(),
                                                                                              method.Name,
                                                                                              arg.ParameterType.GetTypeInfo(),
                                                                                              method.ReturnType.GetTypeInfo());
                    }
                }
                else if (!onlyDoc)
                {
                    api.Action = TypeExtend.CreateFunc <IApiResult>(type.GetTypeInfo(), method.Name, method.ReturnType.GetTypeInfo());
                }
            }
        }