DCSIMG
March 2011 - Posts - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

March 2011 - Posts

IE9 and HTML5 Session Slide Deck and Demos

IE9 and HTML5 Session Slide Deck and Demos

Today I had the pleasure to deliver a MSDN session about IE9 and HTML5 at Microsoft Ra’anana. IE Logo
First of all I want to thank all the attendees who came to hear the session.
In the session I talked about IE9, Feature Detection and Modernizr, CSS3, HTML5 and more. The session was recorded and will be available soon on the internet (in Hebrew). You can download the session’s slide deck and demos from here.

Enjoy!

Posted: Mar 30 2011, 02:31 PM by Gil Fink | with no comments |
תגים:, , , ,

Sela SDP Conference Sessions Slide Deck and Demos

Sela SDP Conference Sessions Slide Deck and Demos

SDPLogo

Yesterday Sela SDP Conference ended after 4 intensive days.
In the conference I had two breakout sessions about OData and about Code First in Entity Framework.
First of all I want to thank all the attendees in those sessions. I hope that you had good time and that you enjoyed the sessions and of course the whole conference.
As always I published the sessions slide decks and demos for you to download.
The recording (in Hebrew) of all the conference sessions will be published soon in the conference site so stay tuned.

What to Look in an ORM Solution?

What to Look in an ORM Solution?

One question that I sometimes being asked is what to look for in an ORM solution or more properly which ORM ORM_thumb[1]is preferable and why. The decision which ORM to choose is very crucial to the project development since after you start developing it will be hard to go back and use another ORM solution. Since there are many ORMs out there, here is a checklist that will help you to evaluate ORM solutions and to pick the one that fits your needs:

  • Basic Features
    • Can handle inheritance & polymorphism
    • Can handle different types of relations (1-1, 1-N, N-M)
    • Supports transactions
    • Supports different SQL operations (aggregation, grouping, ordering)
  • Extended Features
    • Multiple databases support
    • Allows execution of dynamic queries using a query language
    • Data Binding support
    • Stored Procedure and Function support (only if needed)
    • Can handle very big models and databases
  • Flexibility
    • Allows customization of SQL queries
    • Supports Joins and Sub-Queries
    • Supports concurrency
    • Can handle specific Database types (identity, GUIDs, XML…)
  • Ease of use
    • Graphical designer for mappings
    • Auto generation of model classes
    • Generate Database schema (Model First approach)
    • Maintainable (when Database schema changes)
  • Optimizations & Performances
    • Allows Lazy loading
    • Allows Eager loading
    • First level cache
    • Second level cache
    • Optimizes queries
    • Supports Bulk updates & deletions
  • SOA
    • Supports model serialization
    • Allows loading of disconnected objects
    • Supports disconnected state tracking
    • Model objects are readable in other platforms
  • Other things to consider
    • Price
    • Performance
    • Support for multiple platforms
    • Source code provided
    • Enables unit testing
    • Documentation & Support
    • Maturity
    • Vendor stability

This is a suggested list and there are more things to evaluate other then that by it can help you to make the relevant decision.

Entity Framework in Depth at Sela SDP

Entity Framework in Depth at Sela SDP

SDPLogo

Yesterday Ido Flatow, Erez Harari and I delivered a tutorial day about Entity Framework as part of Sela SDP conference. The day included the following topics:

  • Introduction to ORMs and to Entity Framework (delivered by Ido)
  • Querying and Manipulating Data using Entity Framework (delivered by Erez)
  • Entity Framework Internals (delivered by myself)
  • Entity Framework in N-Tier Applications (delivered by myself)
  • Experts Panel (delivered by all of us)

I want to thank all the attendees who came to hear us. 
We had a lot of fun and I hope that you had fun too. 
Since the materials of the day were based on Sela’s Entity Framework 4 official course, we won’t be able to publish them in our Blogs.

Using Code First Model Configurations Classes

Using Code First Model Configurations Classes

In the past I explained how to use the Code First Fluent API in order to configure and shapeUsing Code First Model Configurations Classes your EDM during runtime. One of the problems that might raise when you use the Fluent API as I showed in the previous post is that the OnModelCreating method might become bloated and hard to read. This is the time to get familiar with another model configuration option which is built inside Code First.

Model Configurations Classes

When you use Code First you will probably configure the creation of the model in some way. You can use the Code First Fluent API in order to do that. When you use the Fluent API the place that you will use it is the OnModelCreating method in the DbContext class. In very big models that might be a problem. Quickly you will find yourself having a very big and bloated method which holds all the configurations. This is the time for refactoring your code to use model configurations. There are two major classes that you will use: the generic EntityTypeConfiguration and ComplexTypeConfiguration. Both of the classes lives in the System.Data.Entity.ModelConfiguration assembly.

Usage Example

Lets revisit the DbContext from the previous post:

public class SchoolEntities : DbContext
{
  #region Ctor 
 
  public SchoolEntities() :
    base("MySchool")
  {
  }
 
  #endregion
 
  #region Properties
 
  public DbSet<Course> Courses { get; set; }
  public DbSet<Department> Departments { get; set; }
 
  #endregion
 
  #region Methods
 
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    
    base.OnModelCreating(modelBuilder);
 
    modelBuilder.Entity<Department>().
      Property(d => d.Name).
      IsRequired().
      HasMaxLength(50);
 
    modelBuilder.Entity<Department>().
      Property(d => d.DepartmentID).
      HasDatabaseGenerationOption(DatabaseGenerationOption.None);
 
    modelBuilder.Entity<Department>().
      HasMany(d => d.Courses).
      WithRequired(c => c.Department).
      HasForeignKey(c => c.DepartmentID).
      WillCascadeOnDelete();
 
    modelBuilder.Entity<Department>().
      Ignore(d => d.Administrator);
 
    modelBuilder.Entity<Course>().
      Property(c => c.Title).
      IsRequired().
      HasColumnName("Name");      
  }    
 
  #endregion
}

If I want to use the EntityTypeConfiguration I’ll create a new class for Department configuration. The class will inherit from EntityTypeConfiguration<Department> and in its constructor I’ll use the Fluent API for configurations. The following code sample shows the DepartmentTypeConfiguration class:

public class DepartmentTypeConfiguration : EntityTypeConfiguration<Department>
{
  #region Ctor
 
  public DepartmentTypeConfiguration()
  {
    Property(d => d.Name).
      IsRequired().
      HasMaxLength(50);
 
    Property(d => d.DepartmentID).
      HasDatabaseGenerationOption(DatabaseGenerationOption.None);
 
    HasMany(d => d.Courses).
      WithRequired(c => c.Department).
      HasForeignKey(c => c.DepartmentID).
      WillCascadeOnDelete();
 
    Ignore(d => d.Administrator);
  }
 
  #endregion
}

Now that we have the class we will wire it into the ModelBuilder by using the Add method of its Configuration collection. The following code sample show you how to do that:

public class SchoolEntities : DbContext
{
  #region Ctor
 
  public SchoolEntities() :
    base("MySchool")
  {
  }
 
  #endregion
 
  #region Properties
 
  public DbSet<Course> Courses { get; set; }
  public DbSet<Department> Departments { get; set; }
 
  #endregion
 
  #region Methods
 
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);
 
    modelBuilder.Configurations.Add(new DepartmentTypeConfiguration());
 
    modelBuilder.Entity<Course>().
      Property(c => c.Title).
      IsRequired().
      HasColumnName("Name");
  }
 
  #endregion
}

During runtime we will get the same EDM whether we use the first method with the configurations inside the OnModelCreating method or the second method of EntityTypeConfiguration.

Summary

When you use Code First you get a lot of configuration options. If your model is small you can create all the configurations inside the OnModelCreating method. When the model starts to grow you can use the ModelConfiguration classes in order to divide your implementation to small objects with configuration responsibility.

Back to Basics – Reading a File into Memory Stream

Back to Basics – Reading a File into Memory Stream

Today I was asked to help a developer with a simple task she had. That task included reading an image file into a Back to Basics – Reading a File into Memory Streammemory stream in order to send the image through an e-mail attachment. This post will show you how to do exactly that.

Reading a File into Memory Stream

Here is the code for reading the file into a memory stream:

using (FileStream fileStream = File.OpenRead(filePath))
{
    MemoryStream memStream = new MemoryStream();
    memStream.SetLength(fileStream.Length);
    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}

That’s it for the reading part. Pay attention to use the using statement in order to dispose the FileStream after you use it.

Adding a MemoryStream as Attachment to a MailMessage

In order to use a stream as an attachment for an e-mail message object all you have to do is to write the following code:

msg.Attachments.Add(new Attachment(memStream, filename, MediaTypeNames.Image.Jpeg)); 

In the code sample msg is an instance of a MailMessage class, memStream is the MemoryStream (such as the memory stream from the previous code sample) and filename is the name of the file in the attachment. Since I know that the image is jpeg then I use the MediaTypeNames.Image.Jpeg.

Summary

Reading a file into a stream is a very basic thing to know. In the post I showed how to read an image into a memory stream and also how to attach it into a mail message object.

Posted: Mar 03 2011, 11:47 AM by Gil Fink | with 5 comment(s) |
תגים:,

Installing a Production Server for ASP.NET MVC 2 – Part 2

Installing a Production Server for ASP.NET MVC 2 – Part 2

A few days ago I helped a client to solve a problem they had when they deployed an ASP.NET MVC 2 application. .Net Logo with ASP.NETIn a previous post I wrote about how you can install a production server in order to run ASP.NET MVC 2 application. The client team have installed the server according to the AspNetMVC2 MSI installer section (in my post) and then they used Phil Haack’s IIS6 Extension-less URLs solution to enable the routing engine. Life was beautiful and they were on the right track.

But…
running the application produced the “page cannot be found” page:

Page not found

This made them and me to scratch our heads. So the first thing to do in such cases is to check the IIS log file which can be very helpful by holding the error code which was 404.2. While Googling the net we found the Getting an ASP.NET 4 application to work on IIS6 post by Johan Driessen which holds the solution. Apparently when you install .Net 4 on Windows Server 2003 with IIS6 the .NET 4 ASP.NET ISAPI extension is installed disabled…
By using the following command through the command line you can check whether the ISAPI is enabled:

cscript iisext.vbs /ListFile
If you find out that the status for the .NET 4 ASP.NET ISAPI extension is 0 then in order to enable it you will need to run the following command in the command line:
cscript iisext.vbs /EnFile C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll

You need to run both of the previous commands in <WINDOWS DIR>/System32.
That solved the client’s problems and the application is running on the production server now.
Thanks Johan!

Update:
Another option to do the same thing is through the Web Service Extensions in the IIS Manager. All you have to do is to mark ASP.NET 4 in the extension list and press the Allow button. The following figure shows how to do that:
Web Service Extensions

Thanks to Ran Wahle for this tip!