如何获得LogicalTypes随机值 [英] How to get Random values for LogicalTypes

查看:183
本文介绍了如何获得LogicalTypes随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具,生成我需要生成一个用于测试的samlpe价值实体。问题是,我们有很多的逻辑类型(一些相同类型的,但仍然不同),并befor编码我想知道,如果有人有一个简单的解决方案...

I have a tool that generate entities I need to generate a samlpe value for testing. the problem is that we have alot of logical types (some of the same type but still different) and befor coding i wanted to know if someone have a easier solution...

这里是枚举:

public enum LogicalTypeEnum
    { 
        Identity,
        DateAndTime,
        Binary,
        Quantity,
        Comment,
        Money,
        Rate,
        TimeStamp,
        Caption,
        Reference,
        Number,
        Weight,
        Space,
        Username,
        Phone,
        Email,
        ZipCode
    }

谢谢!

编辑1:我想生成一个随机值没有得到枚举随机元素。我在寻找一种方式来获得一个随机的电子邮件或邮递区号或物有所值。

EDIT 1: I want to generate a random value not get a random element from the enum. I'm searching a way to get a random email or zipcode or money value.

推荐答案

我认为你必须分配你的答案分为两部分:

I think you have to divide your answer into two parts:

首先得到一个随机枚举类型淘汰之列。我认为这部分已经提供的其他答案解决。

First get a random enum type out of the list. I think this part is already solved by the other answers provided.

之后,您希望创建随机值的列表所选枚举。所以你需要一个工厂,可以为每种类型的有效的随机值。那个最接近您的需要的东西应该是 AutoPoco 。这是很容易地创建一个充满一堆了一些你喜欢的,例如值

Afterwards you like to create a list of random values for the selected enum. So you need a factory that can create a valid random value for each of these types. The thing that come closest to your needs should be AutoPoco. It is quite easy to create a bunch of sample object filled up with some values you like for example

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();



正如你可以看到你可以为每个属性自己的源(。使用< ...&来源GT;())。
目前已经在项目中一些默认的来源,但我也很喜欢下面做了一些我自己的:

As you can see you can provide for each property your own Source (.Use<...Source>()). There are already some default sources within the project but i also made some on my own like the following:

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;
    }
}



所以,你可以为每种类型创建自己的源和事后很容易创建一批样品的对象。

So you could create your own source for each type and afterwards quite easy create a bunch of sample objects.

这篇关于如何获得LogicalTypes随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆