DCSIMG
November 2009 - Posts - Ido Flatow's Blog Veni Vidi Scripsi

Ido Flatow's Blog

Veni Vidi Scripsi

News

Have you heard me speak?
Powered
<style type='text/css' media='screen' id='sm_css'> #smix {overflow: visible;height: auto;border-radius: 10px;max-width: 250px;background-color: #323232;text-align: left;font-size: 12px;line-height: 16px;font-family:'Lucida Sans Unicode','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;} #smix a {color: #0056CC;text-decoration: none;} #smix .sm_head {color: #fff; line-height: 1em;font-size: 1.4em;padding: 10px;color: #fff;} #smix .sm_lanyard_wrapper {background-color: #fff;;clear: both;width: 97%;margin: 0 auto;margin-bottom: 0px;} #smix .sm_lanyard_content {padding: 7px;}#smix button.sm_rec, #smix a.sm_rec, #smix input[type=submit].sm_rec { padding: 6px 10px; -webkit-border-radius: 2px 2px;-moz-border-radius: 2px; border-radius: 2px; border: solid 1px rgb(153, 153, 153); background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(255, 255, 255)), to(rgb(221, 221, 221))); color: #333; text-decoration: none; cursor: pointer; display: inline-block; text-align: center; text-shadow: 0px 1px 1px rgba(255,255,255,1); line-height: 1; }#smix .sm_rec:hover { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(248, 248, 248)), to(rgb(221, 221, 221))); }#smix .sm_rec:active { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(204, 204, 204)), to(rgb(221, 221, 221))); }#smix .sm_rec.medium { padding: 3px 7px; font-size: 13px; }#smix .sm_rec span.icon.thumbs_up {background-position: 0px 36px;vertical-align: text-top;display: inline-block;margin-right: 4px;height: 18px;width: 16px;background-image: url(http://speakermix.com/images/new/thumbsold.png);}#smix .sm_rec:hover span.icon.thumbs_up {background-position: 0px 18px;} #smix .sm_events {padding:2px 0px 4px 0px;} #smix .sm_section {font-size: 10px; border-bottom: 1px solid silver; margin-bottom: 6px;} #smix .sm_subline {font-size:120%;margin-top:4px;font-weight:bold} #smix .powered {text-align: right} #smix .powered img {margin: 7px} </style>
Sela Technology Center

Advertisement

November 2009 - Posts

PDC 09 – Entity Framework 4

First time that I can say – there are MANY people that use EF4. How do I know? because the session had the room filled with people, with people waiting outside !

EF 4 is great. It solved many of the problems of EF3.5 and made the technology more mature and useful.

What annoyed me in the lecture is that it showed all the features of EF4 that people already know of (well, I know of). The title was misleading because I thought they will talk about new features that don't exist currently.

Here is the list of features, and links to blog posts regarding them:

One new think that was introduced was the “Quadrant” project – cool graphical tool that allows editing SQL Server data.

I’m not saying the session wasn’t good, for people that are unfamiliar with EF4, the session was great and it covered a lot of new features. For someone like me that is familiar with EF4 since the first beta, the session didn’t add much.

Don’t forget the Entity Framework session and tutorial on the SDP – we’ll have a session about Entity Framework 4 (for those who are unfamiliar) and will have a whole day of EF & ORM tutorials with the best EF guys in Sela (Erez Harari, Gil Fink, Bernie Almosni and myself).

PDC 09 updated – ASP.NET 4 Futures

I must admit, I had not used ASP.NET for a long time (lately I’m a Silverlight fan), so most of the things were new to me. What I liked about this session is that it was indeed about “Futures” because some of the things shown were still in development.

The topics discussed:

  1. Helpers
    1. RAD development using ActiveRecord over Entity Framework for Asp.Net MVC
    2. Introducing TaskScheduler to perform background tasks (no need to code with System.Threading no more)
    3. Sending Emails with verification
    4. New control for file upload with progress bar
    5. Simple mechanism for using routing with web forms (aspx).
      Instead of using “\App\Welcome.aspx” use “App\Welcome”.
      This is being done with the new SmartyRoute class (I have a feeling that this name will change when the RTM is released).
  2. HTML5
    1. The menu control is now implemented under HTML5 as a <menu> element.
    2. The team is checking how to incorporate new HTML5 standards such as <video> and <audio> elements
    3. Web Storage can be accessed with AJAX using the IntermediateDataContext (Again, I have a feeling that the name will change) which works together with the existing AdoNetDataContext class
  3. Caching
    1. Velocity implementation for Session state provider  - but that we already know (if not, take a look at a lecture I did).
    2. Velocity implementation for OutputCache. For this the OutputCache was changed to support provider mechanism, so you can actually write your own provider for the output cache.
  4. CSS sprites: CSS sprites are used to optimize download of images, instead of making the browser run multiple requests to get numerous images, the browser sends one request to get a large image, constructed of all the images and CSS styles are used to specify which section of the large image should be displayed in each element.
    1. CSS sprite support is added to ASP.NET (just change the “Images” folder to “Sprites”) – You can control whether you want sprites or not using the SpriteGroups class.
    2. You can use a sprite image class to specify when you want to get an image as a sprite:
      <%= Html.SpriteImage(elementName, altText)%>
    3. The generated code is a DIV element that uses the background-position CSS attribute
  5. ActiveRecord
    1. Used to wrap Entity Framework and to provide easier usage of the technology
    2. Implemented to be used in Asp.Net MVC
    3. Allows automatic creation of SQL Compact edition databases (MDF file)
    4. Supports automatic DB table changing when the entity class is changing (for example – adding a property to an entity results in a new column in the table).
      Nice feature – not sure how it will work in production environments

To conclude, I enjoyed this lecture, looking forward to the release of Asp.Net 4 with VS 2010.

As for HTML5 – funny that all the demos were in FireFox and not IE8.X. When will IE support HTML5? If the Asp.Net team accepts that HTML5 is coming, shouldn’t the IE team also start working on the matter?

We will discuss Asp.Net 4 on the SDP which will be on December, so don’t forget to register.

PDC blogging – Data Programming and Modeling

As I've stated before, I attended the data programming and modeling session - And I enjoyed every minute. Chris Anderson and Don Box rock !  

They talked about Entity Framework 4 – The automatic lazy load (no more need to call the damn Load() method), different approached of model first, POCO and code only (using the fluent API) etc.

There was a talk about code first having a configuration class instead of writing all the fluent API by hand, and without having to add attributes to your POCO classes - that is something worth checking out.

They also showed the new Transact Sql editor that was shipped with VS2010, Which allows running TSQL queries and also has intellisense:

image

image

After talking about EF we got to the real deal - ODATA (Open Data Protocol) that allows producing and consuming data through various applications, for example export Sharepoint data and consume with Excel,

If you want to expose your data using the OData protocol you can easily build a service using the ado.net data services. To describe the data returned from these services you don't use XSD schemas anymore but rather... the EDM (Entity Data Model) ! A new use for the well known EF's EDM.

Last, but not least, they showed bits from project “M” - you can take an EDMX file, rename its extension to “.M” in VS and it will automatically change to a “M” model and vice versa – I tried it in VS 2010 beta 2 but it didn’t work, maybe it part of the next version ?. This project is quite new and I didn’t had a chance to look at it yet, but there are some lectures on the project in the following days which I’ll try to attend and post about later on.

PDC 2009 First Impression

Wow ! I’m not familiar with events of this size. For me, Israeli Tech-Ed was the biggest event I attended.

Well, there’s something strange about flying 20 hours to the far end of the world, going to a city with million of people of different types, and entering a conference center will thousands of geeks moving around.

First Keynote was interesting, a lot of talking about “3 screens and a cloud”, cloud in general, Azure in specific. Many new features in VS2010, IIS, and Windows to support all the features of Azure and such.

I’m looking forward to the different session. My interest is in the technologies I use in my day to day work – Silverlight, Entity Framework, WCF, Asp.Net & VS2010 & TFS.

Currently I’m sitting in the Data Programming and Modeling for the Microsoft .NET Developer session, talking about EF more.

Don’t forget, in the coming SDP we’ll talk about EF 4 and we’ll have a full day of ORM & EF tutorials

Implementing Composite design pattern in Entity Framework

Last week, a colleague of mine asked me to create a sample composite design pattern model in Entity Framework. I thought to myself, no problem, created the table that contains the Component, Leaf & Composite entities (according to TPH – Table Per Hierarchy) and went to build the EDM based on the table and there I started to have problems which needed manual fixing. Since I’m working with EF 3.5, I’ve built the DB prior to the model – In EF 4.0 you can do model-first and build your db by script.

After reverse engineering the db table, I got one entity named “CompositePattern”. From there I had to do some things in the designer and some things manually:

  1. In the designer, change the name of “CompositePattern” to “Component”.
  2. Mark the entity abstract.
  3. Create a new entity, called “Leaf” and base it on the “Component” entity.
  4. Move the required properties from the “Component” type to the “Leaf” type by cutting & pasting the scalar properties (You may need to set the mappings for the properties you’ve moved).
  5. Create a new entity, called “Composite” and base it on the “Component” type.
  6. Now comes the tricky part – moving the children relation from the “Component” type to the “Composite” type. This cannot be done in the designer, only straight in the XML itself.
  7. Before changing the navigational property, note that there are two navigational properties for the composite->component relation - one on each side: a navigational property “Children” from “Composite” to “Component”, and another navigational property “Parent” from “Component” to “Composite” – This is what we’ll need to change in the EDMX file.
  8. Open the EDMX file with a xml editor (right-click the .EDMX file and choose “Open with…” and select “XML Editor”.
  9. Since EF’s wizard didn’t name the navigational properties correctly, we’ll start by finding and replacing them. You can identify which navigational property is the parent and which is the children according to the multiplicity – the “Parent” should be marked as “0..1” and the “Children” should be marked as "*” (in my case it was “CompositePattern” and “CompositePattern1” accordingly) – Locate all the occurrences of these names and replace them with “Parent” and “Children” accordingly.
  10. Now that we named the navigational properties and associations, we ca start changing things. The changes are only in the CSDL part:
    1. Locate the association element for the composite->component relation (If you’re working in a new model, you should have only one association element) - Change the type of “Parent” to “Composite” (because parents are always of type composite)
    2. Locate the “Children” navigation property in the “Component” type and move it to the “Composite” type.
  11. Close the file. You can try to open it again in the designer, for me it did not work with EF 3.5 and VS 2008 SP1, but did work with EF 3.5 and VS 2010 Beta 2.

Write some code to insert composites and leafs and see how it works.

If you got mixed up and didn’t manage to create the composite model, no fear – the sample model can be downloaded from here.

The sql for building the db table (In SqlServer) is:

CREATE TABLE [dbo].[CompositePattern](
[ID] [uniqueidentifier] NOT NULL,
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[ParentID] [uniqueidentifier] NULL,
[Type] [int] NOT NULL,
CONSTRAINT [PK_CompositePattern] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF
, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

 
I will continue to post some more Entity Framework tips & tricks this week while I'm attending the PDC, and of course post about the new stuff discussed in the conference.

Just to remind you all, in December we’re having the SDP where we will talk a lot about Entity Framework and ORM in general, don’t forget to register.

Entity Framework View Generation Fixup

A few day ago, a colleague of mine, Shlomo Goldberg, raised a question about Entity Framework and large models. Another colleague of mine, Gil Fink posted some links to helpful tips for using large models, and I want to talk about one of these tips – using the View Generation option.

If you’ve worked with large models in Entity Framework 3.5, you probably noticed that as the model grows, it takes more time to initialize the ObjectContext for the first time. A solution to this is to use view generation, using the EdmGen tool, which can reduce up to 30% of the time it takes to initialize the context for the first time.

If you’ve tried to use the view generation for very large models (80+ entities), you probably noticed that the generation it self takes a lot of time to complete, the output code file is very large (we’ve reached up to 70Mb) and if the model is very large and complex (large inheritance trees are a big contributor), the generated file can sometimes makes Visual Studio crash on opening and compiling.

If you look at the generated file content, you’ll see that it includes many if/else/if statements, that looks something like this:

if (index == 0)
{
}
else
{
if (index == 1)
{
}
else
{

}
}

This structure of code is problematic for the following reasons:

  1. It creates a very large if/else/if tree, that at some point cannot be translated by the compiler
  2. The canonical if/else/if will sometimes cause Visual Studio to crash (I assume whoever created VS didn’t think someone will use that many if/else/if statements)
  3. The whitespaces used to create this form causes the size of the file to be extremely large

So what can you do?

You can move to Entity Framework 4.0 (currently in beta) where the view generation was fixed. In EF4 the generated code is of the form:

if (index == 0)
{
}
if (index == 1)
{
}
 
If you can’t or don’t want to move to EF4, you can edit the generated file and change the structure of the code to make it similar to the one generated by EF4 (remove brackets and whitespaces). Since this file can be auto generated and it’s a waste of time doing find & replace, I’ve attached a Powershell script that performs the changes. Using this script can reduce the generated file size a lot (sometimes up to 90% reduction of file size).

If you’re interested in learning more about Entity Framework or about ORM in general, we’ll be discussing these topics and more in the SDP on 27-30 of December.

This week I’ll continue to write more posts about Entity Framework Tips & Tricks, directly from the PDC (Los-Angeles), and hopefully some of these posts will contain new insight to Entity Framework 4.0 and future roadmap