Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC

Listing 6-1. The Product Model Class

Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EssentialTools.Models
{
    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string Category { set; get; }
    }
}
View Code

Listing 6-2. The LinqValueCalculator Class

Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EssentialTools.Models
{
    public class LinqValueCalculator
    {
        public decimal ValueProducts(IEnumerable<Product> products)
        {
            return products.Sum(p => p.Price);
        }
    }
}
View Code

Listing 6-3. The ShoppingCart Class

Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EssentialTools.Models
{
    public class ShoppingCart
    {
        private LinqValueCalculator _calculator;

        public ShoppingCart(LinqValueCalculator calculator)
        {
            _calculator = calculator;
        }

        public IEnumerable<Product> Products { get; set; }

        public decimal CalculateProductTotal()
        {
            return _calculator.ValueProducts(Products);
        }
    }
}
View Code

Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC

上一篇:HTML页面规范


下一篇:C# 生成条形码、二维码