Asp.net MVC樣本項目“Suteki.Shop”分析之Model和Service

來源:互聯網
上載者:User

在Suteki.Shop中Model的原型是基於Linq to SQL建立的,其dbml檔案位於Suteki.Shop\Shop.dbml。 而Suteki.Shop在此檔案的基本上,以"partial class "的方式在Suteki.Shop\Model檔案夾下 建立了相應的類檔案以擴充Shop.dbml中Model類的一些方法和屬性聲明,如下圖:

為了便於 大家理解,下面以Model中的Product.cs為例進行說明。

Product是對網站中所銷售商品的資料信 息類。在其中定義了一些屬性(聲明在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連結

而Product.cs這個檔案其實是以partial方式對Shop.dbml中的Product類的 "擴充",下面是其實現代碼:

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().First().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
};

}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.