asp.net MVC example Project "Suteki.shop" Analysis of the filter

Source: Internet
Author: User

There are two ways to use filter in Suteki.shop, one that is inherited and implemented from FilterAttribute (abstract class properties) and interface Iactionfilter and Iresultfilter. The other is the way we often refer to inheriting from ActionFilterAttribute to achieve our own actionfilter. First, take a look at the first one, and it is also the way the project is widely used by the action, the following is a class diagram:

Of course, the core of the graph is Filterusingattribute, which inherits the FilterAttribute class and Iauthorizationfilter, Iactionfilter, iresultfilter these three interfaces, So the functionality it implements is the same as the ActionFilterAttribute defined in MVC. At the same time, here is the core code:

public class Filterusingattribute:filterattribute, Iauthorizationfilter, Iactionfilter, Iresultfilter
{
Private ReadOnly Type FilterType;
Private Object Instantiatedfilter;

Public Filterusingattribute (Type filtertype)
{
if (! Isfiltertype (FilterType))
{
throw new InvalidOperationException (' Type ' {0} ' is not valid within the filterusing
attribute as it is not a filter type. ". With (Filtertype.name));
}
This.filtertype = FilterType;
}

private bool Isfiltertype (type type)
{
Return typeof (Iauthorizationfilter). IsAssignableFrom (Type)
|| typeof (Iactionfilter). IsAssignableFrom (Type)
|| typeof (Iresultfilter). IsAssignableFrom (type);
}

Public Type FilterType
{
get {return filtertype;}
}

Private T getfilter<t> () where T:class
{
if (Instantiatedfilter = null)
{
Instantiatedfilter = ServiceLocator.Current.GetInstance (FilterType);
}
return Instantiatedfilter as T;
}

private void Executefilterwhenitis<tfilter> (action<tfilter> Action) where Tfilter:class
{
var filter = getfilter<tfilter> ();

if (filter!= null)
{
Action (filter);
}
}

public void Onauthorization (AuthorizationContext filtercontext)
{
Executefilterwhenitis<iauthorizationfilter> (f => f.onauthorization (Filtercontext));
}

public void onactionexecuting (ActionExecutingContext filtercontext)
{
Executefilterwhenitis<iactionfilter> (f => f.onactionexecuting (Filtercontext));
}

public void onactionexecuted (ActionExecutedContext filtercontext)
{
Executefilterwhenitis<iactionfilter> (f => f.onactionexecuted (Filtercontext));
}

public void onresultexecuting (ResultExecutingContext filtercontext)
{
Executefilterwhenitis<iresultfilter> (f => f.onresultexecuting (Filtercontext));
}

public void onresultexecuted (ResultExecutedContext filtercontext)
{
Executefilterwhenitis<iresultfilter> (f => f.onresultexecuted (Filtercontext));
}
}

At the top of the onaction. and Onresult. event, this generic method is invoked to manipulate the corresponding iactionfilter used in the generic constraint, and the work of obtaining the corresponding filter instance is executefilterwhenitis to the getfilter<t The > () method, because the method uses IOC to create the FilterType as a serviced component, so we'll see the Containerbuilder (suteki.shop\ ContainerBuilder.cs) has the following code, note the last line:

container.Register(
Component.For<IUnitOfWorkManager> ().ImplementedBy<LinqToSqlUnitOfWorkManager>().LifeStyle.Transient,
Component.For<IFormsAuthentication> ().ImplementedBy<FormsAuthenticationWrapper>(),
Component.For<IServiceLocator>().Instance(new WindsorServiceLocator (container)),

It appears that it will eventually use the IOC functionality provided by the Castle framework.

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.