asp.net MVC example Project "Suteki.shop" Analysis model and service

Source: Internet
Author: User
Tags bool

Model prototypes in Suteki.shop are created based on LINQ to SQL, and their dbml files are located in suteki.shop\shop.dbml. And suteki.shop in this file basically, in the way of "partial class" in the Suteki.shop\model folder to create a corresponding class file to extend the SHOP.DBML model class of some methods and property declarations, the following figure:

In order to facilitate understanding, the following model in the Product.cs as an example to explain.

Product is a class of data information about the items sold in a Web site. Some of the attributes are defined (declared in shop.dbml):

private int _ProductId;  // 产品ID
private int _CategoryId;    //产品所属分类ID
private string _Name;  // 产品名称
private string _Description;//产品描述
private decimal _Price;  //产 品价格
private int _Position;  //在列表中的位置
private int _Weight;  //重量
private bool _IsActive;  //当前是否激活显示
private string _UrlName;    //产品的 URL链接

And Product.cs This file is actually the "extension" of the product class in the shop.dbml in partial way, and here's the implementation code:

Public partial class product:iorderable, Iactivatable, iurlnamed
{
partial void Onnamechanging (string value)
{
Value. Label ("Name"). IsRequired ();
}

partial void onnamechanged ()
{
URLName = Name.tourlfriendly ();
}

partial void Ondescriptionchanging (string value)
{
Value. Label ("Description"). IsRequired ();
}

public bool Hasmainimage
{
Get
{
return this. Productimages.count > 0;
}
}

Public Image Mainimage
{
Get
{
if (hasmainimage) return this. Productimages.inorder (). A (). Image;
return null;
}
}

public bool Hassize
{
Get
{
return this. Sizes.active (). Count () > 0;
}
}

Public Size DefaultSize
{
Get
{
if (this. Sizes.count () = = 0) throw new ApplicationException ("Product has no default size");
return this. Sizes[0];
}
}

public string isactiveasstring
{
Get
{
if (isactive) return string. Empty;
Return ' not Active ';
}
}

public static Product defaultproduct (int parentcategory, int position)
{
return new Product
{
ProductId = 0,
CategoryID = Parentcategory,
Position = Position
};

}
}

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.