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

What are some web stacks using .NET for the back end development?

What are some web stacks using .NET for the back end development?

Microsoft has helped web developers develop web applications for a long time. There have been many updates in its framework and even after the release of so many other web development frameworks it has sustained itself in the market. The framework...

Microsoft has helped web developers develop web applications for a long time. There have been many updates in its framework and even after the release of so many other web development frameworks it has sustained itself in the market. The framework was released in the year 2002 and since then there have been many web applications that took its help for creating both front end and back end. With this, the developers can develop not just web-based applications but also form-based applications. Asp .net development services can be used by the developers to develop dynamic web applications that are in trend.

What is Back-End Development?

The back end of a web application is also known as the server-side. Basically, the back end of a web application consists of a server, a database and an application(Web server, Programming Language, Web Framework). All the work like programming the logic for the business, server-side hosting and deployment, working with the database, etc. are the things that are performed by the web developers under the back end development. The enterprises can hire a dot net programmer for the purpose of developing a good web application whose back end will also be strong and secure. A web development company uses many stacks for the purpose of developing a web application whose back end and front end are developed perfectly. The best web development services also use the stacks that the use of Asp .net development services for the purpose of back end development.

Using the .Net web stack:

The developers can use the .Net web stack for the purpose of back end web development too, generally .Net is used in the development of the middle tier of a web application but the web stack can also be used to develop the server-side or the back end. For years .Net has been a framework that has been trusted by many developers because of its rich features, and because its framework is battle-tested. Enterprises can hire a dot net programmer to develop a dynamic and interactive web application. One of the best web stacks in the world, Microsoft’s technology stack uses .Net for its web application development. The .Net stack is can be viewed as a subset of the overflow stack, it is a comprehensive technology stack that can fulfill the demands of the front-end, .Net and the database developers.

.Net stack can be combined with various open-source technologies to develop a wide variety of applications ranging from small scale server-side(Back-end) web apps to big enterprise-level and transaction processing system applications.

The .Net web stack consists of almost 60 frameworks, platforms, SDKs, SOA, IDE’s, Libraries, etc. It is spread over 13 different layers. The stack includes CLI languages like C#, F#, VB.NET, Fantom, etc.

Below are some of the key features of the .Net Web stack:

1. Interoperability

Interoperability means the quality of a system or a language or a framework to exchange and use the information. Interoperability between various platforms is a major advantage of using the .Net technology stack. It enables users to make use of a wide range of complementing features. The web development company that used the .Net tech stack will be benefitted very much because of this feature.

2. Dependence of Languages

With .Net tech stack, the web app developers get the freedom to use any language they prefer. Other stacks do not allow the developers to choose any language they choose, there are some specified languages that have been used by the developers while developing a web application with other stacks.

3. Portable

The stack is compatible with many other frameworks and platforms, which makes the portability of the application being developed very easy. This is one of the most important features of this stack.

4. Secure

Security is one of the most important aspects when any kind of application is developed, especially in the case of a web application, there are many hackers out there who can try to attack and gain control of the developed application. With the use of this stack, the developers can ensure the security of the code and the application that is being developed.

5. Speed

The projects developed through this stack can be completed faster and on time because of its high speed. The developers can ensure timely launches, and satisfaction of the stakeholders. The speed of the framework, the stack and the platform on which the application is being developed to play an important role in the development time and runtime.

There best web development services in the world want a web tech stack that can provide them with the best in class features and the features of the .Net stack look great. With so many benefits there should be no doubts on why .Net Stack should be used for the purpose of the development of web applications.

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