Monthly Archives: December 2009

Vermont IT Jobs: ASP.NET Developer Position in Burlington

Vermont Oxford Network is looking for web developer.

ASP.NET Developer
We are looking for a motivated ASP.NET (VB) developer to join us in building testable business object-based web applications. We build and maintain a range of web, windows forms and Microsoft Access applications for our network of hospitals using Microsoft Technologies. More and more of our emphasis is on web applications and we will be starting to migrate some of our internal applications to web applications.
Our development team believes in using continuous integration and iterative testing. Unit testing and automated test suites are becoming core strengths of the team.

Applicants should be experienced with CSS and TSQL. Experience with Microsoft Access and unit testing a plus. We offer a good working environment, the opportunity to work with a professional IT team and competitive salary and benefits.

Please mail or email your resume and cover letter by January 15, 2010.

Vermont Oxford Network
33 Kilburn St
Burlington, VT 05401
mail@vtoxford.org

A message from your sponsor: Single and SingleOrDefault are supported in EF4

Just a friendly reminder that I’m passing on from one of the tech reviewers of my book who (thankfully) noticed that I had left a note in there about Single & SingleOrDefault not being supported.

They were not in .NET 3.5 SP1 Entity Framework, but they are now supported in EF4.

Single is the way to go when you only expect one item to be in your result set.

As noted by many, the benefit of Single is highlighted in testing. Single expects only one item to be in the result set. If there are more, then an exception is thrown and this can help you discover logic errors in your application.

For a list of supported & unsupported LINQ methods used (or not used) in LINQ to ENtities, see the MSDN Topic: Supported and Unsupported LINQ Methods (LINQ to Entities)

Sampson the Draft Dog

Sampson has been happily wearing a back pack for months now when we go on hikes.

fall hike 023

It’s a Ruffwear Palisades pack. We love this pack. The packs are removable and what’s left is a decent, comfy, well-fitting harness.

W’e’ve known for a while that Sampson is a good candidate for doing draft work. We don’t have the proper harness yet, but for his first pulling, the back pack harness was sufficient.

We just had a wicked storm and the wind was blowing very hard for over 24 hours. There were a lot of trees down along the trail in our woods. THat won’t do since soon we’ll be skiing back there and until then this is where we walk the dog.

Here’s one spot where there were over 6 trees down. Many of them came up by their roots. That’s what you’re seeing with the arrows on the right – root beds.The ones on the left are across the trail. As usual, it was a friggin poplar that came down but took a nice spruce and another tree along with it.

binghamton and trail work at home 008 (Small)

About 20 feet up the trail were two more big trees across the trail.

Sampson and down trees

Rich needed to bring his chainsaw and other tools back there. Finally he has a little assistance!

first time puling

Sampson didn’t mind it a bit. He barely seemed to notice. He’s a natural!

samspon ready to go

On the way back, I was the brake-man, using an extra bungee cord to be sure the sled didn’t slide right into our baby’s behind.

Just a caveat about the sled, rope, harness setup. This is far from optimal. The rope was rubbing against his sides a bit but it was a short walk in the woods today.

The proper drafting harness protects the dog much more and the weight attaches behind him. Here’s an example: http://alpineoutfitters.net/Secure/Scripts/prodView.asp?idproduct=18. We actually have a harness that says it’s X-L and it is much too small so we have to get another.

Agile Entity Framework 4 Repository Part 5: IObjectSet and Include

  1. Agile Entity Framework 4 Repository: Part 1- Model and POCO Classes
  2. Agile Entity Framework 4 Repository: Part 2- The Repository
  3. Agile EF4 Repository: Part 3 -Fine Tuning the Repository
  4. Agile EF4 Repository: Part 4: Compiled LINQ Queries
  5. Agile Entity Framework 4 Repository Part 5: IObjectSet and Include
  6. Agile Entity Framework 4 Repository: Part 6: Mocks & Unit Tests

In Part 2 of this blog series, I created a context interface, IBAGAContext, that returns IObjectSet types rather than ObjectSet types from the EntitySets.

Example:

     IObjectSet<Contact> Contacts{get;}
     IObjectSet<Customer> Customers {get;}
    

IObjectSet is the new EF4 interface that is used to create the ObjectSets returned by the default context.

For a point of comparison, here’s a method from a default code generated context:

 public ObjectSet<Activity> Activities
        {
            get
            {
                if ((_Activities == null))
                {
                    _Activities = base.CreateObjectSet<Activity>("Activities");
                }
                return _Activities;
            }
        }
        private ObjectSet<Activity> _Activities;

ObjectSet implements IObjectSet which provides it with set operations. It also inherits from QueryObject which gives it lots of EF capabilities. One of the methods that come with QueryObject is Include which provides eager loading.

context.Customers.Include(“Orders”) blah blah

(A few people, including Matthieu Mezil, have created overloads for Include that take lambdas instead of magic strings. Here’s Matthieu’s second of two posts which contains a link to the first: Entity Framework Include with Function Next. Additionally, Jamie Philips had a lambda function for EF1 that allowed further filtering. Last time we talked, he reported that it wasn’t working with EF4 POCOs.)

But Include is not available with IObjectSet.

In a recent comment, Nick asked how I was using Include on an IObjectSet.I realize I neglected to add that into the relevant post.

With nods to Jamie Phillips again, Include came from his POCO sample when he and I co-presented back-to-back EF4 POCO sessions at the New England Code Camp in October.

Jamie created an Include extension method to replace the Include supplied by the ObjectQuery. I placed the method in a separate class file in the same project that contains the model and the BAGAContext file because it simply calls the real ObjectQuery.Include.

namespace ExtensionMethods
{
  public static class MyExtensions
  {
    public static IQueryable<TSource> Include<TSource>
      (this IQueryable<TSource> source, string path)
    {
      var objectQuery = source as ObjectQuery<TSource>;
      if (objectQuery != null)
      {
        return objectQuery.Include(path);
      }
      return source;
    }
  }
}

In the repositories in Part 3 of this series, this is how I was able to use the Include method.

Jamie did write a follow-up post about the problem with the lambdas and POCO classes: Include() Extension Method and POCO

N-Tier Methods in Entity Framework 4 – Use with Care

One of the things I struggled with in n-Tier apps in EFv1 was the options for reconstructing entity state on the server side in order to call SaveChanges when modified data was passed in from the client.

The options were to hit the database to get a set of current server values and then apply updates to those or to just attach the incoming data and iterate through each property marking it as modified so that SaveChanges would be able to construct an Update.

The problem with the former was the extra hit to the database. That made me crazy.

The problem with the latter is that I was marking every property as modified even when they weren’t and that made me crazier. Admittedly not really worse than the first.

If you’ve seen the WCF chapter in my book, you’ll know I opted for the extra database hit.

In EF4, there are new state methods. In addition to ApplyPropertyChanges (from v1) being renamed to ApplyCurrentValues and acquiring a sibling, ApplyOriginalValues, there is the ChangeState method. I explored the impact of these methods in my Sept/Oct 2009 CoDe Magazine article, What’s New in Entity Framework 4? Part 1: API Changes .

I want to focus now on using ChangeState to set an entity to Modified.

This is similar to walking through each property and marking it modified. The concept is that you are saying “this entity is modified” without having to specify exactly what was modified. Every property is marked as modified and every property will be included in an update statement.

But what if every property was not supplied?

What if you have a client app that allows users to edit a few fields of a contact record. And…the client app is a website which means that when the user hits a Save button and the page posts back, there’s no original contact object. So in order to save, in one layer or other, some code constructs a contact to return to the server/repository. Here’s the code:

Contact c = new Contact {ContactID= Convert.ToInt32(IDLabel.Text),
        FirstName = TextBox1.Text, LastName = TextBox2.Text,
        ModifiedDate = DateTime.Now, State = State.Modified };

I have a state property in there because this is a POCO object and I can put whatever I want in it. πŸ™‚ Plus I use the state property to help me out when I get to the server side.

Looks okay, but what if I told you there is also a Title property in this class?

Because the UI developer didn’t populate that value, on the server, the assumption will be that Title is null. When you call ChangeState(EntityStateModified), that null will be seen as the value to be used for the update. Take a look at part of the update method sent to the database. It’s setting Title=null.

exec sp_executesql 
N'update [dbo].[Contact] set [FirstName] = @0, [LastName] = @1, [Title] = null, [ModifiedDate] = @2 where...

This is not to say that there’s anything wrong with the ChangeState method (or ApplyCurrentValues/ApplyOriginalValues, which will do the same thing). THe point (as always) is that you really need to be aware of what’s going on.

My worry is that I can’t really control what’s happening on the consumer side. It’s such a critical rule. I don’t want to “solve” it by making all of my properties non-nullable. (Can you imagine? πŸ™‚ )

So it means that I have to think carefully about how and where I use these methods because I may not always have control of the state of what’s being passed in.

I was getting accustomed to the idea of just marking all of the properties modified and living with the inefficient update. One inefficient trip to the database is more cost effective than two trips to the database.

There’s always the CACHING option. But that gets really sticky with multiple users and graphs and lazy loading etc. I know I can cache my POCOs, even my graphs, or just values if I want… easily enough. I was just hoping to avoid that extra layer for now.

So I either have to make the consumer cross-their-heart-and-hope-to-die-promise to return the entire object back to me with the correct values or I have to go back to using some of the options from EFv1 – hit the database, manually (and selectively) mark each properites as modified or pass original values around.

Some of my Recent Entity Framework 4 content

Not sure if I’ve blogged all of these (so twitter-addicted these days) so here’s a list of recent articles & webcasts I’ve done on EF4.

CoDe Magazine

DevConnectionsPro Magazine (was aspNETPro Magazine)

(Note that the images for the article can be seen via hyperlinks. Wherever it says “as shown in Figure X”, there’s a hyperlink on Figure X and you can the see the image. Penton is working on the website, so hopefully this will be more obvious in the future.)

  • Entity Framework 4, Beta 2 is Here (Jan 2010) Not online yet

Visual Studio Magazine

Webcasts

Screencasts

 

 

 

 

Interviews

Blog

Lots of blog posts on EF4 under the EF4 tag: thedatafarm.com/blog/tags/ef4

The Book
I’m hard at work on the 2nd Edition of my book, Programming Entity Framework which will be based on EF4. Look for the Rough Cuts version to appear soon on OReilly.com.