/// <summary>
        /// Initializes a new instance of the <see cref="DbSetAdapter{TEntity, TContext, TModel}"/> class.
        /// </summary>
        /// <param name="dataMapper">automapper profile</param>
        /// <param name="factory">context factory</param>
        /// <param name="configuration">config options</param>
        /// <param name="logger">injected logger</param>
        public DbSetAdapter(IMapper dataMapper, IDbContextFactory factory, IDbSetConfiguration <TEntity> configuration, ILogger logger)
            : base(dataMapper, configuration, logger)
        {
            var f = Arguments.EnsureNotNull(factory, nameof(factory));

            this.context = new Lazy <DbContext>(f.Create <TContext>, LazyThreadSafetyMode.PublicationOnly);
        }
Beispiel #2
0
        private object GetDataFromContext(Type type, ResolveFieldContext <object> resolveContext,
                                          IDbSetConfiguration dbSetConfiguration)
        {
            var getDataMethod = typeof(DynamicQuery <TContext>)
                                .GetMethod(nameof(GetTypedDataFromContext), BindingFlags.NonPublic | BindingFlags.Instance);

            // ReSharper disable once PossibleNullReferenceException
            getDataMethod = getDataMethod.MakeGenericMethod(type);

            using (var scope = _resolver.CreateScope())
            {
                var context = scope.ServiceProvider.GetService <TContext>();

                try
                {
                    return(getDataMethod.Invoke(this, new object[] { context, resolveContext, dbSetConfiguration }));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException == null)
                    {
                        throw new GraphityException(ex.Message);
                    }

                    throw new GraphityException(ex.InnerException.Message);
                }
            }
        }
Beispiel #3
0
    public DynamicObjectGraphType(IDbSetConfiguration configuration, Action <Type> typeRegistrar)
    {
        Name = configuration.TypeName;

        var properties = typeof(TEntity)
                         .GetProperties();

        foreach (var property in properties)
        {
            if (configuration.PropertyConfigurations.Any(pc => pc.PropertyName == property.Name && pc.Exclude))
            {
                continue;
            }

            if (property.PropertyType == typeof(string))
            {
                Field <StringGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(Guid))
            {
                Field <NonNullGraphType <GuidGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(Guid?))
            {
                Field <GuidGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(byte) ||
                     property.PropertyType == typeof(short) ||
                     property.PropertyType == typeof(int) ||
                     property.PropertyType == typeof(long))
            {
                Field <NonNullGraphType <IntGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(byte?) ||
                     property.PropertyType == typeof(short?) ||
                     property.PropertyType == typeof(int?) ||
                     property.PropertyType == typeof(long?))
            {
                Field <IntGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(bool))
            {
                Field <NonNullGraphType <BooleanGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(bool?))
            {
                Field <BooleanGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(double) ||
                     property.PropertyType == typeof(float))
            {
                Field <NonNullGraphType <FloatGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(double?) ||
                     property.PropertyType == typeof(float?))
            {
                Field <FloatGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(decimal))
            {
                Field <NonNullGraphType <DecimalGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(decimal?))
            {
                Field <DecimalGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(DateTime))
            {
                Field <NonNullGraphType <DateTimeGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(DateTime?))
            {
                Field <DateTimeGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(DateTimeOffset))
            {
                Field <NonNullGraphType <DateTimeOffsetGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(DateTimeOffset?))
            {
                Field <DateTimeOffsetGraphType>(property.Name);
            }
            else if (property.PropertyType == typeof(TimeSpan))
            {
                Field <NonNullGraphType <TimeSpanSecondsGraphType> >(property.Name);
            }
            else if (property.PropertyType == typeof(TimeSpan?))
            {
                Field <TimeSpanSecondsGraphType>(property.Name);
            }
            else if (property.PropertyType.IsEnum)
            {
                var enumGraphType = typeof(EnumerationGraphType <>).MakeGenericType(property.PropertyType);
                var graphType     = typeof(NonNullGraphType <>).MakeGenericType(enumGraphType);
                Field(graphType, property.Name);
                typeRegistrar(enumGraphType);
            }
            else if (property.PropertyType.IsGenericType &&
                     property.PropertyType.GetGenericArguments().First().IsEnum)
            {
                var enumType      = property.PropertyType.GetGenericArguments().First();
                var enumGraphType = typeof(EnumerationGraphType <>).MakeGenericType(enumType);
                Field(enumGraphType, property.Name);
                typeRegistrar(enumGraphType);
            }
            else if (property.PropertyType == typeof(byte[]))
            {
                //Unsupported type, just ignore
            }
            else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                var type = property.PropertyType.GetGenericArguments()[0];

                var graphType     = typeof(DynamicObjectGraphType <,>).MakeGenericType(typeof(TContext), type);
                var listGraphType = typeof(ListGraphType <>).MakeGenericType(graphType);
                Field(listGraphType, property.Name);
            }
            else if (DynamicQuery <TContext> .QueryOptions.CanBeAChild(property.PropertyType))
            {
                var graphType = typeof(DynamicObjectGraphType <,>).MakeGenericType(typeof(TContext), property.PropertyType);
                Field(graphType, property.Name);
            }
        }
    }
Beispiel #4
0
        // ReSharper disable once UnusedMember.Local
        private IEnumerable <object> GetTypedDataFromContext <TEntity>(
            DbContext context,
            ResolveFieldContext resolveContext,
            IDbSetConfiguration dbSetConfiguration)
            where TEntity : class
        {
            IQueryable <TEntity> query = context.Set <TEntity>();

            if (dbSetConfiguration.FilterExpression != null)
            {
                query = query.Where((Expression <Func <TEntity, bool> >)dbSetConfiguration.FilterExpression);
            }

            if (resolveContext.Arguments.ContainsKey("where"))
            {
                var whereExpressions = resolveContext.GetArgument <WhereExpression[]>("where");

                foreach (var whereExpression in whereExpressions)
                {
                    var expression = ComparisonExpressions.GetComparisonExpression <TEntity>(
                        whereExpression.Comparison,
                        whereExpression.Path,
                        whereExpression.Value);

                    query = query.Where(expression);
                }
            }

            if (resolveContext.Arguments.ContainsKey("filter"))
            {
                var filterExpression = resolveContext.GetArgument <string>("filter");

                var expression = GetFilterExpression <TEntity>(filterExpression);
                query = query.Where(expression);
            }

            if (resolveContext.Arguments.ContainsKey("orderBy"))
            {
                var orderByExpression = resolveContext.GetArgument <OrderByExpression>("orderBy");
                query = ApplyCustomOrderBy(query, orderByExpression);
            }
            else if (dbSetConfiguration.DefaultOrderByExpression != null)
            {
                query = dbSetConfiguration.OrderByDirection == OrderByDirection.Ascending
                    ? query.OrderBy((Expression <Func <TEntity, object> >)dbSetConfiguration.DefaultOrderByExpression)
                    : query.OrderByDescending((Expression <Func <TEntity, object> >)dbSetConfiguration.DefaultOrderByExpression);
            }

            if (resolveContext.Arguments.ContainsKey("skip"))
            {
                var skip = resolveContext.GetArgument <int>("skip");
                query = query.Skip(skip);
            }

            var take = resolveContext.GetArgument <int>("take");

            query = query.Take(take);

            var projectionExpression = (Expression <Func <TEntity, TEntity> >)GetProjectionExpression(typeof(TEntity), resolveContext.SubFields.Values);

            return(query
                   .Select(projectionExpression)
                   .ToList());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DbSetAdapterSimpleKey{TEntity, TContext, TKey, TModel}"/> class.
 /// </summary>
 /// <param name="dataMapper">automapper profile</param>
 /// <param name="factory">context factory</param>
 /// <param name="configuration">config options</param>
 /// <param name="logger">injected logger</param>
 public DbSetAdapterSimpleKey(IMapper dataMapper, IDbContextFactory factory, IDbSetConfiguration <TEntity> configuration, ILogger logger)
     : base(dataMapper, factory, configuration, logger)
 {
 }