Skip to main content

Domain helpers

Overview

Domain helpers are single purpose and reusable static classes to simplify usage of various functionality. Domain helpers are placed into Domain.Helpers namespace.

Queryable extensions

ApplyPagingFilter extension method

//usage of paging filter on queryable collection
var instances = dataQuery
.OrderByDescending(x => x.ModifiedOn)
.ApplyPagingFilter(query.PagingFilter)
.ToList();
//implementation of paging filter for queryable collection
using System;
using System.Linq;
using ASOL.Core.Paging.Contracts.Filters;

namespace My.App.Domain.Helpers
{
public static class QueryableExtensions
{
public static IQueryable<T> ApplyPagingFilter<T>(this IQueryable<T> query, PagingFilter pagingFilter)
{
if (pagingFilter == null) throw new ArgumentNullException(nameof(pagingFilter));

return query
.Skip(pagingFilter.Offset)
.Take(pagingFilter.Limit);
}
}
}

Enumerable extensions

DistinctBy extension method

//usage of distinct with key-selector on enumerable collection
var qGroups = dataQuery
.ToLookup(x => x.QualificationId);

//basic qualifications
var qList = new List<Qualification>();
foreach (var qGroup in qGroups)
{
qList.AddRange(qGroup.Select(x => x.RelatedQualification));
}

//related qualifications
var qItems = qList.DistinctBy(x => x.Id);
//implementation of distinct with key-selector for enumerable collection
using System;
using System.Collections.Generic;
using System.Linq;

namespace My.App.Domain.Helpers
{
public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer = null)
{
var knownKeys = comparer == null ? new HashSet<TKey>() : new HashSet<TKey>(comparer);
return source.Where(element => knownKeys.Add(keySelector(element)));
}
}
}