Comments (6)
Hi @AhmedMagdu , you'll need to be more specific with your questions:
how to make this code generic or dynamic
if have entityDto is customer how to make map properties of person in Faker
customer have some properties and have List about customerAddresses how to make actually map with Address in Faker
What do you mean "more generic or dynamic"? What problem are you trying to solve?
from bogus.
from bogus.
If you're talking about mapping entities to DTOs, then you'll need to use some kind of object mapping tool like ValueInjector
, AutoMapper
, Mapster
, Mapperly
, TinyMapper
, or some other utility library to translate object values between classes.
Object translations or transformations into other objects is not a concern of Bogus. Here is a blog post that goes into different kinds of mapping tools and utility libraries:
Finally, here's a fixed up version using ValueInjecter
:
void Main()
{
var fakeCustomers = InsertFakeDataCustomer(3);
fakeCustomers.Dump();
}
public static List<CustomerInsertUpdateDto> InsertFakeDataCustomer(int count)
{
var customerFaker = new Faker<Customer>()
.RuleFor(c => c.Name, f => f.Person.FirstName)
.RuleFor(c => c.ForeignName, f => f.Person.LastName)
.RuleFor(c => c.Email, f => f.Person.Email)
.RuleFor(c => c.Phone, f => f.Phone.PhoneNumber());
var fakeCustomers = customerFaker.Generate(count);
var customerInsertDtos = fakeCustomers.Select(c =>
{
var customerDto = Mapper.Map<CustomerInsertUpdateDto>(c);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // here is the ValueInjecter mapping
customerDto.CustomerAddresses = GenerateFakeAddresses();
return customerDto;
})
.ToList();
return customerInsertDtos;
}
private static List<CustomerAddressUpdateDto> GenerateFakeAddresses()
{
var faker = new Faker<CustomerAddressUpdateDto>()
.RuleFor(a => a.Zone, f => f.Address.City())
.RuleFor(a => a.Floor, f => f.Address.StreetName())
.RuleFor(a => a.RegionName, f => f.Address.City())
.RuleFor(a => a.Apartment, f => f.Address.BuildingNumber());
int numberOfAddresses = new Random().Next(1, 4);
return faker.Generate(numberOfAddresses);
}
public class Customer
{
public string Name { get; set; }
public string ForeignName { get; set; }
public string Email { get; set; }
public string Phone { get; set;}
}
public class CustomerInsertUpdateDto
{
public string Name { get; set; }
public string ForeignName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public List<CustomerAddressUpdateDto> CustomerAddresses { get; set;}
}
public class CustomerAddressUpdateDto{
public string Zone { get; set; }
public string Floor { get;set; }
public string RegionName { get; set; }
public string Apartment { get; set; }
}
OUTPUT
from bogus.
Also, consider just creating Faker<T>
s for your DTOs without having to go to Entity -> DTO
translation; eg:
new Faker<CustomerInsertUpdateDto>().RuleFor(....)...
new Faker<CustomerAddressUpdateDto>().RuleFor(...)...
Ultimately, you might not need new Faker<Customer>()
if all you're doing is creating CustomerInsertUpdateDto
in the first place; then just use new Faker<CustomerInsertUpdateDto>()
instead of new Faker<Customer>()
.
from bogus.
from bogus.
If you're looking for "Automatic" mapping between DTO/Entities to Datasets; you can try looking for:
AutoBogus:
- https://github.com/soenneker/soenneker.utils.autobogus
- https://github.com/nickdodd79/AutoBogus
- And specifically,
AutoBogus.Conventions
- https://www.nuget.org/packages/AutoBogus.Conventions
- And specifically,
Other than "Conventions", there is no way to automatically "generate" mappings between DTO/Entities and Datasets. A human must be involved in the process to map value generation to class entity/dto properties.
Tangentially, there is IDE tooling like Bogus.Tools.Analyzer
that help "generate" .RuleFor()
values, but they are default value rules, and you'll still need to specify what datasets are needed and assigned to entity/dto properties:
from bogus.
Related Issues (20)
- Faker.Tv() is missing from Bogus.Hollywood HOT 3
- Feature request: New localized data: Word(s).
- Add ability to except random numbers HOT 2
- Update Denmark (da) locale from faker-js
- Nuget for .NET 8.0.x HOT 4
- Random history facts generation
- Are Randomizer's number ranges inclusive or exclusive? HOT 3
- bogus is missing NuGet package README file HOT 1
- .NET 8 support HOT 9
- Gender-digit is incorrect for Swedish Personnummer generation HOT 3
- Switch to licence expression rather than URL HOT 1
- Invalid Iban generation for CR (Costa Rica) HOT 3
- Add hungarian (hu) locale from faker-js HOT 2
- Fakers used inside Rules of other Fakers share the same object reference HOT 2
- Determinism with Custom Datasets HOT 1
- Include the increment count for the Rules Expressions HOT 1
- Stack memory issue in locale tests (overflows on Mac) HOT 2
- Update `zh_CN` dataset to latest HOT 3
- private setters workaround is not working for generic types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bogus.