Archive for the ‘.NET’ Category

Is POCO the right choice when working with entity framework?

The main benefit of a POCO is that you can pass it to a class, library or assembly that doesn’t need to know anything about Entity Framework to do its job.

Remember the Single Responsibility Principle – your DAL should know about EF, but your Domain should not, and neither should your presentation layer, because EF deals with data access and those layers do not. Passing EF generated class objects up to those layers generally means you need to make those layers aware of EF, breaking the SRP and making it harder to unit test those layers in isolation.

In more complex applications, you will want to split the logic up into separate concerns – data access, business logic, presentation (and perhaps a lot more).

As entity framework deals with data access, it resides in the data access layer – here, you will see little difference between POCOs and EF generated classes. This is fine, because this layer already knows about Entity Framework.

However, you may want to pass data up to the business logic layer – if you do this with EF generated classes, then your business logic layer must also know about Entity Framework because the EF classes rely on a lot of things EF provides specially. What this does is remove the isolation that your business logic layer should have – you need this isolation so you can unit test it correctly by injecting known data into the class from a fake data access layer, which is incredibly hard to do if you let the business logic layer know about EF.

With unit testing, you should be testing the layers functionality, not the functionality of third party libraries – but with EF you end up testing a lot of EF’s functionality, or your own functionality which relies very heavily on that of EF’s. This isn’t good, and it can mask errors or issues.

Removing the business logics dependency on EF generated classes also allows you to move the layer to as remote a location as you like from the data access layer – you can even stick it behind a web service and it would be completely happy. But you can only do this with POCOs, you cannot do this with EF generated classes.

POCO’s really come into their own in large, complex multi layered applications – if you aren’t layering your app, then you won’t see a whole load of benefits.

Print html page in .net windows application

You can use the WebBrowser control to do so. It will allow you to show HTML inside your WinForms.

The DocumentText proprety will allow you to set a String that represent the HTML you want to show.

For example:

webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";

Afterward if you want to print the page, you’ll have to wait until the Document is completed and call the Print method of the WebBrowser. MSDN shows an easy way to do it:

 private void PrintHelpPage()
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");

private void PrintDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
    // Print the document now that it is fully loaded.

    // Dispose the WebBrowser now that the task is complete. 

Method Override vs Method Overload

February 19, 2012 Leave a comment

Some people might get confused between method overriding and method overloading, here i will explain the differences between these two methods and how each one of them is used:

Method Overriding means that you have 2 methods in 2 different classes with the same name, same parameters (signatures) but with different implementations in each class, mostly this happen between a base (abstract) class and derived class. Here is an example for method overriding:

Base Class:

Class Animal

protected string Name;

public string WhatAmI()


return “I am an animal”;


Derived Class:

Class Dog: Animal

public string WhatAmI()


return “I am a dog”;


Method Overloading means that you have 2 methods in the same class with the same name but different parameters (signatures), example for method overloading:

class Calculations
public static int Add(int number1, int number2)
return number1 + number2 ;

public static int Add(int number1, int number2, int number3)
return number1 + number2 + number3;

SQL CTE (common table expression)

February 17, 2012 Leave a comment

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.

A CTE can be used to:

  • Create a recursive query. For more information, see Recursive Queries Using Common Table Expressions.
  • Substitute for a view when the general use of a view is not required; that is, you do not have to store the definition in metadata.
  • Enable grouping by a column that is derived from a scalar subselect, or a function that is either not deterministic or has external access.
  • Reference the resulting table multiple times in the same statement.

Using a CTE offers the advantages of improved readability and ease in maintenance of complex queries. The query can be divided into separate, simple, logical building blocks. These simple blocks can then be used to build more complex, interim CTEs until the final result set is generated.

CTEs can be defined in user-defined routines, such as functions, stored procedures, triggers, or views.

SQL CTE Example

WITH ProductAndCategoryNamesOverTenDollars (ProductName, CategoryName, UnitPrice) AS
FROM Products p
INNER JOIN Categories c ON
c.CategoryID = p.CategoryID
WHERE p.UnitPrice > 10.0

FROM ProductAndCategoryNamesOverTenDollars
ORDER BY CategoryName ASC, UnitPrice ASC, ProductName ASC

The CTE should be used directly by the query following it since it will be deleted after that, so you can’t define a cte and use it after 2 or 3 queries since an error will be generated telling you that the table ctetable1 (for example) doesn’t exist.

For Recursive Queries Using Common Table Expressions check this link:

The network BIOS command limit has been reached

October 21, 2011 Leave a comment

When you are building an deployment project, you may often recieve an ASPNETCOMPILER error “The network BIOS command limit has been reached”
. As describes by Microsoft, the error occurs because of the following reasons:

  • This issue may occur if the client computer submits simultaneous, long-term requests against a file server that uses the Server Message
    Block (SMB) protocol. An example of a long-term request is when a client computer uses the FindFirstChangeNotification function to
    monitor a server share for changes.
  • This issue may occur if the MaxCmds registry value setting on the client is less than 50, or the MaxMpxCt registry value
    setting on the server is less than 50.

To resolve this issue, verify that the MaxCmds and MaxMpxCt registry values are set to 50 or more. To do this, follow these steps:

  1. Click Start -> Run – > “regedit”
  2. Navigate to the following registry key:


  3. Open the MaxCmds entry in the right listview.
  4. In the Value data, enter a value of 50 or more.
  5. Navigate to the following registry key:


  6. Open the MaxMpxCt entry in the right listview.
  7. In the Value data, enter a value of 50 or more.
  8. Restart your computer for the changes to take effect.

Note The MaxCmds and MaxMpxCt registry entries are REG_DWORD decimal entries. If they do not exist on your computer, you can create them as new REG_DWORD values. The range of values for these registry entries is between 0 and 65535.

String Optimization in VB.NET

October 21, 2011 Leave a comment

This article describes a set of tips and tricks for using strings in that will boost the performance of your application especially
when the code contains lots of string related operations such as string concatenation.

String Declaration

The best way to declare a string variable is to set it to an empty string directly after declaration. It is a common mistake for most .net
developers to set the string to “” or Nothing. This is wrong because the “” is not really an empty string for the .net CLR and
the Nothing could throw a NullReferenceException if you reference the string later in the code. Below is the correct code for string declaration :

Dim str As String = “” ‘ wrong

Dim str2 As String = Nothing ‘ wrong

Dim str3 As String = vbNullString ‘ wrong

Dim str4 As String = String.Empty ‘ correct

String Concatenation

The usual way to concatenate strings is to use the + or the & operators. However the & is faster than the +
because it is specially designed for strings while the + also works for numeric addition.

Dim str As String = String.Empty

str = “Hello ” & ” world!” & vbCrLfFor i As Integer = 0 To 4

str &= str & vbCrLf


Optimizing String Concatenation

A similar class to String is called StringBuilder located in System.Text.StringBuilder. The StringBuilder is specially
designed for extreme boosting of string concatenation. It is handled in a special way by the .net CLR. It’s easy to use and manipulate just like
the string class. If you know how much the string length will approximately be at the end of concatenation, you can set this capacity in the constructor
of the StringBuilder which gives additional performance boost:

Dim str As String = “Hello World” & vbCrLf

Dim sb As New System.Text.StringBuilder(str.Length * 4)

For i As Integer = 0 To 4



Dim final As String = sb.ToString ‘get the result string

StringBuilder also supports formatting strings using the AppendFormat method which is also highly optimized and easy to use:

Dim names As New StringCollection




names.Add(“Krystelle”)Dim sb As New System.Text.StringBuilder

For Each name As String In names

sb.AppendFormat(“My name is {0}”, name)


Dim final As String = sb.ToString

There is no need to instantiate multiple StringBuilder within the same procedure. You can reuse the same
StringBuilder object by setting the capacity property to 0 which deletes the current string:

Dim sb As New System.Text.StringBuilder

‘use sb…

sb.Capacity = 0

Exclude folders while building in web deployment projects

October 14, 2011 Leave a comment

Usually when you are building an application in visual studio using WDP you need it quickly to be done.
In this article i will show you how you can perform this in a very simple list of steps:

1 – Add your WDP (web deployment project).

2 – Right click on it and click on Open Project File.

3 – in the wdproj file you will see a tag called “<ItemGroup>”.

4 – Inside this tag add the following line:
<ExcludeFromBuild Include=”$(SourceWebPhysicalPath)\FolderName\**\*.*” />.

5 – Save and close your wdproj file and that’s it.

Note: you can add as many folders to exclude as you want.