Entity Framework and Creating a Reusable Abstraction#

I am currently working on an open source project called MyCodeCamp with Sean Chambers, Fabio Honigmann, and Esteban Garcia.  We decided to use ASP.NET MVC and Entity Framework (EF).  Although I use SubSonic 2.2 and 3 beta at work, I thought this might be a good time to learn other ORM type frameworks for comparison. I have used LINQ To SQL and NHibernate a little also, but am not an expert by any means.  And since we want this project to be accessible to as many as possible, we thought Entity Framework would be a good choice seeing as Linq2Sql is getting folded into Entity Framework anyway.

So, I don’t really know how most people use EF, but coming from working on SubSonic 3, I have some habits that I would like to keep, particularly, using a Repository<T> like pattern.  After looking at all the code and classes I would have to write if I did the traditional, IClassRepository and ClassRepository, I decided to make Repository<T> work kind of like it does in SubSonic 3 with SubSonicRepository<T>.  This was not as easy as I thought.

First problem is when you want to update the model, you need to open up the .edmx file in the designer which sometimes does not open and you have to reboot Visual Studio.  And if you make major changes to your data model, you are better off deleting the file and recreating it.  Now I don’t think that would go over to well if you actually used it to do your mappings of data to entity models, but I am not using Entity Framework that way.  I think it was their intention to use it more of as a mapper, but I think I have found a much easier and scalable way of mapping data to domain objects by using AutoMapper.  We use AutoMapper at work with SubSonic 3 and we just love it.  I won’t go into everything it does, but it is by far the best project out there for mapping objects to other objects.

So here is an example of how I am getting an entity object, then mapping it to a domain object.

public class Repository<T> : IRepository<T>
    {
        private MyCodeCamp.Data.MyCodeCampEntities context;
        public Repository()
        {
            context = new MyCodeCamp.Data.MyCodeCampEntities();

        }

        public T GetById(Int32 id)
        {
            var tableName = typeof(T).Name;
            var key = new EntityKey("MyCodeCampEntities." + tableName, "Id", id);
            var r = (T)context.GetObjectByKey(key);

            return r;
        }
    }

Notice, since I am using T, I need to use reflection to get the Entity Name, or table name.  Then this data returns to a BaseService<TModel, T>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using MyCodeCamp.Data;

namespace MyCodeCamp.Domain.Services
{
     public abstract class BaseService<TModel, T>
        where TModel : new()
        where T : new()
    {
         public Repository<T> _repo;

         protected BaseService()
        {
            _repo = new Repository<T>();
            // Cache maps
            var tm = Mapper.FindTypeMapFor<T, TModel>();
            if (tm == null)
            {
                Mapper.CreateMap<T, TModel>();
            }

            tm = Mapper.FindTypeMapFor<TModel, T>();
            if (tm == null)
            {
                Mapper.CreateMap<TModel, T>();
            }
        }

         public virtual TModel GetById(Int32 id)
         {
             var item = _repo.GetById(id);
             var model = Map(item);
             return model;
         }

        public virtual TModel Map(T d)
        {
            return Mapper.Map<T, TModel>(d);
        }


    
    }
}

Now for the service that derives form the BaseService:

public class VenueService : BaseService<Model.Venue, Data.Venue>
    {
        public VenueService()
        {
           
        }
        
        public List<Model.Venue> GetByCity(string city)
        {
            var result = _repo.Find(v => v.City == city);
            var venues = result.ToList();
            var venueModels = Map(venues);
            return venueModels;
        }

        
    }

As you can see the Code is very clean and simple to read.  What is nice about going down this road with the AutoMapper is that mapping is completely automated and I don’t have to remap anything if I change properties or add them to the data model.  All I need to do is make sure the Property is on the domain model, then it will get mapped.  Also it will map by convention also, so if you have a Complex type as a property, like, Venue.Room.Name it would automatically map to a flattened dto like Venue.RoomName.  There are a ton of other conventions also, but you should go download AutoMapper and look at the tests to see all of the possibilities.

The next big issue I ran into was that the data was not getting updated when I would pass a detached object to the EF context.  Since I was creating the object that needed to get passed in outside of the ObjectContext scope, I needed to get the EF ObjectStateManager to recognize that the Entity Object had changed and to persist it.  I again had to use a little bit of reflection, but after I understood how EF handled things, it made more sense.  It was quite frustrating for a few hours trying to figure out why EF was not persisting updates and only inserts.  As you can see with the code below I needed to get the value of the Entity Key and the hydrate the entity object so that the ObjectStateManager knew that the data had been changed.

public int Update(T o)
       {
           var tableName = typeof(T).Name;

           // Need to get object out of ObjectStateManager, then set it so the dbcontext knows to persist changes
           var Id = GetPrimaryKeyValue(o);
           T currentObject = GetById(Id);
           currentObject = o;

           context.ApplyPropertyChanges(tableName, currentObject);

           return context.SaveChanges();
       }

       private int GetPrimaryKeyValue(T o)
       {
           int Id = 0;
           var properties = typeof(T).GetProperties();
           foreach (PropertyInfo p in properties)
           {
               if (string.Compare(p.Name, "Id", true) == 0)
               {
                   var r = p.GetValue(o, null).ToString();
                   Id = int.Parse(r);
                   break;
               }
           }
           return Id;
       }
All in all, I don’t mind EF for the Data Access Mapping, and the way it is currently abstracted in our project, it could easily be switched out for Linq To Sql, Subsonic, or any other ORM that uses LINQ to access its data.  I think the best way to use Data Mapping technologies in the future is control your own mapping by using tools such as AutoMapper so that you are not locked in to one way of getting your data out of your database. 
Monday, May 18, 2009 4:39:27 AM UTC #    Comments [6]  |  Trackback

 

Modifying a compile asp.net assembly when no longer have source#

So while I was at the MVP Summit, an old client got a hold of me and said there was a weird error on the site when a user was trying to post something.  He sent me a screenshot and it said could not login as sa to sql server.  I remoted into the server where they had moved the application and looked in the web.config file and did not see any connection string that had sa as the user name.  So I was stumped.  I began to look at the stack trace and realized which assembly this was coming from.  This happened to be the only assembly which did not have the source code on the server.  I wrote the app over 6 years ago and the laptop the source was on had recently had a failed hard drive.

Anyway, it was a vb.net 1.1 app from visual studio 2003.  Yes I used to do a little VB. :)  So I realized that there must have been a connection string hard coded in the assembly where the stack trace was coming from.  Oh no!  How was i going to fix this without changing the sa password on their new host which was not really an option? 

First thing I did was download reflector here. I opened the assembly and did see the hard code connection string.  Man what an idiot for doing that.  Don’t know why I did that at the time, but not a very smart move. So I was stuck.  How do I edit an assembly without having to export all the code from reflector, find visual studio 2003 and run it in a virtual, download the rest of the app and pray that I could get it to work.  Well i searched for modifying a .net assembly and found this: Reflexil - http://sebastien.lebreton.free.fr/reflexil/ . It is a plug-in that works inside of reflector that allows the modification of the code in the assembly.  So i downloaded it opened the assembly, changed the connection string code and then saved it.  The .dll ended up being a third the size, but still worked after deploying it to the server.  I was shocked.  I just had edited a vb.net 1.1 assembly on a windows 7 laptop using reflector while in a session during the MVP summit.  Pretty cool I think.  Saved me a whole day of figuring out how to run visual studio 2003 and get that code compiled again.

Wednesday, March 25, 2009 7:42:51 AM UTC #    Comments [3]  |  Trackback

 

Windows 7 and My 2 year old Laptop#

I have owned a Dell Inspiron 9400 for almost 2 years with Vista Ultimate and have enjoyed it for the most part.  I had some issues with wireless and getting the sluggish feeling after I had visual studio, 20 browser tabs open, iis 7, sql server, etc. all open at the same time.  Of course I thought, well i have a lot of stuff running at same time so of course it will be slower.  Well now with Windows 7 I have not one wireless issue, speed is crazy faster and I  am still only using 1 gb of ram of 2 gb available. 

I am absolutely shocked a how much better I like my laptop.  I have a 8 Gb Vista 64 desktop at home, so my laptop is always going to seem sluggish, but now I really like using my laptop.  I am not looking forward to having to wipe it again when the beta expires, but is well worth the joy i now get from my laptop. 

Windows7DesktopSmall

I love the new task bar and my favorites always being down there.  I can’t remember if I have even done a full reboot in a month.  Oh if you are going to try this at home, make sure to do a clean install. 

Tuesday, February 24, 2009 9:00:35 AM UTC #    Comments [2]  |  Trackback

 

Enabling 301 redirects on IIS 7 on Windows server 2008#

I was stumped the other day as to why i could not get a 301 redirect in from site.com to www.site.com on IIS 7.  After much looking around I found out that you need to enable this feature in the server manager. 

image

Then you need to go back to the IIS node that you want to enable 301 redirect for:

 

image

And then set the values for your redirect:

image

If you notice what is also cool is that it will redirect addresses relative to the domain and not just to the root (i.e site.com/oldfile.htm will redirect to www.site.com/oldfile.htm

Hope this helps someone, cause it was not very intuitive for me to find out how to do it since the feature was not installed by default.

Thursday, February 05, 2009 10:37:27 AM UTC #    Comments [2]  |  Trackback

 

ASP.NET MVC 1.0 Release Candidate Is Released#

Time to dig into the T4 template system (learn here) in Visual Studio since they added a cool feature for generating scaffold controllers and views. Check out ScottGu's Article, of course. I am thinking that this was influenced by the SubSonic MVC Scaffold Addin from Rob Conery a little bit ago. I am so happy that we are going to have our own scaffold playground with built in tools. It is getting so much closer to how I really want to develop web apps. Woohoo!

I know it has been a long time since i blogged, since before the holidays. Four kids is a little bit busier than I imagined, but all worth it. I am still trying to stay up late to learn more new things coming out in the web world, as I love the information overload. Much more to come soon I am sure!

Just found this link. You will need this to check out the MVC futures stuff that has Html.TextBoxFor(p => p.PropteryName).

Wednesday, January 28, 2009 8:31:47 AM UTC #    Comments [1]  |  Trackback

 

How to speed up your ASP.NET Website#

Recently we noticed that our site was a little sluggish.  Now of course when we started to look at why it was slow sometimes and not others, we opened up a huge can of worms.  Was it our code?  Was it our server?  Was it our proxy server?  Was it our database?  Was it our graphics?  Was it the JavaScript?  Or was it just that the end user had a slow connection?  Well it was all of them. 

Sometimes we forget that not everyone has FIOS.  We are very lucky to have a 20 Mbps / 20 Mbps connection at work.  So first thing we did was to look at the size of our pages when downloading the html, CSS, images and JavaScript.  We were shocked at the size of the files.  The first eye opening fact was that our home page was 1.5 MB.  Whoa.  The best tool that we found to figure the details out was YSlow.  Here is a snippet from their site about what the tool does:

YSlow analyzes web pages and tells you why they're slow based on the rules for high performance web sites. YSlow is a Firefox add-on integrated with the popular Firebug web development tool

This tool saved us so much time figuring out what our issues were.  I highly suggest reading the rules.  Also you can listen to a recent podcast from DotNetRocks: YSlow! Steve Souders finds Website Bottlenecks

So we began the process of optimizing the client.  The first thing you can do is to compress the JavaScript, CSS and html files on the server to reduce the file download size up to 75% on the client.  The way to do this in IIS6 is to enable GZip compression.  This is turned off by default which I think should actually be turned on by default.  Here is some info on how to enable it for specific file types.  Don't forget the .axd files if you use and Ajax in webforms.

Using HTTP Compression for Faster Downloads (IIS 6.0)

So next on our list was minimizing the size of the images that we were using as backgrounds in CSS.  They are the first calls from the browser when loaded from CSS.  Also moving JavaScript references to the bottom of the page helps.  The browser using a blocking request when loading JavaScript files, so if you have them at the top of the page, you will get a perceived load issue since the browser waits for that first.  The best thing to get out of optimizing for the client is the perceived load to the user which really makes the most difference.

So after removing the client side issues we realized we had a database load issue on certain pages with many records.  Well we found out we ran into the famous ORM gotcha of way too many queries when using strictly the data objects with related foreign key objects.  For instance, by relating a member record to the aspnet user table and doing a foreach on the member and then checking to see their username on the aspnet user table, it will cause another db query to get that info for each user.  Sometimes when using the orm objects that are generated instead of writing a custom query to populate your own domain object, you can get into a lot of trouble.

Our website has many applications to it, some mvc and some using webforms.  We have noticed a significant speed increase on the mvc sites.  There is less code to render and there is no Viewstate so right away there is some performance increases. 

I hope that this helps some other people figure out why their site is slow for some people some of the time.

Wednesday, December 10, 2008 9:52:17 AM UTC #    Comments [5]  |  Trackback

 

Missed PDC 2008? Go to MSDN Developer Conference#

If you missed PDC 2008 in L.A this year, you can catch some sessions at the MSDN Developer Conference in Orlando on December 11, 2008. It's only $99, so I am sure the registrations will fill up fast.  Here is a blurb from their site about what it will cover:

We’re bringing the PDC to you! For just $99 you’ll get the best of the PDC in your own backyard and hear all of the exciting announcements around the Azure Services Platform and Windows 7. Other sessions include the latest developments in .NET, Silverlight, Surface, Parallel Programming, Live Mesh, and more.

Register today to stay current in an ever-competitive job market, enhance your skills and gain access to the next generation of tools and technology.

  1. Experience the Azure Services Platform
    Create applications that seamlessly bridge PC, Web, and phone
  2. Be among the first to see Windows 7
    See the latest advances in Multi-Touch Application Development
  3. Take your .NET skills to the next level
    See sessions on WPF 4.0, Silverlight 2, ASP.NET 4.0, Parallel Programming, Live Mesh and more

The Cost? Just $99.
And, did we mention that attendees will get some cool giveaways?

 

For more information, here is a link to the site and registration page

http://www.msdndevcon.com/Pages/start.aspx

See ya in Orlando!

Monday, November 10, 2008 7:45:16 AM UTC #    Comments [5]  |  Trackback

 

Scaffolding in an ASP.NET MVC App#

So at work I had to create a simple admin for a few new features in our app and decided I would try out a few new technologies while I was at it to not only save some time, but to see what is out there now.  My heart was set on this being a simple MVC web app, but really wanted to try ASP.NET Dynamic Data also.  Well good news is that you can use Dynamic Data in MVC fairly cleanly and easily.  By adding a simple route to the global.asax.cs file you can easily have Dynamic Data working although it is web forms based:

 MetaModel model = new MetaModel();
            model.RegisterContext(typeof(MvcApplication1.Models.NorthwindDataContext), 
                new ContextConfiguration() { ScaffoldAllTables = true });
            routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx") {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = model
            });

Now this opened my eyes a little that you can easily create a hybrid web app of MVC and Web Forms.  Web Forms does have its advantages sometimes, especially when you start looking at existing tools and controls that you just need to work quickly and don't feel like reinventing the wheel.

You can download a sample project of this working in an mvc app over at codeplex. http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14473

That same night I was playing with Dynamic Data, Rob Conery had just posted his prototype of a Subsonic MVC Add-in for Visual Studio.  I was pretty impressed with the code and views that it generated, and I think is a great start to seeing what is possible.  Both samples took less that an hour to download and implement.  I ended up spending more time integrated security across the multiple web apps on different sub-domains than getting the scaffolds to work.

Wednesday, October 29, 2008 8:10:43 AM UTC #    Comments [3]  |  Trackback

 

 

All content © 2010, Jim Zimmerman
Book
New Book
Links to me
On this page
Sponsors
Calendar
<May 2009>
SunMonTueWedThuFriSat
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456
Archives
Tags
Blogroll OPML
Technorati
Favorite Links
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail