Exemplo n.º 1
0
        public override object GetValue(IDomainExecutionContext executionContext, ParameterInfo parameter)
        {
            IValueProvider provider = executionContext.DomainContext.GetRequiredService <IValueProvider>();
            object         value    = provider.GetValue(Name ?? parameter.Name, EntityDescriptor.GetMetadata(parameter.ParameterType).KeyType);

            if (value == null)
            {
                throw new ArgumentNullException(parameter.Name, "获取" + (Name ?? parameter.Name) + "实体的值为空。");
            }
            var     databaseContext = executionContext.DomainContext.GetRequiredService <IDatabaseContext>();
            dynamic entityContext;
            //if (parameter.ParameterType.GetTypeInfo().IsInterface)
            //    entityContext = typeof(DatabaseContextExtensions).GetMethod("GetWrappedContext").MakeGenericMethod(parameter.ParameterType).Invoke(null, new object[] { databaseContext });
            //else
            var type = EntityDescriptor.GetMetadata(parameter.ParameterType).Type;

            entityContext = typeof(IDatabaseContext).GetMethod("GetContext").MakeGenericMethod(type).Invoke(databaseContext, new object[0]);
            object entity = entityContext.GetAsync(value).Result;

            if (IsRequired && entity == null)
            {
                throw new EntityNotFoundException(parameter.ParameterType, value);
            }
            return(entity);
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override Task OnExecutingAsync(IDomainExecutionContext context)
        {
            var provider = context.DomainContext.GetService <IAuthenticationProvider>();

            Authentication = provider.GetAuthentication();
            return(AuthorizeCore(context));
        }
Exemplo n.º 3
0
 /// <summary>
 /// 授权。
 /// </summary>
 /// <param name="context">领域执行上下文。</param>
 /// <returns></returns>
 protected virtual Task AuthorizeCore(IDomainExecutionContext context)
 {
     if (!AllowAnonymous && !Authentication.Identity.IsAuthenticated)
     {
         throw new DomainServiceException(new UnauthorizedAccessException("用户未登录。"));
     }
     if (Roles.Length > 0)
     {
         if (Mode == AuthenticationRequiredMode.All)
         {
             foreach (var role in Roles)
             {
                 if (!Authentication.IsInRole(role))
                 {
                     throw new DomainServiceException(new UnauthorizedAccessException("用户没有“" + role + "”的权限。"));
                 }
             }
         }
         else
         {
             foreach (var role in Roles)
             {
                 if (Authentication.IsInRole(role))
                 {
                     return(Task.FromResult(0));
                 }
             }
             throw new DomainServiceException(new UnauthorizedAccessException("用户没有" + string.Join(",", "“" + Roles + "”") + "权限。"));
         }
     }
     return(Task.FromResult(0));
 }
Exemplo n.º 4
0
        public override Task OnExecutingAsync(IDomainExecutionContext context)
        {
            var provider       = context.DomainContext.GetService <IAuthenticationProvider>();
            var authentication = provider.GetAuthentication();

            if (!authentication.Identity.IsAuthenticated)
            {
                throw new UnauthorizedAccessException("用户未登录。");
            }
            if (Roles.Length > 0)
            {
                if (Mode == AuthenticationRequiredMode.All)
                {
                    foreach (var role in Roles)
                    {
                        if (!authentication.IsInRole(role))
                        {
                            throw new UnauthorizedAccessException("用户没有“" + role + "”的权限。");
                        }
                    }
                }
                else
                {
                    foreach (var role in Roles)
                    {
                        if (authentication.IsInRole(role))
                        {
                            return(Task.FromResult(0));
                        }
                    }
                    throw new UnauthorizedAccessException("用户没有" + string.Join(",", "“" + Roles + "”") + "权限。");
                }
            }
            return(Task.FromResult(0));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 引发事件。
        /// </summary>
        /// <param name="route">事件路由。</param>
        /// <param name="context">领域执行上下文。</param>
        /// <param name="eventArgs">事件参数。</param>
        public virtual void RaiseEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs)
            where T : EventArgs
        {
            if (route == null)
            {
                throw new ArgumentNullException(nameof(route));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (route.HandlerType != typeof(DomainServiceEventHandler <T>))
            {
                throw new InvalidCastException("事件路由处理器类型不符。");
            }
            Delegate d = GetEventRouteDelegate(route);

            if (d == null)
            {
                return;
            }
            foreach (var item in d.GetInvocationList().Cast <DomainServiceEventHandler <T> >())
            {
                item(context, eventArgs);
            }
            if (route.ParentRoute != null)
            {
                RaiseEvent(route.ParentRoute, context, eventArgs);
            }
        }
        /// <summary>
        /// 获取领域服务。
        /// </summary>
        /// <typeparam name="T">领域服务类型。</typeparam>
        /// <param name="context">当前领域执行上下文。</param>
        /// <returns>获取到的领域服务。</returns>
        public static T GetDomainService <T>(this IDomainExecutionContext context)
            where T : IDomainService
        {
            var domainProvider = context.DomainContext.GetRequiredService <IDomainServiceProvider>();

            return(domainProvider.GetService <T>());
        }
Exemplo n.º 7
0
 private async Task Domain_EntityPreUpdate(IDomainExecutionContext context, EntityUpdateEventArgs <T> e)
 {
     if (e.Entity.Member == null)
     {
         var authProvider = context.DomainContext.GetRequiredService <IAuthenticationProvider>();
         e.Entity.Member = await authProvider.GetAuthentication().GetPermission <IMember>();
     }
 }
Exemplo n.º 8
0
        public override Task OnExecutedAsync(IDomainExecutionContext context)
        {
            var valueProvider = context.DomainContext.GetRequiredService <IValueProvider>();
            var key           = GetCacheKey(context, valueProvider);
            var cacheProvider = context.DomainContext.GetRequiredService <ICacheProvider>();

            return(cacheProvider.GetCache().SetAsync(key, context.Result, ExpireTime));
        }
Exemplo n.º 9
0
 public override void RaiseEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs)
 {
     base.RaiseEvent <T>(route, context, eventArgs);
     if (_Parent != null)
     {
         _Parent.RaiseEvent(route, context, eventArgs);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// 获取值。
        /// </summary>
        /// <param name="executionContext">领域执行上下文。</param>
        /// <param name="parameter">参数信息。</param>
        /// <returns>返回值。</returns>
        public override object GetValue(IDomainExecutionContext executionContext, ParameterInfo parameter)
        {
            var option = executionContext.DomainContext.Options.GetOption(parameter.ParameterType);

            //if (option == null)
            //    throw new ArgumentNullException(parameter.Name, "获取" + parameter.ParameterType.Name + "选项为空。");
            return(option);
        }
Exemplo n.º 11
0
        public override async Task RaiseAsyncEvent(DomainServiceEventRoute route, IDomainExecutionContext context)
        {
            await base.RaiseAsyncEvent(route, context);

            if (_Parent != null)
            {
                await _Parent.RaiseAsyncEvent(route, context);
            }
        }
Exemplo n.º 12
0
 private Task Service_Executed(IDomainExecutionContext context)
 {
     if (context.DomainContext.DataBag.SearchItem != null && context.Result != null)
     {
         dynamic model = context.Result;
         model.SearchItem = context.DomainContext.DataBag.SearchItem;
     }
     return(Task.FromResult(0));
 }
Exemplo n.º 13
0
        public override Task OnExecutingAsync(IDomainExecutionContext context)
        {
            var valueProvider = context.DomainContext.GetRequiredService <IValueProvider>() as IConfigurableValueProvider;

            if (valueProvider != null)
            {
                valueProvider.SetAlias("id", "Index");
            }
            return(Task.CompletedTask);
        }
Exemplo n.º 14
0
        /// <summary>
        /// 获取值。
        /// </summary>
        /// <param name="executionContext">领域执行上下文。</param>
        /// <param name="parameter">参数信息。</param>
        /// <returns>返回值。</returns>
        public override object GetValue(IDomainExecutionContext executionContext, ParameterInfo parameter)
        {
            IAuthenticationProvider provider = executionContext.DomainContext.GetRequiredService <IAuthenticationProvider>();
            var user = typeof(IAuthentication).GetMethod("GetUser").MakeGenericMethod(parameter.ParameterType).Invoke(provider.GetAuthentication(), new object[0]);

            if (user == null)
            {
                throw new DomainServiceException(new ArgumentNullException(parameter.Name, "获取" + parameter.ParameterType.Name + "身份验证为空。"));
            }
            return(user);
        }
Exemplo n.º 15
0
        public override async Task OnExecutingAsync(IDomainExecutionContext context)
        {
            var valueProvider = context.DomainContext.GetRequiredService <IValueProvider>();
            var key           = GetCacheKey(context, valueProvider);
            var cacheProvider = context.DomainContext.GetRequiredService <ICacheProvider>();
            var value         = await cacheProvider.GetCache().GetAsync(key, ValueType);

            if (value != null)
            {
                context.Done(value);
            }
        }
Exemplo n.º 16
0
        public async Task <T> ExecuteAsync <T>(IDomainContext domainContext, MethodInfo method)
        {
            if (domainContext == null)
            {
                throw new ArgumentNullException(nameof(domainContext));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (method.DeclaringType != GetType())
            {
                throw new ArgumentException("该方法不是此领域服务的方法。");
            }
            IDomainServiceFilter[] filters = _FilterCache.GetOrAdd(method, t => t.GetCustomAttributes <DomainServiceFilterAttribute>().ToArray());
            var accessor         = domainContext.GetRequiredService <IDomainServiceAccessor>();
            var context          = new DomainExecutionContext(this, domainContext, method);
            var executionContext = ExecutionContext.Capture();

            Context = context;
            accessor.DomainService = this;
            try
            {
                await Task.WhenAll(filters.Select(t => t.OnExecutingAsync(context)));

                if (Executing != null)
                {
                    await Executing(context);
                }
                var result = await(Task <T>) method.Invoke(this, context.ParameterValues);
                context.Result = result;
                if (Executed != null)
                {
                    await Executed(context);
                }
                await Task.WhenAll(filters.Select(t => t.OnExecutedAsync(context)));

                return(result);
            }
            catch (Exception ex)
            {
                await Task.WhenAll(filters.Select(t => t.OnExceptionThrowingAsync(context, ex)));

                throw ex;
            }
            finally
            {
                accessor.DomainService = null;
            }
        }
Exemplo n.º 17
0
        private Task Domain_EntityQuery(IDomainExecutionContext context, EntityQueryEventArgs <T> e)
        {
            var valueProvider = context.DomainContext.GetRequiredService <IValueProvider>();
            var threadId      = valueProvider.GetValue <Guid?>("id");

            if (!threadId.HasValue)
            {
                return(Task.CompletedTask);
            }
            Guid key = threadId.Value;

            e.Queryable = e.Queryable.Wrap <IPost, T>().Where(t => t.Thread.Index == threadId.Value.Wrap()).Unwrap <IPost, T>();
            return(Task.CompletedTask);
        }
Exemplo n.º 18
0
 protected virtual async Task OnFilterThrowing(IDomainExecutionContext context, IDomainServiceFilter[] methodFilters, Exception exception)
 {
     foreach (var filter in Filters)
     {
         await filter.OnExceptionThrowingAsync(context, exception);
     }
     foreach (var filter in methodFilters)
     {
         await filter.OnExceptionThrowingAsync(context, exception);
     }
     foreach (var filter in context.DomainContext.Filter)
     {
         await filter.OnExceptionThrowingAsync(context, exception);
     }
 }
Exemplo n.º 19
0
 protected virtual async Task OnFilterExecuted(IDomainExecutionContext context, IDomainServiceFilter[] methodFilters)
 {
     foreach (var filter in context.DomainContext.Filter)
     {
         await filter.OnExecutedAsync(context);
     }
     foreach (var filter in methodFilters)
     {
         await filter.OnExecutedAsync(context);
     }
     foreach (var filter in Filters)
     {
         await filter.OnExecutedAsync(context);
     }
 }
Exemplo n.º 20
0
        private Task Domain_ThreadQuery(IDomainExecutionContext context, ComBoost.Data.EntityQueryEventArgs <IThread> e)
        {
            var valueProvider        = context.DomainContext.GetRequiredService <IValueProvider>();
            var page                 = valueProvider.GetValue <int?>("page");
            EntityPagerOption option = context.DomainContext.Options.GetOption <EntityPagerOption>();

            if (option == null)
            {
                option = new EntityPagerOption();
                context.DomainContext.Options.SetOption(option);
                option.CurrentSize = 20;
            }
            option.CurrentPage = page ?? 1;
            return(Task.CompletedTask);
        }
Exemplo n.º 21
0
 private Task Service_EntityPropertyUpdate(IDomainExecutionContext context, EntityPropertyUpdateEventArgs <T> e)
 {
     if (e.Property.Type == System.ComponentModel.DataAnnotations.CustomDataType.Password)
     {
         if (e.Value != null && (string)e.Value != "")
         {
             //                    if (e.Entity.IsNewCreated)
             //                        return Task.CompletedTask;
             //                    else
             //                        throw new ArgumentNullException("“" + e.Property.Name + "”不能为空。");
             e.Entity.SetPassword((string)e.Value);
         }
         e.IsHandled = true;
     }
     return(Task.CompletedTask);
 }
Exemplo n.º 22
0
        private async Task Domain_EntityPropertyUpdate(IDomainExecutionContext context, EntityPropertyUpdateEventArgs <T> e)
        {
            if (e.Property.Type == System.ComponentModel.DataAnnotations.CustomDataType.Image)
            {
                e.IsHandled = true;
                var storage = context.DomainContext.GetRequiredService <IStorageProvider>().GetStorage();
                var file    = (ISelectedFile)e.Value;
                if (file == null)
                {
                    return;
                }
                var path = await storage.PutAsync(file.Stream, file.Filename);

                e.Property.SetValue(e.Entity, path);
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// 获取值。
        /// </summary>
        /// <param name="executionContext">领域执行上下文。</param>
        /// <param name="parameter">参数信息。</param>
        /// <returns>返回值。</returns>
        public override object GetValue(IDomainExecutionContext executionContext, ParameterInfo parameter)
        {
            var service = executionContext.DomainContext.GetService(parameter.ParameterType);

            if (service == null)
            {
                if (parameter.HasDefaultValue)
                {
                    service = parameter.DefaultValue;
                }
                else if (IsRequired)
                {
                    throw new DomainServiceException(new ArgumentNullException(parameter.Name, "获取" + parameter.ParameterType.Name + "服务为空。"));
                }
            }
            return(service);
        }
Exemplo n.º 24
0
        protected virtual string GetCacheKey(IDomainExecutionContext context, IValueProvider valueProvider)
        {
            string key = "__ComBoostCache_" + context.DomainService.GetType().Name + "_" + context.DomainMethod.Name;

            foreach (var parameter in Parameters)
            {
                var value = valueProvider.GetValue <string>(parameter);
                if (value == null)
                {
                    key += "_";
                    continue;
                }
                var hash = Convert.ToBase64String(BitConverter.GetBytes(value.GetHashCode()));
                key += "_" + hash;
            }
            return(key);
        }
Exemplo n.º 25
0
        /// <summary>
        /// 获取值。
        /// </summary>
        /// <param name="executionContext">领域执行上下文。</param>
        /// <param name="parameter">参数信息。</param>
        /// <returns>返回值。</returns>
        public override object GetValue(IDomainExecutionContext executionContext, ParameterInfo parameter)
        {
            IValueProvider provider = executionContext.DomainContext.GetRequiredService <IValueProvider>();
            object         value    = provider.GetValue(Name ?? parameter.Name, parameter.ParameterType);

            if (value == null)
            {
                if (DefaultValue != null || parameter.HasDefaultValue)
                {
                    value = DefaultValue ?? parameter.DefaultValue;
                }
                else if (IsRequired)
                {
                    throw new DomainServiceException(new ArgumentNullException(parameter.Name, "获取" + (Name ?? parameter.Name) + "的值为空。"));
                }
            }
            return(value);
        }
Exemplo n.º 26
0
        public override Task OnExecutingAsync(IDomainExecutionContext context)
        {
            var provider       = context.DomainContext.GetService <IAuthenticationProvider>();
            var authentication = provider.GetAuthentication();

            if (!authentication.Identity.IsAuthenticated)
            {
                throw new UnauthorizedAccessException("用户未登录。");
            }
            if (Roles.Length > 0)
            {
                foreach (var role in Roles)
                {
                    if (!authentication.IsInDynamicRole(role))
                    {
                        throw new UnauthorizedAccessException("用户没有“" + role + "”的权限。");
                    }
                }
            }
            return(Task.FromResult(0));
        }
Exemplo n.º 27
0
        /// <summary>
        /// 引发异步事件。
        /// </summary>
        /// <param name="route">事件路由。</param>
        /// <param name="context">领域执行上下文。</param>
        public virtual async Task RaiseAsyncEvent(DomainServiceEventRoute route, IDomainExecutionContext context)
        {
            if (route == null)
            {
                throw new ArgumentNullException(nameof(route));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (route.HandlerType != typeof(DomainServiceAsyncEventHandler))
            {
                throw new InvalidCastException("事件路由处理器类型不符。");
            }
            Delegate d = GetEventRouteDelegate(route);

            if (d == null)
            {
                return;
            }
            foreach (var item in d.GetInvocationList().Cast <DomainServiceAsyncEventHandler>())
            {
                if (route.AsyncMode == DomainServiceAsyncEventMode.Await)
                {
                    await item(context);
                }
                else
                {
                    Task itemTask = item(context);
                }
            }
            if (route.ParentRoute != null)
            {
                await RaiseAsyncEvent(route.ParentRoute, context);
            }
        }
Exemplo n.º 28
0
 /// <summary>
 /// 异步抛出异常时。
 /// </summary>
 /// <param name="context">领域执行上下文。</param>
 /// <param name="exception">异常内容。</param>
 /// <returns>异步任务。</returns>
 public Task OnExceptionThrowingAsync(IDomainExecutionContext context, Exception exception)
 {
     return(Task.CompletedTask);
 }
Exemplo n.º 29
0
        private Task Service_EntityQuery(IDomainExecutionContext context, EntityQueryEventArgs <T> e)
        {
            List <EntitySearchItem> searchItems = new List <EntitySearchItem>();

            var valueProvider = context.DomainContext.GetRequiredService <IValueProvider>();

            if (!valueProvider.GetValue <bool>("Search"))
            {
                return(Task.CompletedTask);
            }

            IQueryable <T> queryable = e.Queryable;

            var keys = valueProvider.Keys.Where(t => t.StartsWith("Search.", StringComparison.OrdinalIgnoreCase)).Select(t => t.Substring(7).Split('.')).GroupBy(t => t[0], t => t.Length == 1 ? "" : "." + t[1]).ToArray();

            for (int i = 0; i < keys.Length; i++)
            {
                string            propertyName = keys[i].Key;
                IPropertyMetadata property     = Service.Metadata.SearchProperties.FirstOrDefault(t => t.ClrName.Equals(propertyName, StringComparison.OrdinalIgnoreCase));
                if (property == null)
                {
                    continue;
                }
                EntitySearchItem searchItem = new EntitySearchItem();
                string[]         options    = keys[i].ToArray();
                switch (property.Type)
                {
                case CustomDataType.Date:
                case CustomDataType.DateTime:
                    for (int a = 0; a < options.Length; a++)
                    {
                        if (options[a].Equals(".Start", StringComparison.OrdinalIgnoreCase))
                        {
                            DateTime start;
                            if (!DateTime.TryParse(valueProvider.GetValue <string>("Search." + keys[i].Key + options[a]), out start))
                            {
                                continue;
                            }
                            searchItem.MorethanDate = start;
                            ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                            queryable = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(Expression.GreaterThanOrEqual(Expression.Property(parameter, property.ClrName), Expression.Constant(start)), parameter));
                        }
                        else if (options[a].Equals(".End", StringComparison.OrdinalIgnoreCase))
                        {
                            DateTime end;
                            if (!DateTime.TryParse(valueProvider.GetValue <string>("Search." + keys[i].Key + options[a]), out end))
                            {
                                continue;
                            }
                            if (property.Type == CustomDataType.Date)
                            {
                                end = end.AddDays(1);
                            }
                            searchItem.LessthanDate = end;
                            ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                            queryable = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(Expression.LessThanOrEqual(Expression.Property(parameter, property.ClrName), Expression.Constant(end)), parameter));
                        }
                    }
                    break;

                case CustomDataType.Boolean:
                case CustomDataType.Gender:
                    if (options[0] == "")
                    {
                        bool result;
                        if (!bool.TryParse(valueProvider.GetValue <string>("Search." + keys[i].Key), out result))
                        {
                            continue;
                        }
                        searchItem.Equal = result;
                        ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                        queryable = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(Expression.Equal(Expression.Property(parameter, property.ClrName), Expression.Constant(result)), parameter));
                    }
                    break;

                case CustomDataType.Currency:
                case CustomDataType.Integer:
                case CustomDataType.Number:
                    for (int a = 0; a < options.Length; a++)
                    {
                        if (options[a].Equals(".Start", StringComparison.OrdinalIgnoreCase))
                        {
                            object start;
                            try
                            {
                                start = TypeDescriptor.GetConverter(property.ClrType).ConvertFromString(valueProvider.GetValue <string>("Search." + keys[i].Key + options[a]));
                            }
                            catch
                            {
                                continue;
                            }
                            searchItem.Morethan = double.Parse(start.ToString());
                            ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                            queryable = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(Expression.GreaterThanOrEqual(Expression.Property(parameter, property.ClrName), Expression.Constant(start)), parameter));
                        }
                        else if (options[a].Equals(".End", StringComparison.OrdinalIgnoreCase))
                        {
                            object end;
                            try
                            {
                                end = TypeDescriptor.GetConverter(property.ClrType).ConvertFromString(valueProvider.GetValue <string>("Search." + keys[i].Key + options[a]));
                            }
                            catch
                            {
                                continue;
                            }
                            searchItem.Lessthan = double.Parse(end.ToString());
                            ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                            queryable = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(Expression.LessThanOrEqual(Expression.Property(parameter, property.ClrName), Expression.Constant(end)), parameter));
                        }
                    }
                    break;

                case CustomDataType.Other:
                    if (property.CustomType == "Enum")
                    {
                        object result;
                        try
                        {
                            result = Enum.Parse(property.ClrType, valueProvider.GetValue <string>("Search." + keys[i].Key));
                        }
                        catch
                        {
                            continue;
                        }
                        searchItem.Enum = new EnumConverter(property.ClrType).ConvertToString(result);
                        ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                        queryable = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(Expression.Equal(Expression.Property(parameter, property.ClrName), Expression.Constant(result)), parameter));
                    }
                    else if (property.CustomType == "Entity")
                    {
                        searchItem.Contains = valueProvider.GetValue <string>("Search." + keys[i].Key);
                        if (searchItem.Contains == null)
                        {
                            continue;
                        }
                        var isId = valueProvider.GetValue <bool?>("Search." + keys[i].Key + ".Id");
                        ParameterExpression parameter = Expression.Parameter(Service.Metadata.Type);
                        if (isId.HasValue && isId.Value)
                        {
                            var        keyProperty = EntityDescriptor.GetMetadata(property.ClrType).KeyProperty;
                            var        id          = keyProperty.Converter.ConvertFrom(searchItem.Contains);
                            Expression expression  = Expression.Property(Expression.Property(parameter, property.ClrName), keyProperty.ClrName);
                            expression = Expression.Equal(expression, Expression.Constant(id));
                            queryable  = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(expression, parameter));
                        }
                        else
                        {
                            Expression expression = Expression.Property(Expression.Property(parameter, property.ClrName), EntityDescriptor.GetMetadata(property.ClrType).DisplayProperty.ClrName);
                            expression = Expression.Call(expression, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(searchItem.Contains));
                            queryable  = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(expression, parameter));
                        }
                    }
                    else if (property.ClrType == typeof(string))
                    {
                        searchItem.Contains = valueProvider.GetValue <string>("Search." + keys[i].Key);
                        if (searchItem.Contains == null)
                        {
                            continue;
                        }
                        ParameterExpression parameter  = Expression.Parameter(Service.Metadata.Type);
                        Expression          expression = Expression.Property(parameter, property.ClrName);
                        expression = Expression.Call(expression, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(searchItem.Contains));
                        queryable  = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(expression, parameter));
                    }
                    break;

                default:
                    if (property.ClrType == typeof(string))
                    {
                        searchItem.Contains = valueProvider.GetValue <string>("Search." + keys[i].Key);
                        if (searchItem.Contains == null)
                        {
                            continue;
                        }
                        ParameterExpression parameter  = Expression.Parameter(Service.Metadata.Type);
                        Expression          expression = Expression.Property(parameter, property.ClrName);
                        expression = Expression.Call(expression, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(searchItem.Contains));
                        queryable  = queryable.Where <T>(Expression.Lambda <Func <T, bool> >(expression, parameter));
                    }
                    break;
                }
                if (searchItem.Contains != null || searchItem.Enum != null || searchItem.Equal.HasValue || searchItem.Lessthan.HasValue || searchItem.LessthanDate.HasValue || searchItem.Morethan.HasValue || searchItem.MorethanDate.HasValue)
                {
                    searchItem.Name = property.Name;
                }
                if (searchItem.Name != null)
                {
                    searchItems.Add(searchItem);
                }

                e.Queryable = queryable;
            }

            context.DomainContext.DataBag.SearchItem = searchItems.ToArray();
            return(Task.CompletedTask);
        }
Exemplo n.º 30
0
 public override Task OnExecutingAsync(IDomainExecutionContext context)
 {
     context.DomainContext.GetRequiredService <IConfigurableValueProvider>().SetAlias("id", "Index");
     return(Task.CompletedTask);
 }