Code Monkey home page Code Monkey logo

Comments (6)

bchavez avatar bchavez commented on June 2, 2024

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.

AhmedMagdu avatar AhmedMagdu commented on June 2, 2024

from bogus.

bchavez avatar bchavez commented on June 2, 2024

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

image

from bogus.

bchavez avatar bchavez commented on June 2, 2024

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.

AhmedMagdu avatar AhmedMagdu commented on June 2, 2024

from bogus.

bchavez avatar bchavez commented on June 2, 2024

If you're looking for "Automatic" mapping between DTO/Entities to Datasets; you can try looking for:

AutoBogus:

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:

Bogus.Tools.Analyzer

from bogus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.