Skip to content

Seeding

Charles Solar edited this page Jun 13, 2018 · 2 revisions

Seeding is the process of creating fake data for the app so demo purposes. In this app we seed data by sending commands generated using Bogus.

Here is an example seed operation for user identities

// Define some random users
var bogus = new Faker<Types.User>()
    .StrictMode(false)
    .Rules((f, o) =>
    {
        o.UserName = f.Internet.UserName();
        o.Password = f.Internet.Password();
        o.GivenName = f.Name.FindName(withPrefix: false, withSuffix: false);
        o.Roles = new[] { "customer" };
    });

var users = bogus.Generate(20);
await ctx.LocalSaga(async bus =>
{
    foreach(var user in users)
    {
        await bus.CommandToDomain(new eShop.Identity.User.Commands.Register
        {
            GivenName = user.GivenName,
            UserName = user.UserName,
            Password = user.Password
        }).ConfigureAwait(false);

        foreach (var role in user.Roles)
        {
            await bus.CommandToDomain(new eShop.Identity.User.Entities.Role.Commands.Assign
            {
                UserName = user.UserName,
                RoleId = roleIds[role]
            }).ConfigureAwait(false);
        }
    }
}).ConfigureAwait(false);
Clone this wiki locally