In-depth Analysis and Improvement Scheme of C # version MVC Framework puremvc

Source: Internet
Author: User

In puremvc, notification runs through the entire framework, giving full play to the observer mode. The layer-3 Communication of MVC is through notification. Notification consists of name and body. If notification is regarded as an email, the name is the recipient. However, in puremvc, multiple observers (observers) can receive the same email, and the body is the notification content. The relationship between notification and observer is 1: N, which can be seen in the Code of the view layer.

Code
Observermap= NewDictionary<String, ilist<Iobserver>();

 

The observer has two attributes:

Code
PrivateString y;
PrivateObject context;

Notify is the method name, and context is the carrier of the method. When the observer receives the notification, the following method is called.

 Code 
Public Void Notifyobserver (inotification notification)
{
Type T = This . Getpolicycontext (). GetType ();
Bindingflags F = Bindingflags. Instance | Bindingflags. Public | Bindingflags. ignorecase;
Methodinfo Mi = T. getmethod ( This . Getpolicymethod (), F );
Mi. Invoke ( This . Getpolicycontext (), New Object [] {notification });
}

To execute the notify method.

In puremvc, observer registration and notification observer are both performed at the view layer.

  Code   
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Public interface iview
{< br> void registerobserver (string icationicationname, iobserver observer);
void removeobserver (string icationicationname, object yycontext );
void policyobservers (inotification note);

I think this is not well designed, which leads to coupling between the view layer and the observer. These things should not be done by the view layer. In addition, the notification received by the observer is not only from the view, but also from the Controller and model. Therefore, according to the AOP principle, this part of the operation should be vertically separated from the MVC layer, change to horizontal mode. You can create an observer company obsertor to manage the observer in a unified manner, which can reduce the work of the view layer. The structure is as follows:

 

In puremvc, there is a class called consumer. According to the literal meaning, it should be the transfer of notification, that is, the postman. However, this class does not do its own job-to send notifications to the observer, but to push all notifications to the facade, and then the facade sends the notification to the observer through the view. I think this design is not reasonable. Previously, I have created an obsertor FOR THE obsertor, so as long as the observer directly sends the notification to the obsertor, The obsertor will send the notification to the specific observer. In reality, the company is like this, and often the front-end tells me that my package has arrived. Therefore, he should be asked to do his job.

The C # version of puremvc has a bug in mediator registration. Some mediator code is registered:

Code
Public VoidRegistermediator (imediator mediator)
{
If(Mediatormap. containskey (mediator. getmediatorname ()))Return;

 

It is intended to avoid repeated registration of mediator. When the page is reloaded, the mediator view in mediatormap is out of date, so that the page cannot be updated. My solution is

Code
Public VoidRegistermediator (imediator mediator)
{

If(Mediatormap. containskey (mediator. getmediatorname ()))
{
Mediatormap [mediator. getmediatorname ()]. setviewcomponent (mediator. getviewcomponent ());
Return;
}

Update the viewcomponent locally so that it runs normally.

I originally wanted to analyze the implementation mechanism of puremvc, but I found that my expression ability is limited and I have enough work to do. But on that day I came up with ideas and I would like to offer it to you. At the same time, I improved puremvc based on my own solutions to make it more suitable for C #, and then released the source code together. You may have to wait for a while because your work is busy.

 

 

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.