MVC Modular Architecture

Source: Internet
Author: User
Tags table definition

Comprehensive analysis of ASP. NET MVC Modular Architecture Scheme

What is architecture? Uncover the architectural mystery of the veil, nothing more than: Layering + modularity. Any complex architecture, you will also find that architects do both things.

This article will be a comprehensive introduction to our team's experience in modular design. The "full" word is added because the content of this article will involve: database, routing, C #, JavaScript, CSS, HTML and so on a complete module needs.

Before reading this article, you can also go to our Open source project: Https://github.com/leotsai/mvcsolution. This open source project is a complete summary of our team's layered architecture in ASP. NET MVC, as well as defining a number of standards.

The purpose of this paper is to introduce the modularization of architecture thought.

Current project architecture and modular architecture diagram

The operation of a module usually relies on a complete system. In the example of this article, Modularity relies on our open source project Mvcsolution this architecture. Let's take a look at the project reference diagram for this architecture:

The above architecture is the original architecture of the system, and on top of this original architecture, how do we add modules? Please see:

As can be seen, our added module refers to the system of the original multiple projects, although it will increase the coupling between the module and the current system, but it makes the development of the module much easier. For our team, because all projects are based on the Mvcsolution architecture, each module can be quickly integrated into every system.

Here's a look at the project file structure after adding the module:

Because our module is not big, so we put all C # files into a project, layered only in the mind. This also helps us to move this module to other projects.

1. Entrance to the module

The entrance of our module is very simple, no reflection, and there is no dynamic initialization, it is a normal System.Web.MvcAreaRegistration. In the Global.ascx.cs file, register all the referenced area by Arearegistration.registerallareas ();

In this arearegistration class, the main completion of the registration of the route and IOC registration, such as:

In this way, the public URL address of the module has been generated, such as: Http://www.example.com/donating. You can add this link elsewhere in the program, so the user can access the module.

2. Modular data access

How data access within a module can coexist with data access to existing systems is the most difficult problem for all modular architects, and data access is most difficult to handle compared to other files (C #, JS, CSS, HTML) because the following goal is difficult to achieve:

Target: The module can easily access all the contents of the existing database, but it is best not to make any changes to the existing database, only then, when the module is removed, will not leave any useless code;

In our practice, we can basically achieve this goal. Here's a detailed description of how we design for data access, based on EF Codefirst.

The legacy system uses a very complex and large database, and we want to make no changes to the newly added modules. The original database probably looks like this:

The file structure of our added modular engineering is as follows:

We are adding a new database Donatingdatacontext, of course, this database inherits from the original system database. This allows the newly added modules to have full access to the existing database. Take a look at the module database code:

View Code

Please note that the above code is base. Onmodelcreating (ModelBuilder); This sentence is very important, do not have to explain.

When adding a new module to the table, it is unavoidable to establish a foreign key relationship with the table of the original system. Normal practice, we are to add through entitytypeconfiguration<t>, such as:

However, in modular design, the definition of the foreign key relationship cannot be completed by entitytypeconfiguration<t> because the table definition cannot be modified in the original system. At this time, can only use dataannotations way:

Public Guid Creatoruserid {get; set;} [ForeignKey ("Creatoruserid")]public virtual User creatoruser {get; set;}

In this way, in the new database, you can freely access the existing database. As shown below:

1 public donatepost getpost (Guid PostID) 2 {3     using (var db = new Donatingdatacontext ()) 4     {5         return db. Donateposts.get (PostID); 6     }7}

When publishing to a product server, you only need to execute the new module's SQL script on the product database (you can generate a new library from EF and generate the script with Managementstudio).

3. Modular static files (JS, CSS, HTML)

As you can see from the file structure diagram at the very beginning of this article, we put almost all the static files in a folder called "Plugins/donating", which would be very useful for us to move this module into other projects.

Please note that the word "almost" in the above paragraph indicates that there is a small part of the file that is not here. This part of the special files are JS, there are two parts, part in the "_js-plugins/donating", the other part in the "_grunt/gruntfile.js". Used grunt all know, this is JS uncompressed to confuse the source files, you can also refer to our Open source project Mvcsolution Learn more about these two parts of JS design ideas. Simple will, these two parts JS in the publishing site, is to be deleted, do not need to copy to the server.

Below, take a look at the static file structure diagram:

Explains a lot of things, in addition to JS folder is generated by Grunt, the others are at a glance, no longer repeat.

For the cshtml file, in our example, is a layout of the original project referenced, including some basic CSS and JS, all have references to the original project. This adds to the coupling, but our projects are all based on the same architecture, and each level of code is within the standard, so it's easy to move the module.

Well, the introduction of ASP. NET MVC modularity is over, I don't know if you are looking at the foggy. No matter, when you decide to start experimenting with modular design, I believe some of the practices in this article can give you some inspiration.

At last

I hope you will speak freely about the views and practices in this article and say what you think.

MVC Modular Architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.