Creating more family time with NuGet Packages

Creating more Family time with NuGet Packages II

In part I, I shared the background story and practical walkthrough for my first custom NuGet package called BabaFunke.DataAccess – a simple generic repository framework for CRUD (Create, Read, Update, Delete) operations which is typical of client/server Asp.Net web applications. Its sole purpose is to eliminate the need for some related repetitive tasks using the repository pattern. 

In this concluding piece, I create a simple product management project that utilises my NuGet package. This project is included in the GitHub repo for the NuGet package as BabaFunke.DataAccessDemo and includes a UnitTest project.

Say, like me, you are an app developer who’d like to create a dynamic website like https://geniigames.app/ for managing your apps, you could easily develop a product management system using Asp.Net Core + any of the popular Database systems. This way, you have a way of storing, updating, deleting your products. 

To keep things simple for this demo project, I use a pre-defined list of products instead of a database. Below, I share a simple walkthrough of the demo project showing snippets for some of the classes. Access the demo project in the GitHub repo for a complete implementation.

Create a Web Api Project

In Visual Studio, create a Asp.Net Core Web Api project.

Add the NuGet Package

Open the NuGet Package Manager and search for BabaFunke.DataAccess in the browse tab. Alternatively, simply enter the command line below into the Package Manager Console

Install-Package BabaFunke.DataAccess -Version 1.0.4

Add a Model

Create a class called Product; this is your model. Make sure it inherits the IPrimaryKey interface which ensures a property is implemented for the primary key. Note the addition of the namespace BabaFunke.DataAccess to make the interface accessible. I have also added other useful properties for a typical product. If your product was an App, the Title is the name of the app, Count could be the number of versions (Android, iOS), IsDisabled is used to determine the active or inactive status of the app and DateAdded is the date the product was added or created.

Add the Service

Create a class and inherit the class SqlServerEfRepository<T> from the BabaFunke.DataAccess NuGet Package. Remember to include the Product model in the angular brackets as it’s a generic. Then override the methods already implemented in the parent class.

If using Visual Studio, right-click the inherited class and select ‘Go to definition’ to see the class and methods to override. You can simply copy it from there. 

To override the implementation, add the async override keywords to each. 

My overridden implementations use a static DataManager class to mock the CRUD operations.

In this case, I’m going to mock a database operation using a list. I add a static class MockDatabase and a static method to replicate some of the CRUD operations.

I then register the interface and service to the Dependency Injection container in Startup.cs so it can be injected into the controller via its constructor

Note that if you have multiple services to register, you would still use the same IRepository<T> and simply replace the T with the model.

Lastly, add the Api controller and implement the calls for the different endpoints.

That’s it! Feel free to modify the code to suit your purpose. And if you need any help, feel free to contact me.