Generating a fake data in .Net Core API

Generating a fake data in .Net Core API

A quick glance on how to generate a fake or random data in .net core web API

A quick glance on how to generate a fake or random data in .net core web API

Well, the other day, I was working on something where I needed some fake data or some random data to be generated in my .net core web API. On searching I ran into many blogs and links and I am sure there are many who have run into this scenario where we have to generate a fake data for some or the other reason.

Well there is a simple and easy way to do it, add a loop, create a new object of a class and add it to a list. Return the list.

Well all of us are aware of this. However, it is was pain in the neck to add this new and loops every time. I searched and found Genfu which generates the test data. I am like wow! Most of my work is done now. If you look at it, I will have to call a method from this GenFu package which is

GenFu.GenFu.ListOf<T>()

But again, I was thinking, I will have to write it at too many places. What else can be done here?

I was thinking to write a service for this, add it in the startup, and use it through DI in every controller. I somehow managed to do this. Here is my interface and the service that I am using for DI.

    public interface IDataGenerator<T> where T : class
    {
        /// <summary>
        /// Generates a collection of type T based on the properties in T
        /// </summary>
        /// <returns>List<T></returns>
        List<T> Collection();

        /// <summary>
        /// Generates the collection of type T of size = length 
        /// </summary>
        /// <param name="length">The size of the collection to be passed</param>
        /// <returns>A collection of type T based on the length passed</returns>
        List<T> Collection(int length);

        /// <summary>
        /// Generates an object of type T with data
        /// </summary>
        /// <returns>T with data based on the properties in T</returns>
        T Instance();
    }

And here is my class inheriting the interface.

    public class DataGeneratorService<T> : IDataGenerator<T>
        where T : class, new()
    {
        /// <summary>
        /// Generates a collection of type T based on the properties in T
        /// </summary>
        /// <returns>List<T></returns>
        public List<T> Collection() => GenFu.GenFu.ListOf<T>();

        /// <summary>
        /// Generates the collection of type T of size = length 
        /// </summary>
        /// <param name="length">The size of the collection to be passed</param>
        /// <returns>A collection of type T based on the length passed</returns>
        public List<T> Collection(int length) => GenFu.GenFu.ListOf<T>(length);

        /// <summary>
        /// Generates an object of type T with data
        /// </summary>
        /// <returns>T with data based on the properties in T</returns>
        public T Instance() => GenFu.GenFu.New<T>();
    }

Here, there can be many methods that can be added. GenFu gives way lot of things. I have shown a couple of them here. You can always try and play with them.

The method Collection will give me a list of type T and Collection(int length) will give me a list of size which is passed to the method.

Now here, don't forget to add the package Genfu. You can add it using package manager console like this:

install-package GenFu

That's it, now the only thing that remains is adding this to the startup. Since this is a generic thing, I have added this to the startup like this:

In your ConfigureServices method:

services.AddSingleton(typeof(IDataGenerator<>), typeof(DataGeneratorService<>));

Now before going to DI, consider a class like a Contact. This is a model class for which the data will be generated.

    public class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAdress { get; set; }
        public string PhoneNumber { get; set; }
    }

Now, I have a values controller, in which there is a HttpGet, that will give me a fake data for these contacts.

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IDataGenerator<Contact> _contactsGeneratorService;

    public ValuesController(IDataGenerator<Contact> dataGeneratorService)
    {
        _contactsGeneratorService = dataGeneratorService;
    }

    // GET: api/<controller>
    [HttpGet]
    public IEnumerable<Contact> Get()
    {
        var data = _contactsGeneratorService.Collection(100);
        return data;
    }
}

That's it ! I have a service ready. Whenever I need any fake data, I will just inject into my controller and run it.

**Originally published by **Praneet Nadkar *at *dev.to

MVC.Net Web Application Development Services| MVC.Net Expertise | Hire MVC.Net Developers

CMARIX is among leading MVC.Net web application Development Services. We have the inhouse team of MVC.Net expertise to build scalable, standards-based MVC architecture enterprise applications as per your business needs.

CMARIX is among leading MVC.Net web application Development Services. We have the inhouse team of MVC.Net expertise to build scalable, standards-based MVC architecture enterprise applications as per your business needs.

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company