2013-08-27 4 views
0

제품 클래스가 꽤 있습니다. 인터페이스를 사용하여 컨트롤러를 통해 Product 개체 목록을 보내고 싶습니다.인터페이스를 사용하여 컨트롤러에 목록 보내기?

제 제품 클래스 및 IProduct 인터페이스와 함께 사용하는 것이 좋습니다.

내 문제 : MYcontext 클래스에서 목록을 만들려고 시도하지만 IM이 진행 방법을 잘 모르겠습니다. 및 MyRepository 클래스는 목록을 컨트롤러로 보내야합니다.

인터페이스를 사용하려는 나의 목적은 연습을위한 것입니다. 나중에 테스트도 수행 할 것입니다.

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

p1, p2, p3 .. 등을 만드는 대신 다음을 수행하여 목록을 만들 수 있습니다. Products.add (new Product (21, "RollerSkates", 82.5, 164, 1.25)); ... 그게 당신의 질문입니까? –

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

그리고 컨트롤러에 다음과 같이 사용할 수 있습니다.

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

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

위대한이 많은 도움이 될 것 같습니다.^_^ –

+0

당신을 환영합니다! –

관련 문제