私はあなたがあなたの答えを2つの部分に分けなければならないと思います:
まず、リストからランダムな列挙型を取得します。この部分は、提供された他の回答によってすでに解決されていると思います。
その後、選択した列挙型のランダムな値のリストを作成します。したがって、これらのタイプごとに有効なランダム値を作成できるファクトリが必要です。あなたのニーズに最も近いものはAutoPocoでなければなりません。たとえば、好きな値で満たされたサンプルオブジェクトの束を作成するのは非常に簡単です。
var factory = AutoPoco.AutoPocoContainer.Configure(x =>
{
x.Conventions(c =>
{
c.UseDefaultConventions();
});
x.Include<DataRowWrapper>()
.Setup(row => row.Timestamp).Use<DateTimeUniqueSource>()
.Setup(row => row.Name).Use<LastNameSource>()
.Setup(row => row.Value).Use<ApproximateNumberSource<decimal>>()
.Setup(row => row.Description).Use<RandomReadableStringSource>(10, 20);
});
var session = factory.CreateSession();
var sampleRows = session.List<DataRowWrapper>(1000).Get();
ご覧のとおり、各プロパティに独自のソース()を提供できます.Use<...Source>()
。プロジェクト内にはすでにいくつかのデフォルトのソースがありますが、私は次のように自分でいくつか作成しました。
public class RandomReadableStringSource : DatasourceBase<string>
{
private readonly char[] _Vocals = new char[] { 'a', 'e', 'i', 'o', 'u' };
private readonly char[] _Consonants = new char[] { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w' };
private Random _Random;
private int _Minimum;
private int _Maximum;
public RandomReadableStringSource()
: this(20)
{ }
public RandomReadableStringSource(int max)
: this(5, max)
{ }
public RandomReadableStringSource(int min, int max)
{
if (min <= 0)
{
throw new ArgumentOutOfRangeException("minimum must be greater zero.");
}
if (min > max)
{
throw new ArgumentOutOfRangeException("minimum must be less or equal maximum.");
}
_Random = new Random();
_Minimum = min;
_Maximum = max;
}
public override string Next(IGenerationSession session)
{
var length = _Random.Next(_Minimum, _Maximum);
var sb = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
var array = i % 2 == 0 ? _Consonants : _Vocals;
sb.Append(array[_Random.Next(array.Length)]);
}
return sb.ToString();
}
}
public class DateTimeUniqueSource : DatasourceBase<DateTime>
{
private Random _Random;
private DateTime _LastDateTime;
public DateTimeUniqueSource()
: this(new DateTime(1900, 1, 1))
{ }
public DateTimeUniqueSource(DateTime startdate)
{
if (startdate == null)
{
throw new ArgumentNullException("startdate");
}
_Random = new Random();
_LastDateTime = startdate;
}
public override DateTime Next(IGenerationSession session)
{
_LastDateTime = _LastDateTime.AddHours(_Random.NextDouble() * 1000);
return _LastDateTime;
}
}
したがって、タイプごとに独自のソースを作成し、その後、一連のサンプルオブジェクトを非常に簡単に作成できます。