public IEnumerable <Query.Document> Search(Query.Document.Specification specification)
        {
            Guard.AgainstNull(specification, nameof(specification));

            var result = _queryMapper.MapObjects <Query.Document>(_queryFactory.Search(specification))
                         .ToDictionary(document => document.Id);

            if (specification.StatusEventsIncluded && result.Any())
            {
                foreach (var mappedRow in _queryMapper.MapRows <Query.Document.StatusEvent>(_queryFactory.GetStatusEvents(result.Keys)))
                {
                    if (result.TryGetValue(Columns.DocumentId.MapFrom(mappedRow.Row), out var document))
                    {
                        document.StatusEvents.Add(mappedRow.Result);
                    }
                }
            }

            if (specification.PropertiesIncluded && result.Any())
            {
                foreach (var mappedRow in _queryMapper.MapRows <Query.Document.Property>(query: _queryFactory.GetProperties(result.Keys)))
                {
                    if (result.TryGetValue(Columns.DocumentId.MapFrom(mappedRow.Row), out var document))
                    {
                        document.Properties.Add(mappedRow.Result);
                    }
                }
            }

            return(result.Values);
        }
        public IQuery Search(Query.Document.Specification specification)
        {
            Guard.AgainstNull(specification, nameof(specification));

            var ids = string.Join(",", specification.GetIds().Select(item => $"'{item}'"));

            return(RawQuery.Create($@"
select {(specification.MaximumRows > 0 ? $"top {specification.MaximumRows}" : string.Empty)}
	Id,
    ReferenceId,
    EffectiveFromDate,
    EffectiveToDate,
	ContentType,
	FileName,
	SystemName,
	Username,
	Status,
	StatusDateRegistered
from
	Document
where
    1 = 1
{(specification.HasIds ? $@"
and 
(
    Id in ({ids})
    or
    (
        ReferenceId in ({ids}) {(specification.ActiveOnly ? "and EffectiveToDate = @EffectiveToDate" : string.Empty)}
    )
)
" : string.Empty)}
order by
    EffectiveFromDate desc
")
                   .AddParameterValue(Columns.EffectiveToDate, DateTime.MaxValue));
        }