ASP.NET Boilerplate – Step by step

Hi there,

I wrote this article to describe how I started to develop a full Web API Application and his web interface with a starter from ASP.Net BoilerPlate.
I choose the EntityFramework and AngularJs starter pack from the following link : http://www.aspnetboilerplate.com/

The architecture is well-documented and there are many samples, so I’m gonna just describe the steps by which I passed in order to take in hand the solution and starting develop by myself.

Steps summary :

  1. Code First & Code First Migration
  2. Repository
  3. Application services
  4. Web API
  5. User Interface
  6. Conclusion

Code First & Code First Migration

The entity classes which we wanna persist in our database have to be created in the Core project of the solution.
We are using code first, so it’s possible to play with Data Annotations to personalize tables name, schemas name, columns name, columns type, indexes..

I derive most of my entities from AuditedEntity(see documentation)
which automatically manage Id, date creation and author creation of my entities until their persistence in database.

To link this entities to EntityFramework context, I added for each of them a property inside the DbContext public IDbSet<MyClass> MyClass { get; set; }

Having years practicing EntityFramework DatabaseFirst, I had to get up to date about the framework to understand how to update the database.
Well, it’s very simple! We just need to use the migration tool provided by Entityframework which seems to be very complete. There are two migration modes:
– Automatic -> We let EntityFramework manage himself the database update.
– Manual (let’s say half manual) -> We create a migration class file and EntityFramework will use it automatically to update the database.

The following link describe the procedure to follow : Entity Framework Code First Migrations.

Repository

First of all, I started on the simplest solution which is to use the default repository provided by ABP who have some useful methods which are required to do CRUD operations : The IRepository.
In ABP, these repositories are directly used on the Application Layer. For instance, to define a variable which is my repository for my class MyClass, I proceed as follows : private IRepository<MyClass> myClassRepository;.

Application services


I created the service IMyAppService which inherit from IApplicationService. I implemented CRUD operations for my object MyClass to expose them for the Web API layer.

Web API

Nothing to do on this layer, ABP manage a generic Web API who’s based on the application services. You just have to make inherit your application services from IApplicationService, great!

User Interface

By default the application redirect us to the Account page in order to login. I want to have an home page accessible for all without registering, so I removed the [AbpMvcAuthorize] attribute of the HomeController to let the home page accessible for every one.
I also updated the header.js and remove the appSession service from the constructor and the vm.getShownUserName function because no one must be connected to view the page.
To finish, I updated too the header.cshtml file to remove the use of the current user.
I just injected my web service in the angular constructor controller to use it on the UI.
That’s all, the front layer is ready to build with the angularjs framework and the power of Razor.
Let’s the code begin!!

Conclusion

The architecture of the template is build on the latest technologies, so it’s a good starter for developers who wanna develop a projet almost from scratch, all layers are configurates. If you are searching a .Net CMS, go your way, you have to develop yourself all functionalities.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *