Introduction

In this article, we will discuss about a modular way to develop AngularJS single page applications for better maintenance. The example code demonstrates sign up and login functionality and you can improvise it as per your need. It is available here.

Background

You should have knowledge of the following technologies for understanding the code.

Take Away from this article

Drawbacks of traditional SPA approach

Advantages of my approach

Suppose, we are developing a dating site which has the following features.

Instead of considering these features as one application, we will separate the above functionality into three mini SPAs, as shown below.

  1. HomeSpa

    1. Welcome page
    2. SignUp
    3. Login
    4. External login

  2. SearchProfileSpa

    1. Profile search
    2. Advanced Search
    3. Individual profile

  3. PaymentSpa

    1. Payment by card
    2. Payment by paypal
Only a few functionalities have been implemented in the sample code that is available on GitHub here.

Sample Code Explanation

The sample code solution has the following layers.

  1. UI folder - Sample.MVC.Angular.UI - It is AngularJS project wrapped in ASP.NET MVC.
  2. ApplicationServices folder - DatingSite.WebApi4 - It is Web API layer which gets called in AngularJS code.
  3. DomainServices folder - Contains the projects related business layer which gets called in Web API projects.
  4. MongoDbAccess - Contains the projects related data access layer which gets called in Sample.DomainServices project.

You can focus only on the UI folder - Sample.MVC.Angular.UI project. The underlying layers follow the repository pattern and you can replace these layers by your business and data access layers easily.

The below image shows Mini SPA folder structure in the UI project.



For each mini SPA, we have an MVC Controller and a View page which will play a role of container page, just like ASP.NET MVC area and will hold all reference files of the related Mini SPA.

All Angular code goes under the Mini SPA folder and a sub folder for each mini SPA.

All mini SPAs are Angular modules and can be reused just in another module.

ModuleInitiator.js



This module creates or loads a module with required dependencies automatically when MiniSpas.ModuleInitiator.GetModule() method gets called.

_Layout.cshtml



The file contains references of files being used by all mini SPAs.

And, specific references, required only for a particular mini SPA, will be in the index.cshtml page which gets called by MVC Controller when mini SPA is requested.



Mini SPA structure

Each Mini SPA has each routing and module creation code. The code has two layers – Controller and Services, as shown below.



That’s it

You need to go through the code for better understanding. Let me know what you think of the approach/application architecture.