Factory.Generator
1.0.5
dotnet add package Factory.Generator --version 1.0.5
NuGet\Install-Package Factory.Generator -Version 1.0.5
<PackageReference Include="Factory.Generator" Version="1.0.5" />
<PackageVersion Include="Factory.Generator" Version="1.0.5" />
<PackageReference Include="Factory.Generator" />
paket add Factory.Generator --version 1.0.5
#r "nuget: Factory.Generator, 1.0.5"
#:package Factory.Generator@1.0.5
#addin nuget:?package=Factory.Generator&version=1.0.5
#tool nuget:?package=Factory.Generator&version=1.0.5
Hi, This package for generation of fake data for test environment in .Net Core. After Installation, somewhere create folder Factories/ and put for any model (entity) one or more Factory file like this template, for example our Entity City:
using model_Refrence;
using My.Factory.Factories;
namespace YourNameSpace
{
public class CityFactory : IFactory<City>
{
private static readonly string[] fakes = new string[]
{
"fake string 1",
"fake string 2",
"fake string 3",
};
public City Generate()
{
var random = new Random();
return new City
{
Name = fakes[random.Next(fakes.Length)],
CreatedAt = DateTime.UtcNow
};
}
}
}
also you can use Bogus package for fake data:
using Bogus;
using model_Refrence;
using My.Factory.Factories;
namespace YourNameSpace
{
public class CityFactory : IFactory<City>
{
public City Generate()
{
return new Faker<City>("en")
.RuleFor(x => x.Name, f => f.Address.City())
.RuleFor(x => x.CreatedAt, _ => DateTime.UtcNow)
.Generate();
}
}
}
And You Can Call the Factory like This:
FactoryGenerator.GenerateAndSave<City, CityFactory>(count, appContext);
You can send third parameter is save in production mode or Not like:
FactoryGenerator.GenerateAndSave<City, CityFactory>(count, appContext, true);
also you can send forth parameter if you need to override some keys as Dictionary like:
var cities = FactoryGenerator.GenerateAndSave<City, CityFactory>(
5,
appContext,
new Dictionary<string, object?>
{
{ "Name", "new name" }
}
);
all Name keys set new name and other keys from fake data.
also if you need add relations first create factory for your relation entities and update calling the factory like this:
var goals = FactoryGenerator.GenerateAndSave<Goal, GoalFactory>(
5,
db,
relations: new HasOne<Goal, Plan, PlanFactory, int>(
g => g.PlanId
)
);
Goal is parent entity, Plan is relation entity, PlanFactory factory of relation entity and int is key type of foreign key.
You can easily add:
HasMany
new HasMany<Plan, GoalFactory, int>(p => p.Id, g => g.PlanId)
BelongsTo
new BelongsTo<Goal, UserFactory, Guid>(g => g.UserId)
also you can call instead relations like: Plan → Goal → Activity → Achievement
var achievement = FactoryGenerator.GenerateAndSave<Achievement, AchievementFactory>(
1,
_db,
relations: new IFactoryRelation<Achievement>[]
{
new HasOne<Achievement, Activity, ActivityFactory, int>(
a => a.ActivityId,
childRelations: new IFactoryRelation<Activity>[]
{
new HasOne<Activity, Goal, GoalFactory, int>(
a => a.GoalId,
childRelations: new IFactoryRelation<Goal>[]
{
new HasOne<Goal, Plan, PlanFactory, int>(
g => g.PlanId
)
}
)
}
)
}
).First();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Microsoft.EntityFrameworkCore (>= 9.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.