16 Feb 2012

One Note about mvc 3

We need to tell MVC that we want to use the NinjectController class to create controller objects,
repository pattern
Listing 7-4. The IProductRepository Interface File
   1: using System.Linq;
   2: using SportsStore.Domain.Entities;
   3: namespace SportsStore.Domain.Abstract {
   4:     public interface IProductRepository {
   5:         IQueryable<Product> Products { get; }
   6:     }
   7: }
This interface uses the IQueryable<T> interface to allow a sequence of Product objects to be obtained, without saying anything about how or where the data is stored or how it will be retrieved.

Making a Mock Repository
Listing 7-5. Adding the Mock IProductRepository Implementation
   1: private void AddBindings() {
   2:     // Mock implementation of the IProductRepository Interface
   3:     Mock<IProductRepository> mock = new Mock<IProductRepository>();
   4:     mock.Setup(m => m.Products).Returns(new List<Product> {
   5:         new Product { Name = "Football", Price = 25 },
   6:         new Product { Name = "Surf board", Price = 179 },
   7:         new Product { Name = "Running shoes", Price = 95 }
   8:     }.AsQueryable());
   9:     ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
  10: }

Displaying a List of Products
Adding a Controller
Listing 7-6. The Empty ProductController Class
   1: using System.Linq;
   2: using System.Web.Mvc;
   3: using SportsStore.Domain.Abstract;
   4: namespace SportsStore.WebUI.Controllers {
   5: public class ProductController : Controller {
   6:     private IProductRepository repository;
   7:     public ProductController(IProductRepository productRepository) {
   8:         repository = productRepository;
   9:     }
  10: }
  11: }

Listing 7-8. The List.cshtml View
   1: @model IEnumerable<SportsStore.Domain.Entities.Product>
   2: @{
   3:     ViewBag.Title = "Products";
   4: }
   5:  
   6: @foreach (var p in Model)
   7: {
   8:     <div class="item">
   9:         <h3>@p.Name</h3>
  10:         @p.Description
  11:         <h4>@p.Price.ToString("c")</h4>
  12:     </div>
  13: }

■ Tip Notice that we converted the Price property to a string using the ToString("c") method, which renders numerical values as currency, according to the culture settings that are in effect on your server. For example, if the server is set up as en-US, then (1002.3).ToString("c") will return $1,002.30, but if the server is set to fr-FR, then the same method will return 1 002,30 €. You can change the culture setting for your server by adding a section to the Web.config <system.web> node like this: <globalization culture="fr-FR" uiCulture="fr-FR" />.

Setting the Default Route
We do this by editing the statement in the RegisterRoutes method of Global.asax.cs, as shown in Listing 7-9.
Listing 7-9.
   1: public static void RegisterRoutes(RouteCollection routes)
   2: {
   3:     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   4:     routes.MapRoute(
   5:         "Default", // Route name
   6:         "{controller}/{action}/{id}", // URL with parameters
   7:         new { controller = "Product", action = "List", id = UrlParameter.Optional } 
   8:         // Parameter defaults
   9:     );
  10: }

Tip Notice that we have set the value of the controller in Listing 7-9 to be Product and not ProductController, which is the name of the class. This is a compulsory (Bắt buộc) ASP.NET MVC naming scheme, in which controller classes always end in Controller, and you omit this part of the name when referring to the class.

No comments: