I am trying to create a custom implementation of List where my list IntegrityList enforces some business rules.
[Serializable]
public class IntegrityList<TSource> : List<TSource> where TSource : IEvent
{
... business logic...
}
But I also need to have custom extensions method e.g. ToList() and ToListAsync(). I looked into the implementation of microsoft and created a simple ToIntegrityList() extension method, but I cannot find the implementation of ToListAsync(). Does anyone have any idea how to implement such method?
public static class IntegrityListExtensions
{
public static IntegrityList<TSource> ToIntegrityList<TSource>(this IEnumerable<TSource> source) where TSource : IEvent
{
if (source == null) throw new ArgumentNullException(nameof(source));
return new IntegrityList<TSource>(source);
}
public static Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source) where TSource : IEvent
{
if (source == null) throw new ArgumentNullException(nameof(source));
throw new NotImplementedException("Missing implementation");
}
public static Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken) where TSource : IEvent
{
if (source == null) throw new ArgumentNullException(nameof(source));
else if (cancellationToken == null) throw new ArgumentNullException(nameof(cancellationToken));
throw new NotImplementedException("Missing implementation");
}
}
CodePudding user response:
- Implementation in EF Core EF Core GitHub
More resources
A basic iteration might look like this, you can extend it to save results to the list and do whatever you want in between
await foreach (int item in RangeAsync(10, 3).WithCancellation(token))
Console.Write(item " "); // Prints 10 11 12
CodePudding user response:
I really don't understand the use case of your IntegrityList
(*1), any case, here the static extensions methods:
public static class IntegrityListExtensions
{
public static IntegrityList<TSource> ToIntegrityList<TSource>(this IEnumerable<TSource> source) where TSource : IEvent
{
if (source == null) throw new ArgumentNullException(nameof(source));
var myList = new IntegrityList<TSource>();
myList.AddRange(source);
return myList;
}
public static Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source) where TSource : IEvent
{
if (source == null) throw new ArgumentNullException(nameof(source));
var myList = source.ToIntegrityList();
return Task.FromResult(myList);
}
public static Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken) where TSource : IEvent
{
if (source == null) throw new ArgumentNullException(nameof(source));
var myList = source.ToIntegrityList();
return Task.FromResult(myList);
}
}
(*1) May be you are looking for public class IntegrityList<TSource> : IList<TSource>, IAsyncEnumerable<TSource> where TSource : IEvent
. Notice the IList
instead List
and also the IAsyncEnumerable
interface to deal with async.