Thoughts on ASP .NET MVC Framework + Entity Framework

This weekend I decided to spend some time digging into the MVC framework.  Up to this point the majority of what I knew about it was based off my preexisting knowledge of MVC from Rails as well as what I heard; but I wanted to get a good look for myself.  In addition, I wanted explore methods of supporting the non-automated lazy loading in the Entity Framework. As a test bed for this I decided to start creating a simple data entry interface for my Anime Manager application that I previously wrote using Rails.

So let me give a brief overview of I organized things to permit the lazy loading from the Entity Framework to be fairly simple and contained to the controller.  In my previous attempts I would ended up having to persist the reference to the Context to the View which violates separation of concerns.  To prevent this I structured each Action as such:

   1:  public ActionResult EditSeries(int id)
   2:  {
   3:       using (AnimeManagerEntities Context = new AnimeManagerEntities())
   4:       {
   5:            Series series = Series.GetSeries(Context, s => s.SeriesId == id);
   6:            series.StudioReference.Load();
   7:            series.SeriesGenres.Load();
   8:   

9: ViewData["Studios"] = new SelectList(Studios.GetAllStudios(Context), "StudioId", "Name",

  10:                                                                          series.StudioId);
  11:            ViewData["Genres"] = Genres.GetAllGenres(Context).Select(g => g as IListing).ToList();
  12:   
  13:            return View("Series/Edit", series);
  14:       }
  15:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Notice that we are creating the context, passing it to our data methods, and the disposing of it via the end of the using construct.  The downside of this method is that its not really lazy loading since it is required that we know what will be used in the View and manually load the reference via the Load commands on Lines 6 & 7.  The problem with baking the lazy load into the entities themselves is A) the only good way is to modify the designer file which is overwritten each time we update our schema model. B) Because the using properly disposes of the Context our lazy loading is not able to attach to the original context, which appears to be a prerequisite for the Entity Framework.

I am still looking into this as I find this approach decent, but not what I am looking for. Thankfully, I was introduced to a gentlemen on the SQL team at Microsoft and so I have asked him what Microsoft recommends.

Regarding the MVC Framework, I decided to use Preview 5, even though our standard at RCM is Preview 4.  The reason for this was due to my requirements I needed to submit arrays of control values and Preview 4 did not support this, where Preview 5 did. The following is an example of capturing an array of control values:

   1:  public ActionResult UpdateSeries(int seriesId, 
   2:       string seriesName, int selStudio,
   3:       string[] genres, int[] weights)
   4:  {
   5:       // create the array of GenreWeight objects
   6:       var query = genres.Select((string g, int index) => new GenreWeight()
   7:       {
   8:            GenreId = int.Parse(g),
   9:            Weight = weights[index]
  10:       }).ToArray();
  11:   
  12:       if (Series.Save(seriesId, seriesName, selStudio, query))
  13:            return Index();
  14:       else
  15:            return EditSeries(seriesId);
  16:  }

This is accomplished my assigning the same name to multiple controls in a form (name=”genres” and name=”weights”). In this case I allow the user to add some template HTML via JavaScript which allows me to easily support the many to many relationship between the entities. I will have a post later on utilizing this sort of strategy for gathering user input.

The basic premise of the MVC framework is that you define your routes to get your users to an area of your site and then you create parameter names for the actions that match your elements name attribute value.  Overall, I have found MVC to be quite nice, though regrettably, given the scope of what I set out to do I have not yet leveraged it fully, although I can see quite a bit of potential for when I start to really construct the various screens for the Anime Manager.  More posts are definitely forthcoming.


 
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

3 thoughts on “Thoughts on ASP .NET MVC Framework + Entity Framework

Leave a comment