Exemple #1
0
        /// <summary>
        /// automatically transform T to TDto,according to property-flatten pattern like automapper.(Use Caption to identiyfy words)
        /// </summary>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="pager"></param>
        /// <param name="where"></param>
        /// <param name="sort"></param>
        /// <param name="propNameMap">use this to manually set mapping propname of DTO to prop of DO.words are delimetered by '|'
        /// </param>
        /// <returns></returns>
        public virtual IList <TDto> GetDtoList <TDto>(Pager pager, Expression <Func <T, bool> > where = null, string sort = null, IDictionary <string, string> propNameMap = null)
        {
            //following to make lamda like o=>new DTO{Id=o.Id,Other=o.Other}}
            var props = typeof(TDto).GetProperties().Where(o => o.CanWrite).ToArray();

            MemberBinding[] propExprs     = null;
            var             parameterExpr = Expression.Parameter(typeof(T), "o");
            var             newDtoExpr    = Expression.New(typeof(TDto));//new TDto;

            Array.Resize(ref propExprs, props.Count());
            for (var i = 0; i < props.Count(); i++)//Id=o.Id;ReferenceId=o.Reference??null:o.Reference.Id
            {
                var entityPropExpr = (propNameMap != null && propNameMap.ContainsKey(props[i].Name))
                    ? ChainedPropExprByMap(parameterExpr, propNameMap[props[i].Name], typeof(T), props[i].PropertyType)
                    : ChainedPropExpr(parameterExpr, props[i].Name, typeof(T), props[i].PropertyType);
                if (entityPropExpr == null)
                {
                    throw new Exception("can't figure out " + props[i].Name);
                }
                MemberAssignment assign;
                try
                {
                    assign = Expression.Bind(props[i], entityPropExpr);
                }catch (ArgumentException e)
                {
                    throw new Exception("assignment error:" + props[i].Name + " " + e.Message);
                }
                propExprs[i] = assign;
            }
            var initExpr = Expression.MemberInit(newDtoExpr, propExprs);
            var selector = Expression.Lambda <Func <T, TDto> >(initExpr, new ParameterExpression[] { parameterExpr });

            return(pager == null?_dao.GetProjectionByQuery(where, SortStruc <T> .CreateFrom(sort), selector) : _dao.GetProjectionByQuery(pager, where, SortStruc <T> .CreateFrom(sort), selector));
        }
Exemple #2
0
 public virtual IList <DORef <T> > GetRefList(Expression <Func <T, bool> > where = null, string sort = null)
 {
     return(_dao.GetProjectionByQuery(where, SortStruc <T> .CreateFrom(sort), o => new DORef <T> {
         Id = o.Id, RefText = o.RefText
     }));
 }
Exemple #3
0
 public virtual T GetOneByQuery(Expression <Func <T, bool> > where, string sort = null, bool unique = true)
 {
     return(_dao.GetOneByQuery(where, SortStruc <T> .CreateFrom(sort), unique));
 }
Exemple #4
0
 public virtual IList <T> GetByQuery(Pager pager, Expression <Func <T, bool> > where = null, string sort = null)
 {
     return(_dao.GetByQuery(pager, where, SortStruc <T> .CreateFrom(sort)));
 }
Exemple #5
0
 public virtual IList <TDto> GetDtoList <TDto>(Pager pager, Expression <Func <T, bool> > where = null, string sort = null, Expression <Func <T, TDto> > selector = null)
 {
     return(_dao.GetProjectionByQuery(pager, where, SortStruc <T> .CreateFrom(sort), selector));
 }