Click here to Skip to main content
15,891,136 members
Articles / Security

N - tier project with WCF OData service, Entity Framework, MVC3.0, Ninject DI, jSOn.net and Automapper

Rate me:
Please Sign up or sign in to vote.
4.62/5 (5 votes)
10 Dec 2012CPOL3 min read 39.5K   1.9K   41  
N-Tier application with WCF Odata service and Entity Framework.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eShopping.Business.IRepository;
using eShopping.ViewModels;
using Newtonsoft.Json;
using eShopping.ActionProvider;
using eShopping.Entities.Entities;
using AutoMapper;
using System.Web.UI.WebControls;
using eShopping.Common;

namespace eShopping.Controllers
{
    public class CustomerController : Controller
    {

        ICustomersRepository customerRepository;
        public int PageSize;
        public CustomerController(ICustomersRepository customerRepository)
        {
            this.customerRepository = customerRepository;
            PageSize = 20;
        }

        public ActionResult List(int page)
        {
            try
            {

                var customersViewModel = new CustomersViewModel
                {
                    Customers = customerRepository.Customers.OrderBy(x => x.CustomerID).Skip((page - 1) * PageSize).Take(PageSize),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = PageSize,
                        TotalItems = customerRepository.Customers.Count()
                    }

                };
                return View(customersViewModel);
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message == "Unauthorized")
                {
                    return RedirectToAction("login", "authentication");
                }
                else
                {
                    return View();
                }
            }
        }
        public ActionResult Orders(string customerId)
        {
            var orders = customerRepository.GetOrders(customerId);
            return new JResult
            {
                Data = orders.ToList()
            };
        }

        [HttpGet]
        public ActionResult Add()
        {
            var cityList = (from customer in customerRepository.Customers
                            select (new { customer.City })).ToMyList<string>("City");

            var CountriesList = (from customer in customerRepository.Customers
                                 select (new { customer.Country })).ToMyList<string>("Country");

            cityList.Insert(0, "--Select--");
            CountriesList.Insert(0, "--Select--");
            CustomerView custView = new CustomerView
            {
                Cities = new SelectList(cityList, cityList.FirstOrDefault()),
                Countries = new SelectList(CountriesList, CountriesList.FirstOrDefault())
            };
            return View(custView);
        }

        [HttpPost]
        public ActionResult Add(CustomerView customerView)
        {
            if (ModelState.IsValid)
            {
                Customer newCustomer = new Customer();

                AutoMapper.Mapper.Map(customerView, newCustomer);

                customerRepository.AddObject(newCustomer);
                customerRepository.SaveChanges();
            }
            return RedirectToAction("list");
        }

        [HttpGet]
        public ActionResult Delete(string customerId)
        {
            return View(customerRepository.Customers.Where(x => x.CustomerID == customerId).SingleOrDefault());
        }

        [HttpPost]
        public ActionResult Delete(Customer customer)
        {
            customerRepository.DeleteObject(customerRepository.Customers.Where(x => x.CustomerID == customer.CustomerID).SingleOrDefault());
            customerRepository.SaveChanges();
            return RedirectToAction("list");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Nagarro Softwares
India India
I am vijay tanwar and i am a software engineer with passion of programming. I love to programming in c#, I love to warp up more and more things in few lines of code. my favirote languages are c# and javascript and both are fully object oriended. I always like to become the .net Architect.

Comments and Discussions