asp.net MVC example Project "Suteki.shop" Analysis of Controller

Source: Internet
Author: User
Tags abstract

In the above, describes how to install and use Suteki, today we use the source to see how Suteki is using controller.

In Suteki, it uses the abstract method to define a controllerbase as the base class for all controller, and the following is a class diagram of its controller:

Some methods commonly used in controller are defined in the base class, such as adding metadescription,title to the current view:

[Rescue ("Default"), Authenticate, Copymessagefromtempdatatoviewdata]
Public abstract class Controllerbase:controller, Iprovidesbaseservice
{
Private Ibasecontrollerservice Basecontrollerservice;

<summary>
Supplies services and configuration to all controllers
</summary>
Public Ibasecontrollerservice Basecontrollerservice
{
get {return basecontrollerservice;}
Set
{
Basecontrollerservice = value;

viewdata["Title"] = "{0}{1}". With (
Basecontrollerservice.shopname,
Getcontrollername ());

viewdata["metadescription"] = "\" {0}\ "". With (basecontrollerservice.metadescription);
}
}

Public ILogger Logger {get; set;}

Public virtual string Getcontrollername ()
{
Return "-{0}". With (GetType (). Name.replace ("Controller", ""));
}


public virtual void Appendtitle (string text)
{
ViewData ["Title"] = "{0}-{1}". With (viewdata["Title"], text);
}

public virtual void Appendmetadescription (string text)
{
ViewData ["metadescription"] = text;
}

public string Message
{
get {return tempdata[' message ' As String;}
set {TempData ["message"] = value;}
}

protected override void Onexception (Exceptioncontext filtercontext) {
Response.Clear ();
Base. Onexception (Filtercontext);
}
}

Of course, a careful friend found that the abstract class also includes an instance of the Ibasecontrollerservice interface.

The interface of the main definition of some online shop system information, such as shop name, copyright information, email information, as follows:

public interface IBaseControllerService
{
IRepository<Category> CategoryRepository { get; }
string GoogleTrackingCode { get; set; }
string ShopName { get; set; }
string EmailAddress { get; set; }
string SiteUrl { get; }
string MetaDescription { get; set; }
string Copyright { get; set; }
string PhoneNumber { get; set; }
string SiteCss { get; set; }
}

The subclass "Basecontrollerservice", which implements the interface as the only one, is defined as follows:

public class BaseControllerService : IBaseControllerService
{
public IRepository<Category> CategoryRepository { get; private set; }
public string GoogleTrackingCode { get; set; }
public string MetaDescription { get; set; }
private string shopName;
private string emailAddress;
private string copyright;
private string phoneNumber;
private string siteCss;

..
}

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.