Esempio n. 1
0
        /// <summary>
        /// Builds the artifact for the root resource of the composite definition.
        /// </summary>
        /// <param name="parentResult">The parent build result, for compositional behavior (if applicable).</param>
        /// <param name="builderContext">The builder context.</param>
        /// <param name="processorContext">The composite definition processor context.</param>
        /// <returns>The build result.</returns>
        public CompositeQuery BuildForChildResource(
            CompositeQuery parentResult,
            HqlBuilderContext builderContext,
            CompositeDefinitionProcessorContext processorContext)
        {
            string hql =
                $"select {Environment.NewLine}\t" + builderContext.Select
                + $"{Environment.NewLine}from " + builderContext.From
                + (builderContext.Where.Length > 0
                                    ? $"{Environment.NewLine}where " + builderContext.Where
                                    : string.Empty)
                + $"{Environment.NewLine}order by " + builderContext.OrderBy;

            if (_logger.IsDebugEnabled)
            {
                object correlationId;

                if (builderContext.QueryStringParameters.TryGetValue(
                        SpecialQueryStringParameters.CorrelationId, out correlationId))
                {
                    _logger.DebugFormat("HQL[{0}]:{1}{2}", correlationId, Environment.NewLine, hql);
                }
                else
                {
                    _logger.DebugFormat("HQL:{0}{1}", Environment.NewLine, hql);
                }
            }

            var session = _sessionFactory.GetCurrentSession();
            var query   = session.CreateQuery(hql);

            object idValues;

            if (builderContext.ParameterValueByName.TryGetValue(BaseEntityIdName, out idValues))
            {
                _parameterListSetter.SetParameterList(query, "BaseEntityId", idValues as IEnumerable);
            }

            // Apply current query's filter parameters.
            SetQueryParameters(query, builderContext.CurrentQueryFilterParameterValueByName);

            bool isSingleItemResult =
                processorContext.IsReferenceResource() ||
                processorContext.IsEmbeddedObject();

            var thisQuery = new CompositeQuery(
                parentResult,
                processorContext.MemberDisplayName.ToCamelCase(),
                builderContext.PropertyProjections.Select(x => x.DisplayName.ToCamelCase() ?? x.ResourceProperty.PropertyName.ToCamelCase())
                .ToArray(),
                query
                .SetResultTransformer(Transformers.AliasToEntityMap)
                .Future <object>(),
                isSingleItemResult);

            parentResult.ChildQueries.Add(thisQuery);

            return(thisQuery);
        }
Esempio n. 2
0
        public async Task <IList <TEntity> > GetByIdsAsync(IList <Guid> ids, CancellationToken cancellationToken)
        {
            using (new SessionScope(SessionFactory))
            {
                IEnumerable <TEntity> results;

                if (ids.Count == 1)
                {
                    results = await GetAggregateResultsAsync(
                        "where a.Id = :id",
                        q => q.SetParameter("id", ids[0]), cancellationToken);
                }
                else
                {
                    results = await GetAggregateResultsAsync(
                        "where a.Id IN (:ids)",
                        q => _parameterListSetter.SetParameterList(q, "ids", ids),
                        cancellationToken,
                        "order by a.Id");
                }

                // Process multiple results in the first-level cache to a list of complete aggregates
                return(results.ToList());
            }
        }