Good practices for Asp.Net MVC 4 - 5

6

I need to know how to organize my project in the% cos_de% issue if there is more than one Model in my application.

For example, I have the Model student and Model teacher.

I create a class of type CRUD's to manage my database, but each model will require its own CRUDs.

Where will the CRUD's code be inserted? No DbContext ? In DbContext (a controller master to controller ) or in controller's itself of each model?

In the answer I ask you to pass an example code to two different models.

    
asked by anonymous 14.02.2014 / 04:57

1 answer

5

If I understand your question, you will need to have two directories in your project:

  • Controllers (will contain the business code for CRUDs);
    • .cs files, being one for each Model
  • Views (Will display information for each CRUD);
    • Subdirectories (one for each Model)
      • _CreateOrEdit.cshtml
      • Create.cshtml
      • Edit.cshtml
      • Index.cshtml
      • Details.cshtml
      • Delete.cshtml

A basic example of CRUD is from a country register (Model Country ). Remember that by rules the Controller name is in the plural, and the subdirectories of the Views as well.

Models \ Country.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MeuProjeto.Models
{
    [DisplayColumn("Name")]
    public class Country
    {
        [Key]
        public Guid CountryId { get; set; }

        [Required]
        [Display(Name = "Name", ResourceType = typeof(Resources.Language))]
        public String Name { get; set; }

        [Required]
        [StringLength(3)]
        [Display(Name = "Acronym", ResourceType = typeof(Resources.Language))]
        public String Acronym { get; set; }

        [Display(Name = "States", ResourceType = typeof(Resources.Language))]
        public virtual ICollection<State> States { get; set; }

        [Display(Name = "LastModified", ResourceType = typeof(Resources.Language))]
        public DateTime LastModified { get; set; }
        [Display(Name = "CreatedOn", ResourceType = typeof(Resources.Language))]
        public DateTime CreatedOn { get; set; }
    }
}

Controllers \ CountriesController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MeuProjeto.Models;
using MeuProjeto.Resources;

namespace MeuProjeto.Controllers
{   
    public class CountriesController : MeuProjetoController
    {
        private MeuProjetoContext context = new MeuProjetoContext();

        //
        // GET: /Countries/

        [Authorize(Roles = "Administradores")]
        public ViewResult Index()
        {
            return View(context.Countries.Include(country => country.States).ToList());
        }

        //
        // GET: /Countries/Details/5

        [Authorize(Roles = "Administradores")]
        public ViewResult Details(System.Guid id)
        {
            Country country = context.Countries.Single(x => x.CountryId == id);
            return View(country);
        }

        //
        // GET: /Countries/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Countries/Create

        [HttpPost]
        [Authorize(Roles = "Administradores")]
        public ActionResult Create(Country country)
        {
            if (ModelState.IsValid)
            {
                country.CountryId = Guid.NewGuid();
                context.Countries.Add(country);
                context.SaveChanges();

                return RedirectToAction("Index");  
            }

            return View(country);
        }

        //
        // GET: /Countries/Edit/5

        [Authorize(Roles = "Administradores")]
        public ActionResult Edit(System.Guid id)
        {
            Country country = context.Countries.Single(x => x.CountryId == id);
            return View(country);
        }

        //
        // POST: /Countries/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administradores")]
        public ActionResult Edit(Country country)
        {
            if (ModelState.IsValid)
            {
                context.Entry(country).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(country);
        }

        //
        // GET: /Countries/Delete/5

        [Authorize(Roles = "Administradores")]
        public ActionResult Delete(System.Guid id)
        {
            Country country = context.Countries.Single(x => x.CountryId == id);
            return View(country);
        }

        //
        // POST: /Countries/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize(Roles = "Administradores")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            Country country = context.Countries.Single(x => x.CountryId == id);
            context.Countries.Remove(country);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                context.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

I do not think you need the codes for each View, but if you need to, just talk.

I do not know if I need to pass another Controller code because they are similar to each other, but if I really need it again, just talk.

    
14.02.2014 / 05:38