CSharp · Console
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
public class MyDto
{
public string Foo { get; set; }
public override string ToString()
{
return this.Foo;
}
}
public interface IRepositoryReloader
{
void ReloadData();
}
public interface IRepositoryLoader<out T> : IRepositoryReloader
{
IEnumerable<T> LoadData();
}
public abstract class Test<TItem,TContainer> : IRepositoryLoader<TItem> where TContainer : class, IEnumerable<TItem>
{
protected TContainer Source;
public abstract IEnumerable<TItem> LoadData();
void IRepositoryReloader.ReloadData()
{
this.Source = (TContainer)Activator.CreateInstance(typeof(TContainer), new [] { LoadData() } );
}
public override string ToString()
{
return string.Format
(
"Type:{0} Count:{1} Elements: {{{2}}}",
Source.GetType().Name,
Source.Count(),
string.Join(",", Source.Select( s => string.Format("'{0}'", s)))
);
}
}
public class ConcurrentDataContainer : Test<MyDto, ConcurrentBag<MyDto>>
{
public override IEnumerable<MyDto> LoadData()
{
yield return new MyDto { Foo = "Element1" };
yield return new MyDto { Foo = "Element2" };
}
}
public class SingleThreadedDataContainer : Test<MyDto, List<MyDto>>
{
public override IEnumerable<MyDto> LoadData()
{
yield return new MyDto { Foo = "Element1" };
yield return new MyDto { Foo = "Element2" };
}
}
public class Program
{
public static void Main()
{
IRepositoryReloader test1 = new ConcurrentDataContainer();
test1.ReloadData();
Console.WriteLine(test1.ToString());
IRepositoryReloader test2 = new SingleThreadedDataContainer();
test2.ReloadData();
Console.WriteLine(test2.ToString());
}
}