Elements of DDD with ASP.NET MVC & Entity Framework Code First v2
MVC and Entity Framework
-
Upload
james-johnson -
Category
Technology
-
view
906 -
download
5
description
Transcript of MVC and Entity Framework
![Page 1: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/1.jpg)
ASP.NET MVC and Entity FrameworkUtah Code Camp
Saturday, September 25, 2010James Johnson
Technical Evangelist
![Page 2: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/2.jpg)
Technical Evangelist with ComponentOne Founder and President of the Inland
Empire .NET User’s Group Microsoft MVP
But I don’t consider myself an expert. I just love to play
ADHD/ADD/OCD when it comes to new technology
Can’t stay away from the shiny new stuff Please don’t drop any new coins during the
presentation
Who am I?
![Page 3: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/3.jpg)
Overview of ASP.NET MVC Overview of Entity Framework
Things that are cool Things to watch out for How to do it
Agenda
![Page 4: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/4.jpg)
Demo
![Page 5: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/5.jpg)
Models Views Controllers No Postbacks Very limited use of existing server controls Clean HTML makes CSS and JavaScript easier What all the cool kids are using these days.
ASP.NET MVC
![Page 6: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/6.jpg)
First version (V 1) came with .NET 3.5 SP1 August 2008 Not widely thought of by the community
Second version (V4) released with .NET 4 Maps POCO objects to Database objects A collection of things instead of a dataset of
rows “things” are the Entities
Entity Framework
![Page 7: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/7.jpg)
Why? Adds a layer of abstraction between Database
and Code DBA can structure DB how they want Developer can map to the DB how they want
Rename Entities for more comfortable use. EF handles the mapping
Entity Framework
![Page 8: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/8.jpg)
Entity Data Model – EDM Deals with the Entities and the Relationships
they use Entities
Instance of EntityType Represent individual instances of the objects
Customer, books, shoes Fully typed
Relationships V1 was difficult to work with relationships
Needed special tricks to load related data
Entity FrameworkDefinitions
![Page 9: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/9.jpg)
Adding an EDM to your project
Demo
![Page 10: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/10.jpg)
A design pattern to defer initialization until needed.
EF 4 fixes a lot of problems with thisSupports Lazy LoadingOFF by defaultObjectContext setting, not application setting
context.ContextOptions.DeferredLoadingEnabled=true;
List<Thing> things = context.Things.ToList();foreach(var thing in things){
var thingItems = thing.ThingItems}
Entity FrameworkLazy Loading
![Page 11: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/11.jpg)
Use if you will be needing every related entity
List<Thing> things = context.Things.Include(“ThingItems”);
foreach(var thing in things){
var thingItems = thing.ThingItems}
Entity FrameworkEager Loading
![Page 12: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/12.jpg)
The context is the instance of the entity Passing an entity around to tiers breaks the
context V4 handles this issue with “self-tracking”
entities Make sure the context is always the same
Entity FrameworkContexts
![Page 13: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/13.jpg)
Entity FrameworkContexts
public class ModelHelper { private static CourseEntities _db; public static CourseEntities CourseEntities { get { if(_db == null) _db = new CourseEntities(); return _db; } set { _db = value; } } }
private readonly CourseEntities _db = new CourseEntities();
![Page 14: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/14.jpg)
Entity FrameworkContexts
private Student AddStudent(Student student, Course course){
student.Courses.Add(course);_db.SaveChanges();
}
Didn’t work because course was in a different context
private Student AddStudent(Student student, Course course){
var newStudent = GetStudent(student.Id);var newCourse = GetCourse(course.Id);newStudent.Courses.Add(newCourse);_db.SaveChanges();
}
![Page 15: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/15.jpg)
Very similar to LINQ to SQL Major difference
LINQ to SQL - .SingleOrDefault() LINQ to Entities - .FirstOrDefault()
Selectingpublic Course GetCourse(int id){ var course = (from c in _db.Courses where c.Id.Equals(id) select c).FirstOrDefault(); return course;}
Entity FrameworkLINQ to Entities
![Page 16: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/16.jpg)
Deletingpublic void DeleteCourse(Course course){
_db.DeleteObject(course); _db.SaveChanges();}
Adding (Inserting)public void AddCourse(Course course){ _db.AddToCourses(course); //this will be a list of AddToX _db.SaveChanges();}
Entity FrameworkLINQ to Entities
![Page 17: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/17.jpg)
Editing (Updating)public void EditCourse(Course course){
_db.Courses.Attach(new Course { Id = course.Id }); _db.Courses.ApplyCurrentValues(course); _db.SaveChanges();}
“course” has been edited somewhere else – MVC Controller, so a “stand-in” is created
Entity FrameworkLINQ to Entities
![Page 18: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/18.jpg)
Questions?
![Page 19: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/19.jpg)
Tweet “@componentone <3’s Utah Code Camp” for a second chance to win
![Page 20: MVC and Entity Framework](https://reader033.fdocuments.us/reader033/viewer/2022042518/5491961eb4795972558b474f/html5/thumbnails/20.jpg)
James [email protected], @latringo
Inland Empire .NET User’s Groupwww.iedotnetug.org2nd Tuesday’s of each month in Riverside
Thank you