0

I have my Product class, which is pretty straight forward, Then I want to use an Interface to send a List of Product objects over the to Controller.

I think Im good to go with my Product class and IProduct interface ?

my problems: In MYcontext class, I am trying to make the list, but IM not really sure how to proceed. and MyRepository class should send the List to the Controller eventually.

My purpose of using the interface is just really for practice.. and I will do some Testing later too.

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; }

        }
    }
}
4

1 回答 1

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) ;
}
}
于 2013-08-27T14:03:22.997 回答