Category Archives: Tip & Trick

Separate Binary Data Out Of An Entity For Effective Loading

I received an email this morning asking about loading binary data related to an entity, here is the excerpt:

Hello, 

I was reading the following post on your blog and checking the code in Google Code. Entity Framework 4 POCO, Repository and Specification Pattern [Upgraded to EF 4.1]

I found it really interesting and realized that the performance issues where solved.

But how do you handle binary data?

Consider you have an entity with 2 Byte[] fields (varbinary or filestream in the database).

public class Product
{
    public Int32 Id { get; set; }

    public Boolean Available { get; set; }

    public String Name { get; set; }

    public Byte[] Image { get; set; }

    public Byte[] Brochure { get; set; }
}

When N records are loaded you will get N * 2 files being loaded even if you want to display only the names.

One solution to this problem is to apply a projection. For example:

context.Products.Where(x => x.Available).Select(x => new ProductView { Available = x.Available, Name = x.Name, Image = x.Image });

In this case I am leaving out the brochure but including the image … How do you address this with your code?”

—————————–

Here is my answer:

In my experience regarding the binary data, I would create another entity, says, BinaryData which is somewhat like below (of course there will be a corresponding BinaryData table in the database):
public class BinaryData
{
    public Int32 Id { get; set; }
    public Byte[] Data { get; set; }
}
Then within an entity that has binary data, like your Product with Image or Brochure properties, I just add dependencies (or relationship) to the BinaryData entity:
public class Product
{
    // ... other properties
    public BinaryData Image { get; set; }
    public BinaryData Brochure { get; set; }
}

With lazy-loading enabled, you won’t worry about loading binary data (Image, Brochure) when loading Product, but it will be loaded when you access to it like: product.Image”

I myself think it is a good design practice when working with ORM framework, like Entity Framework or NHibernate.

What do you think? If there is a better solution for this, please drop a comment.

Macro To Collapse Expanded Projects In Solution Explorer

If you’ve ever worked on a solution which has too many projects, you would find it took a lot of time to ‘manually’ collapse expanded projects once opening the solution on Visual Studio’s Solution Explorer.

Here is a macro which does that for you. Just open VS, create a VS Macro Project by going to Tools > Macros > New Macro Project. VS will create a new macro project named MacroProject1 and show that project in Macro Explorer. Under MacroProject1, there is a module named Module1. Right click on Module1 and select Edit, VS then brings out Module Editor. Copy and paste this piece of code for the module:

Imports System

Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module CollapseSelectedNode

Sub CollapseSelected()
‘ Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

‘ Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count > 0) Then

‘ Get the selected node
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.SelectedItems(0)

UIHSolutionRootNode.DTE.SuppressUI = True

‘ Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
If UIHItem.UIHierarchyItems.Count > 0 And UIHItem.UIHierarchyItems.Expanded Then
‘ As of a Bug in VS 2005 you have to use the following lines
‘ instead of using UIHItem.UIHierarchyItems.Expended = False
UIHItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
UIHSolutionExplorer.DoDefaultAction()
End If
Next

UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
UIHSolutionRootNode.DTE.SuppressUI = False
End If
End Sub
End
Module

Your module editor might look like below:

Macro Editor

Press Ctrl + S to save the module. Now it’s time to bind shortcut keys to execute the module once pressed. Go to Tools > Options, expand ‘Environment’ node then select ‘Keyboard’ subnode. Enter the fully qualified name of the macro you just created in ‘Show command containing’ text box, i.e. Macros.MacroProject1.CollapseSelectedNode.CollapseSelected. Place mouse cursor in ‘Press shortcut key’ text box then press Ctrl + Alt + / (you can choose other combination of keys if you want to).  Your screen might look like below:


Shortcut keyboard binding to macro

Click OK to bind the shortcut keys to the macro. Now, it’s time to test the macro by opening any .NET solution, expand some projects under that solution then press ‘Ctrl + Alt + /’ you will see all the expanded projects are collapsed.

Enjoy!

Reference: http://weblogs.asp.net/israelio/archive/2007/09/06/visual-studio-collapse-selected-node.aspx