Tuesday, November 13, 2018

Part 8 - Server Side Repository Setup for your Mobile App

If you have been following along we have just finished up creating our database using the code-first method and found how easy it is to create and publish database objects that are tied to your server side code.  Now that we have the database objects setup, we need to create a little bit of abstraction from them such that we can consolidate functionality, handle ad-hoc changes to the database, and implement further caching as needed for any necessary speed improvements.  This isn't necessarily required, but will prove invaluable as we develop our application.

To do this, we'll implement data repositories that will handle all interactions with our data.  We should keep these repositories geared toward the main ideas of our database.  This will help us classify the data access methods and keep us from creating repository objects we don't need to manage for a particular request from the application further helping us improve our application response times.  At this point, our application only has three main areas and not many details.

As such, we'll create three repositories for our invoicing application: one for Invoices, one for Clients, and one for Users.  Before we get into the specifics, let's create a generic repository base class to help share functionality between our repositories.

Open Visual Studio 2017 to our Invoicing solution.  Right-click on the Repositories folder in the Invoicing.Core project that we created in our first post and add a new class named Repository.

I made the class public and since this is meant to be a generic base class, I added the generic TEntity attribute and specified it must be derived from EntityBase, which all of our database objects will be derived from in some fashion.  I added the constructor with the ApplicationDbContext as the only parameter and save it to a protected field such that it is accessible in our sub-classes.  I also instantiated an empty DbSet to store temporary/cached results.

image

We'll add to the base class later in this post.  At this point, we can save this and continue to the creation of the specific repositories we are interested in creating.  We'll start with the ClientRepository.  Right-click on Repositories folder and add this class inheriting from the Repository base class and specify the Client model as the entity to be used as the targeted data object.  Create the default constructor as required by the parent class and pass along the context to that class.

image

Create your InvoiceRepository next.  It should look very similar to the one you just created.

image

Lastly, we'll create an ApplicationUserRepository.  You shouldn't be overly surprised at this point on what this one looks like.

image

Next, we'll implement a couple base class methods such that these repositories are somewhat usable with little or no extra effort.

Open the Repository base class again and add a new method to obtain the total count of TEntity records and return them for the user.  The method should look as it does below.  You will need to add a using statement for System.Linq to resolve the red squiggly.

image

Now let's add some basic CRUD (Create, Read, Update, Delete) methods to expose to classes that may be using this repository.

image

Your Repository base class should now resemble the below screen shot.  We'll add more to this class as we go to help with other common requests from our database.

image

That's it for the repository creation for now.  In the next post, we will take a look at the "Unit of Work" pattern that will manage our various repositories to ensure we maintain consistent creation of our repositories and communication to the database.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.