Dotnet
ASP.NET Web API with Victor Campos

Visualizing the Layers

Outline

Overview

We’ll be taking a look at the application from a aerial view. It’s simply to get an overall understanding of the structure. We’ll also go over the technology, the frameworks and the patterns we’ll be implementing.

Things like:

  • Database - for data persistence
  • Entity Framework - Object Relational Mapper
  • Repository Pattern - An abstraction layer that sits between the client and the data.
  • Dependency Injection (Ioc) - A technique used for having dependent objects injected during instantiation.
  • ASP.NET Core Web API - What makes the magic of Restful API EndPoints work correctly.
  • Generics - Just another method implemented by C# that allows us to reuse code by allowing the use of any data type.

Database

The database is where your data will be persisted. The tables we’ll create thru Data Migrations are:

Login accounts

We can’t secure an application without having accounts. We’ll learn how to securely store account information using encryption that is built right into .NET Core.

Profiles

Having the ability to separate the Person data from the accounts is key when it comes to hardening an application to protect it from unwanted intrusion. We’ll learn how to create a link between Accounts and Person’s Profiles to easily access the information for the logged in user without directly accessing persisted account information.

Articles

Articles or Editorials as I’ve called them here, is the bread & butter of the application. This is what drives traffic to a social site like Conduit. People come to read what others have to say.

Comments

This will allow other registered users to comment on Articles. It’s what keeps the conversation going in an Article.

Following

Once you find an interesting Person chances are you have found an Author that you like. Having the ability to follow the work that someone creates is key to bringing users back to your site and making it easy for them to find what is interesting.

Tagging

Having the ability to tag an article will make it easy for others to find things that peak their interest.

Favoriting

Just like bookmarking a page Favoriting an article keeps it handy and easy to find for future reference. Having a visual representation of the data structure will help understand the code that we’ll be writing.

Just think, we’ll never touch the database. All this will be created right from the very C# code we write together!

If you want a copy of this diagram I’ve included a shared link. Copy it to own drive and open it with Diagrams.net Project.

Object Relational Mapping (ORM)

The ORM that we’ll use to map the data is called Entity Framework. Using ADO.NET to access databases comes with its share of caveats. It can be prone to memory leaks if not used correctly. The notion of wrapping ADO.NET in an easy to use framework appeared on February 13, 2002.

As with any version 1.0 it’s never perfect. Entity Framework is no exception to that rule. However, throughout the years Entity Framework has gone through several iterations of improvement. The latest version allows you to abstract the logic of accessing the database. This allows you to easily change the database without modifying any other code in your application. I have to admit that this latest version has really raised the eye brow of many developers - including mine!

The Repository Pattern

This pattern is very well documented by one of my favorite software Developers by the name of Martin Fowler. The whole point of the Repository is to query the database, in our case we’ll ask the ORM to query the database. We might find ourselves querying results from several tables in the database. However, our final result may be a consolidated view model that the Front End requires. A repository performs the tasks of an intermediary between the domain model layers and data mapping, acting in a similar way to a set of domain objects in memory. Client objects declaratively build queries and send them to the repositories for answers. Conceptually, a repository encapsulates a set of objects stored in the database and operations that can be performed on them, providing a way that is closer to the persistence layer. Repositories, also, support the purpose of separating, clearly and in one direction, the dependency between the work domain and the data allocation or mapping.

Basically, it’s another layer of abstraction that bridges the logic between a client and the data. As we start writing the code you’ll easily identify the repository.

The Web API Controller

For every request, this is where the heavy lifting occurs. ASP.NET Core Web API Framework started as an open source project and to this day still is. The only difference is that the .NET Core Team has baked it in as part of the Common Libraries.

ASP.NET Core Built-In Middleware

ASP.NET Core Middleware that comes built-in contains code that every application needs to deal with:

  • Authentication & Authorization
  • Managing User Sessions
  • Controller Routing
  • Swagger API Document Implementation

Along with many other “boiler-plate” code, patterns and implementations.

Just imagine that all the spices and side dishes are included. We get to focus on the “meat & potatoes” of the application.

Dependency Injection

Another well documented technique widely used in OOP. It’s a form of the broader technique called Inversion of Control. An object should not have to know how to construct a dependency. Instead it should delegate the job the injector. Dependent object are not allowed to call the injector. The injector is responsible for creating the chain of dependent objects.

 

I finished! On to the next tutorial