开发者

Generating a value dependent on another value with AutoPoco

I'm using the great .NET library AutoPoco for creating test and Seed Data.

In my model I have 2 date properties, StartDate and EndDate.

I want the EndDate to be 3 hours after the start Date.

I've created a custom Data source for autopoco below that returns a random Datetime between a min and max date

class DefaultRandomDateSource : DatasourceBase<DateTime>
{
    private DateTime _MaxDate { get; set; }
    private DateTime _MinDate { get; set; }
    private Random _random { get; set; }


    public DefaultRandomDateSource(DateTime MaxDate, DateTime MinDate)
    {
        _MaxDate = MaxDate;
        _MinDate = MinDate;

    }

    public override DateTime Next(IGenerationSession session)
    {
        var tspan = _MaxDate - _MinDate;
        var rndSpan = new TimeSpan(0, _random.Next(0, (int) tspan.TotalMinutes), 0);


        return _MinDate + rndSpan;
    }

}

But in AutoPoco's configuration how can i get my EndDate to be say, 3 hours after the autogenerated start Date?

Here's the autopoco config

 IGenerationSessionFactory factory = AutoPocoContainer.Configure(x =>
            {
                x.Conventions(c => { c.UseDefaultConventions(); });
                x.AddFromAssemblyContainingType<Meeting>();
                x.Include&l开发者_开发技巧t;Meeting>()
                    .Setup((c => c.CreatedBy)).Use<FirstNameSource>()
                    .Setup(c => c.StartDate).Use<DefaultRandomDateSource>(DateTime.Parse("21/05/2011"), DateTime.Parse("21/05/2012"));
            });


If I am correctly understanding the problem you need: to set EndDate from StartDate. I had to create a new DataSource and get current item which we are constructing and read value from it. I haven't thoroughly checked but it might fail if StartDate is set after EndDate (though I think the properties are set in the order they are setup, read source code for AutoPoco). Also I am using latest version from CodePlex as of today (20 Feb 2012).

public class MeetingsGenerator
{
    public static IList<Meeting> CreateMeeting(int count)
    {
        var factory = AutoPocoContainer.Configure(x =>
        {
            x.Conventions(c => { c.UseDefaultConventions(); });
            x.Include<Meeting>()
                .Setup((c => c.CreatedBy)).Use<FirstNameSource>()
                .Setup(c => c.StartDate).Use<DefaultRandomDateSource>
                                  (DateTime.Parse("21/May/2012"), 
                                    DateTime.Parse("21/May/2011"))
                .Setup(c => c.EndDate).Use<MeetingEndDateSource>(0, 8);
        });
        return factory.CreateSession().List<Meeting>(count).Get();
    }
}

public class Meeting
{
    public string CreatedBy { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

public class MeetingEndDateSource : DatasourceBase<DateTime>
{
    private int mMin;
    private int mMax;
    private Random mRandom = new Random(1337);

    public MeetingEndDateSource(int min, int max)
    {
        mMin = min;
        mMax = max;
    }

    public override DateTime Next(IGenerationContext context)
    {
        var node = (TypeGenerationContextNode)((context.Node).Parent);
        var item = node.Target) as Meeting;

        if (item == null)
            return DateTime.Now;

        return item.StartDate.AddHours(mRandom.Next(mMin, mMax + 1));
    }
}

class DefaultRandomDateSource : DatasourceBase<DateTime>
{
    private DateTime _MaxDate { get; set; }
    private DateTime _MinDate { get; set; }
    private Random _random = new Random(1337);

    public DefaultRandomDateSource(DateTime MaxDate, DateTime MinDate)
    {
        _MaxDate = MaxDate;
        _MinDate = MinDate;
    }

    public override DateTime Next(IGenerationContext context)
    {
        var tspan = _MaxDate - _MinDate;

        var rndSpan = new TimeSpan(0 
                                  , _random.Next(0, (int)tspan.TotalMinutes)
                                  , 0);

        return _MinDate + rndSpan;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜