Friday, 8 April 2011

SproutCore Backend Using ASP.NET, MVC3, EF4, Humor Free

These are the key elements to implementing 'step-6' of the SproutCore todo's tutorial for asp.net/mvc.  Use  'Code-First Development with .NET 4 Entity Framework' to create and maintain the database schema.  Let the Entity Framework manage the objects so you don't have to.  To access, the data use ASP.NET MVC3.
Code exerpts follow:

Automatically generates the database schema when class is created:
namespace SproutCoreMVC.Models
{
    public class Task
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int id { get; set; }
        public string description { get; set; }
        public int order { get; set; }
        public bool isDone { get; set; }
        public string guid { get {return "/tasks/" + id;} }
    }

    public class SCModels : DbContext
    {
        public DbSet<Task> Tasks { get; set; }
    }

    public class SCModelsInitializer : DropCreateDatabaseIfModelChanges<SCModels>
    {
        protected override void Seed(SCModels context)
        {
            var tasks = new List<Task>
            {
                new Task {
                    order = 1,
                    isDone = false,
                    description = "Some Description"
                }
};

            tasks.ForEach(d => context.Tasks.Add(d));
            context.SaveChanges();
        }
    }
}
 
This is the REST implementation.  Easy da? 
namespace SproutCoreMVC.Controllers
{
    public class TasksController : Controller
    {
        SCModels db = new SCModels();

        // GET: /tasks        
        [HttpGet]
        [NoCache]
        public ActionResult List()
        {
            return Json(new {content = db.Tasks}, JsonRequestBehavior.AllowGet);
        }
        
        //GET: /tasks/id
        [HttpGet]
        [NoCache]
        public JsonResult Get(int id)
        {
            return Json(db.Tasks.Find(id), JsonRequestBehavior.AllowGet);
        }

        //PUT: /tasks/id
        [HttpPut]
        public ActionResult Update(Task task)
        {
            var taskToUpdate = db.Tasks.Find(task.id);
            UpdateModel(taskToUpdate);
            db.SaveChanges();
            return Json(taskToUpdate);
        }
        
        //POST: /tasks
        [HttpPost]
        public ActionResult Create(Task task)
        {
            db.Tasks.Add(task);
            db.SaveChanges();

            //set location and to redirect headers
            Response.StatusCode = 201;
            Response.RedirectLocation = task.guid;
            return new EmptyResult();
        }

        //DELETE: /tasks/id
        [HttpDelete]
        public ActionResult Delete(int id)
        {
            db.Tasks.Remove(db.Tasks.Find(id));
            db.SaveChanges();
            return new EmptyResult();
        }
    }
} 
I left out the routing information. As you can see the integration between SproutCore and asp.net MVC3 and Entity Framework 4 is quite straightforward and painless. I actually only write about 70 lines of code.  Is much easier than you half of project.

No comments:

Post a Comment