使用接口将列表发送到Controller?

时间:2013-08-27 13:44:15

标签: c# list asp.net-mvc-4 interface

我有我的Product类,这很简单,然后我想使用一个接口通过控制器发送Product对象列表。

我认为我的产品类和IP产品界面很好用吗?

我的问题: 在MYcontext类中,我正在尝试制作列表,但IM不确定如何继续。   和MyRepository类最终应该将List发送给Controller。

我使用界面的目的只是练习..我稍后会做一些测试。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Uppgift_1.Models
{
    public class Product 
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public double PriceBuy { get; set; }
        public double PriceSell { get; set; }
        public double Moms { get; set; }

        public Product() 
        {

        }

        public Product(int productId, string productName, double priceBuy, double priceSell, double moms)
        {
            ProductId = productId;
            ProductName = productName;
            PriceBuy = priceBuy;
            PriceSell = priceSell;
            Moms = moms;
        }

        // TODO: add calculation method

    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Uppgift_1.Models
{
    public interface IProduct
    {
        IQueryable<Product> Products { get; }
    }
}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Uppgift_1.Models
{
    public class MYcontext : Product
    {
        IQueryable<Product> Products = new List<Product>();

        Product p1 = new Product(21, "RollerSkates", 82.5, 164, 1.25);
        Product p2 = new Product(22, "Fridge", 88, 844, 1.25);
        Product p3 = new Product(23, "TV", 182.5, 364, 1.25);
        Product p4 = new Product(24, "Boat", 325, 64, 1.25);
        Product p5 = new Product(25, "Car", 22.5, 74, 1.25);
        Product p6 = new Product(26, "Magasine", 84.3, 44, 1.25);
        Product p7 = new Product(27, "Garage", 182.7, 843, 1.25);
        Product p8 = new Product(28, "House", 182.8, 542, 1.25);
        Product p9 = new Product(29, "Beach", 814.9, 62, 1.25);
        Product p10 = new Product(30, "Ball", 69.3, 16, 1.25);


    }
}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Uppgift_1.Models
{
    public class MyRepository : IProduct
    {

        public IQueryable<Product> Products
        {
            get { return MYcontext.pList; }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将getData逻辑添加到您的存储库中(检查上下文产品,我不确定):

public class MyRepository : IProduct
{

    public IQueryable<Product> Products
    {
        get { return MYcontext.pList; }

    }

    public IQueryable<Product> GetProducts() {
        return (from obj in Products select obj).FirstOrDefault();
  }

}

和您的界面

namespace Uppgift_1.Models
{
    public interface IProduct
    {
        IQueryable<Product> GetProducts();
    }
}

在Controller中你可以像这样使用它:

public MyController:Controller {
     IProduct<Product> service = new MyRepository();

       public ActionResult Index() {
            var prods = service.GetProducts();
            return View(prods) ;
}
}