2013-08-27 3 views
0

J'ai ma classe de produit, qui est assez simple, Ensuite, je veux utiliser une interface pour envoyer un objet Liste des produits sur le contrôleur.Envoyer liste au contrôleur en utilisant l'interface?

Je pense que je suis bon avec ma classe de produit et l'interface IProduct?

mes problèmes: Dans la classe MYcontext, j'essaie de faire la liste, mais je ne suis pas vraiment sûr de savoir comment procéder. et la classe MyRepository devrait envoyer la liste au contrôleur par la suite.

Mon but d'utiliser l'interface est vraiment pour la pratique .. et je ferai des tests plus tard aussi.

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

Y a-t-il une question ici? –

+0

Au lieu de créer p1, p2, p3 .. etc .. vous pouvez créer la liste en faisant: Products.add (nouveau produit (21, "RollerSkates", 82.5, 164, 1.25)); ... Est-ce votre question? –

+0

désolé, je pourrais avoir été un peu peu clair dans ma question .. –

Répondre

1

Vous devez ajouter une logique getData à votre référentiel comme celui-ci (vérifier les produits de contexte, je ne suis pas sûr à ce sujet):

public class MyRepository : IProduct 
{ 

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

    } 

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

} 

Et votre interface

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

Et dans le contrôleur vous pouvez l'utiliser comme ceci:

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

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

Super cela semble aider beaucoup.^_^ –

+0

vous êtes les bienvenus! –

Questions connexes