0

私は非常に簡単な製品クラスを持っています。次に、インターフェイスを使用して、製品オブジェクトのリストをコントローラーに送信したいと考えています。

私の Product クラスと IProduct インターフェイスを使用するのは良いと思いますか?

私の問題: MYcontext クラスで、リストを作成しようとしていますが、どのように進めればよいかわかりません。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; }

        }
    }
}
4

1 に答える 1

1

次のように getData ロジックをリポジトリに追加する必要があります (Context Products を確認してください。よくわかりません)。

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 に答える