Asp.net Mvc modular development: "A simple and pleasant journey to start module development and debugging", asp. netmvc

Source: Internet
Author: User

Asp.net Mvc modular development: "A simple and pleasant journey to start module development and debugging", asp. netmvc

All things in the world are divided into two opposite aspects.

The wealth that everyone desires is divided into wealth and poverty, height is divided into height and height, and body is divided into fat and thin, and so on.

We always lament that I don't understand the life of rich people. Rich people can understand how we get up early and greedy every day. They can never make a trip.

The same is true for programmers. When you feel that you have learned something to interview an Internet company, it is very likely that you will face a flood of cold water.

The interviewer may euphemistically tell you that we are enterprise-level development and your project experience demonstrated by myself is hardly used in our actual projects;

What we do is distributed applications. Do you understand distributed applications? Our applications are all migrated to the cloud. Do you understand cloud computing?

What can you do if you are confused and angry? I can only sigh that I don't understand your code world.

The world is already like this, and we are not capable of eliminating poverty. We can only try our best to reduce the gap between the rich and the poor.

The same is true for project development. Where are the requirements of enterprise-level complex systems? We cannot reduce or change the requirements. I am trying to develop some tools to simplify enterprise-level or distributed development as a bridge, make it as similar as our simple application development.

Asp.net Mvc modular Development Series directory

1. Asp.net MVc modular development-partitioned extension framework (source code)

2. Asp.net Mvc modular development: "A simple and pleasant journey to start module development and debugging"

3. "reuse of logic (Project)" In Asp.net Mvc modular Development"

3.1 logic (Project) Reuse of different roles or permissions (Application of partition filters)

3.2. Reuse of different business logic (projects) (DI (dependency injection) applications)

4. Asp.net Mvc modular development: "project (partition) splitting"

5. Asp.net Mvc modular development: "updating some versions and modules (going online )"

 

After talking a lot about it, let's start our module development journey as soon as possible.

I. configure a partition project first

1. Create a common Mvc (4.0) project and add the reference (partition extension framework, Unity container (2.0), and Enterprise Library (4.0 ))

  

2. Configure containers and partitions

2.1 change the route configuration class to a partition class

 

 

In the above modified partition configuration (line Routing) class, I also specially reserved the old routing rules. In addition to the method signature, it is easy to copy the routing rules directly.

2.2 create a container configuration file (unity. config) and configure partitions in it (add an empty dependency injection container for backup)

  

We used the current project name (MvcApplication1) as the partition name and partition path to create a partition (I used to put this configuration file in the ConfigFiles folder under the project, you can place them anywhere according to your habits)

Note that the path configuration (path) of this partition is specially annotated, that is, the default path is not configured, that is, the browsing path is the same as that of the default Mvc project.

2.3 Comment out the routing and filter registration code in Global. asax. cs

2.4 Add nodes that call unity configuration in Web. config.

Here, the Unity configuration is split into independent files, which can effectively reduce Web. the size and independence of config. We recommend that you keep the Web as clean as possible. config, each configuration file tries its best to achieve "single responsibility", which is of great help for program stability and future search problems.

2.5 Add the httpModule of the partition extension framework to system. webServer and system. Web in web. config.

System. web's httpModules works in the Classic mode, and system. webServer's modules works in the integration mode. If you are not sure that both are matched, it will be OK. This is also said in another article.

2.6 The configuration is complete. Now add a test control.

Now let's test it (directly F5 ):

We can see that this interface is full of joy. We have successfully expanded the Mvc partition project, without using the default routing and filter methods.

Some people say that by default, you can directly create an Mvc project F5 to see your interface. It takes six steps to configure it, which is too troublesome.

Have you found that the above steps are both mechanical and simple configuration? familiar people only need to press ctrl + C and ctrl + V to complete the process in a few minutes.

Of course, you can also create a code generation tool (T4 template, CodeSmith, etc.) with one click. You can even create an extended Mvc Project template, which is generated just like the default Mvc project. I will not expand it here.

 

Ii. Continue to explore the partition framework

1. Some people may say, isn't it a good idea to develop modules? It is impossible for each module to be the root directory, which may conflict.

Haha. You can configure the partition path. You only need to set a partition path.

In this case, you can configure two (and more) partitions for the same development project, one of which is the default configuration path for convenient development and debugging, and the other is the simulated deployment path for configuration, preview after deployment at any time>

The partition configuration diagram is as follows:

The above partition configuration nodes are all placed in the Mvc container, which is written 4 in the partition framework, this configuration is required to take effect (for more information about the partition framework introduction and source code)

Let's take a look at the route table:

Compared with the previous configuration, there is an additional partition configuration node in front of it? To avoid route rule conflicts, if the routing rules are based on the default partition,/Test/will match a controller of the default partition, rather than the default Controller under a partition.

The effect of the two shards is as follows:

Haha, not bad. You can perform the same simple debugging as the default Mvc project, without delaying the simulation of the deployment path.

 

2. Some people say that the Views of the two partitions are not scientific in the root directory. The actual deployment environment will conflict.

Yes, view conflicts cannot be solved. How can this be called modular development? In fact, this Asp.net Mvc framework has been fixed for us.

2.1 let's create an Action (Detail) to try it out. Do not create a view first and let it go wrong, because this error message contains a large amount of useful information.

The Action code is simple:

The browsing effect is as follows:

If something goes wrong, don't be afraid. What we need is the error information.

 

2.2 Let's analyze this error message.

This is because the Mvc framework cannot find the appropriate view to report the error. In fact, it guides us to store view files according to the above rules.

We can ignore the previous aspx and ascx (web form user control. cshtml path (. vbhtml is used to the vb language. Most people use C #, which can be ignored)

The partition view first looks for the path :~ /Areas/{areaName}/Views/{controllerName}/{actionName}. cshtml

Next, find the Shared folder of the current partition :~ /Areas/{areaName}/Views/Shared/{actionName}. cshtml

Ignore the view folder search of the partition in the root directory again :~ /Views/{controllerName}/{actionName}. cshtml

Finally, search for the shared view folder in the root directory :~ /Views/Shared/{actionName}. cshtml

As long as you master the above rules, you can easily avoid view conflicts.

We also found that if the Mvc framework cannot find the partition folder (containing the view meeting the requirements), it will find the view based on the view path of the common project, which makes it easier for us to develop modular (partition) based on the common project) project

As long as we copy the view in the corresponding partition project to the partition view folder corresponding to the site during deployment.

2.3 In fact, this view path is configured by ViewEngines. Engines.

First, we can see that Mvc preferentially supports the WebForm view. If the performance is paranoid, we can remove the WebForm view engine.

2. the paths of the partition view and default view can be configured and customized. I think the default rules of the Razor view engine are enough.

 

2.4 let's end this small topic with two view deployment Effects

In the Action (Detail) view of the preceding two partitions, one is placed in the partition view path, and the other is placed in the normal view path.

Once again, it is not necessary to create a partition path (Areas and the following partitions and view folders) in the project during actual development. You only need to develop it according to the normal Mvc project, you only need to copy the data to the corresponding partition folder during deployment.

 

Iii. Continue mining of the partition extension framework

The above describes how to develop a common Mvc project using the partition framework as much as possible. It can be explained at most. After expansion, there is no difference with "no extension". Some people will be scolded, what do you think about.

"No difference" is the sophistry of the concept of stealing. If the extended Mvc partition framework can only be achieved here, there is indeed a lot of regret. Now I am exploring the convenience brought by the extended partition framework for my development projects.

1. Simple, efficient, and considerate partition Configuration

1.1 Add the required configuration items to the partition class.

Modify partition type:

Configuration file modification:

Test program modification Verification:

The above uses RouteConfig. GetCurrent ()

Preview the results:

The results are good.

Some people may say, "simple? I may as well configure a PageSize node in the appSettings of Web. Config.

In this way, the configuration method is "problematic", but it still has a great advantage for the deleettings;

First, we need to implement modular development and make preparations for deploying other projects to the same site. If another project is configured with a PageSize node, it will conflict.

2. As I have already mentioned above, I want to prepare for deploying multiple partitions on the same site for the same project, the number of two partitions is 18 and 10, which does not affect each other.

Can we make it easier? Of course we can. Please continue.

1.2 use dependency injection container configuration

Add a controller for the test, add a PageSize attribute, and declare it as Dependency. This means that the ListPageSize node is used, and the Action Code is much simpler than the previous example. You do not need to obtain information about the current partition.

The configuration also changes. In order to distinguish two partitions, a dependency injection container is added, and the dependency injection container is configured with the corresponding ListPageSize node and controller respectively.

On:

Is it much simpler? Although simple, it contains a wealth of meanings.

1. The controller defines the corresponding attributes for any configuration (object) and does not need to define and call methods for obtaining the configuration in the controller.

2. Each partition has an independent dependency injection space (container), so there will be no conflict between partitions.

3. Each configuration item is independent in the container and can be added dynamically without damaging the original structure (for example, the partition configuration class is modified in the previous example. If there are too many configuration items, partition classes will be too complex and difficult to maintain)

4. The configuration items to be called can be directly arranged and combined, and the configuration items can be taken highly, and the call point (Controller) can be adjusted at any time without damaging the overall project structure.

In fact, the convenience of the above development comes from the partition DI (dependency injection) function of the partition extension framework, which is also described in the partition framework introduction and source code article, this topic also needs to be opened for the article to continue mining.

 

This article has finally been written. Download the source code. This article is similar to the previous partition framework introduction and source code article. The main purpose of this article is to illustrate how to use the partition framework for module-based development is as simple as a common project and the partition framework provides more convenience. Some Mvc and partition framework details can be considered as a partial question. My starting point is to try to expand the part that everyone knows and knows.

Related Article

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.