Resolve same namespace collision between two assembly

One of my friend worked in his freelancing project. In his project there were more than 4 class library projects used within his application. But by mistake he had created the same namespace with same class name within the two class library project. After adding the references of that class library in his main project, when he tried to create an instance of that class, he got the following error.


Error 1
The type ‘Lib1.PatientDataContext’ exists in both ‘d:\TFS\Lib1\bin\Debug\Lib1.dll’ and ‘d:\TFS\Lib2\bin\Debug\Lib2.dll’

It means the C# compiler gets confused that which assembly it has to use for creating an instance of the class. That’s why it throws an above error.

So he asked me to resolve his issue. well, i have never faced this type of error before. So i did some googling and came to the solution that by using extern alias provided in C#, above issue can be resolve.  How ? Let’s see this practically by creating that scenario.

In Visual studio, let’s create one console application and named it as – NameSpaceCollisionApp. Now within the same solution of the application add the two new class library project. In each class library assembly add one new class named – PatientDataContext.cs. But keep in mind that both of these class are within same namespace. The code of both the class looks like this –

ClassLibrary1 / PatientDataContext.cs

namespace DataServiceFactory.DataContext
{
    public class PatientDataContext
    {
        public PatientDataContext()
        {
        }

        public override string ToString()
        {
            return "From Class Library 1";
        }
    }
}

ClassLibrary2 / PatientDataContext.cs

namespace DataServiceFactory.DataContext
{
    public class PatientDataContext
    {
        public PatientDataContext()
        {
        }

        public override string ToString()
        {
            return "From Class Library 2";
        }
    }
}

When you build the application, you’ll get the error highlighted above. So let’s resolve it by following the below steps.

Step-1: In the solution explorer, expand the “References” folder and  select the required assembly name then right click on it and open its property window.

Service1Property

Step-2: In the property window, change the aliases name from “global” to “Service1 (Whatever you want)”.

ChangeAliase

Step-3: Same way follow the Step-2 for ClassLibrary2 reference also.

Step-4: Now to use it in your main application, you need to add – extern alias “alias name”; line of code to the top of your source file, something like this –

extern alias Service1;
extern alias Service2;

using System;

namespace NameSpaceCollision
{
    class Program
    {
        static void Main(string[] args)
        {
            var object1 = new Service1.DataServiceFactory.DataContext.PatientDataContext();

            var object2 = new Service2.DataServiceFactory.DataContext.PatientDataContext();

            Console.WriteLine(object1);
            Console.WriteLine(object2);
            Console.ReadLine();
        }
    }
}

And we are done. Hope you like this post and hope it may help to my future readers. You can find the demo application from here.

 

Tip: Finding a value in any of the columns in a SQL Server table

Few days ago one of my colleague working in another team in my office asked me one question that he have had one table named “PatientDetail” with 58 columns and he wanted to find value from all varchar columns. As a developer the first solution came in my mind was – check value in each column like this FirstName = ‘hello’ OR LastName = ‘hello’ OR…so on. One thing to note here is there are more than 50 varchar columns out of 58 columns. So your query looks something like this –

SELECT * FROM PatientDetail
    WHERE FirstName = 'hello'
	  OR LastName = 'hello'
	  OR Column3 = 'hello'
	  OR Column4 = 'hello'
	  Or Column5 = 'hello'
	  OR ... Column52 = 'hello'

Don’t you think that above query becomes too much lengthy if i write each column name here. So What is the preferred solution ?

Hmm Do you know that you can use column name in “IN Clause” in sql server!! Really ??

Yes, the above query you can rewrite like this –

SELECT * FROM PatientDetail
     WHERE 'hello' IN (FirstName, LastName, Column3, Column4, Column5,...)

Well, we just reverse the IN Clause! and this is perfectly valid statement for sql server. Try it yourself. Now the only problem here is we still have to write more than 50 columns in “IN” clause. So to overcome this problem, let’s convert the above query into dynamic sql.

-- *** Prepare comma seperated column list ***
DECLARE @ColumnNames VARCHAR(3000)

SELECT @ColumnNames = ISNULL(@ColumnNames + ', ' + QUOTENAME(COLUMN_NAME), QUOTENAME(COLUMN_NAME)) 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'PatientDetail'
	AND DATA_TYPE IN ('char', 'nvarchar', 'text', 'varchar')

--PRINT @ColumnNames

DECLARE @query VARCHAR(MAX)=
	'SELECT * FROM PatientDetail
	WHERE ''hello'' IN (' + @ColumnNames + ')'
	
EXEC SP_EXECUTESQL @query

That’s it. Hope you like this tip. If you know any other solution then post it in the comments. Thank you for reading this post.

Zen Coding in Visual Studio 2015 using Web Essesntial Extension

Few months ago, I have added web essentials 2015 extension to my visual studio and studied its features. This is truly for web developers which makes their life easier. In this post we’ll see one of its great productivity booster feature and that is Zen Coding.

Zen Coding is a plug-in to generate HTML or CSS code in much faster way using jquery/css like selectors. Before we go into the detail let’s first install it in our visual studio.

Follow the below steps.

  • Open the visual studio.
  • Go to the Tools menu and click on Extensions and Updates menu item which will open one window.
    Web_Essesntials_Install
  • In the Extensions and Updates window, click on “Online” in the left pane. Once you click on it you got the list of extensions on the right side.

Web_Essesntials_Extension

  • Select the “Web Essesntials 2015” and click on “Download” button.

 

After installing this plug-in, it’s time to play with it.

Let’s say I want to generate simple table tr HTML. So here is how you can generate it with Zen coding.

table>tr>td

Now hit “Tab” key and it generate the following html code.

<table>
    <tr>
        <td></td>
    </tr>
</table>

Now I want to generate table with 3 “tr” and 2 “td” with some content in each “td”.

table>tr*3>td{hello world}*2

Now hit “Tab” key and it generate the following html code.

<table>
    <tr>
        <td>hello world</td>
        <td>hello world</td>
    </tr>
    <tr>
        <td>hello world</td>
        <td>hello world</td>
    </tr>
    <tr>
        <td>hello world</td>
        <td>hello world</td>
    </tr>
</table>

As a web developer you know all the html tags but what is the use of “*”(star) and “{ }”(curly braces) in the above zen code ?

Well, there are some more characters available which will have its own usage in zen code. Let’s first understand each one with example.

1). “#” – Creates an Id attribute for specified tag.

Example

div#testId>span

Hit “Tab” key which will generate the following html code.

<div id="testId">
    <span></span>
</div>

 

2). “.” – Creates a class attribute for specified tag.

Example

div.inlineBlock>ul>li

Hit “Tab” key which will generate the following html code.

<div class="inlineBlock">
    <ul>
        <li></li>
    </ul>
</div>

 

3). “[]” – Creates a custom attribute for specified tag.

Example

div[title]>button[value=”Save”]

generates the following html.

<div title="">
    <button value="Save"></button>
</div>

 

4). “>” – Creates a child element after specified tag.

Example

div>p>img

generates the following html.

<div>
    <p>
        <img src="" alt="" />
    </p>
</div>

 

5). “+” – Creates a Sibling element.

Example

div>p>img+a

generates the following html.

<div>
    <p>
        <img src="" alt="" />
        <a href=""></a>
    </p>
</div>

 

6). “*n” – Creates the specified elements for n number of times.

Example

div>ul>li*2>a

generates the following html.

<div>
    <ul>
        <li><a href=""></a></li>
        <li><a href=""></a></li>
    </ul>
</div>

 

7). “$$” – Generates the incremental numbers.

Example

table>tr#id$$*3>td

generates the following html.

<table>
    <tr id="id01">
        <td></td>
    </tr>
    <tr id="id02">
        <td></td>
    </tr>
    <tr id="id03">
        <td></td>
    </tr>
</table>

 

8). “{}” – Add the text inside the elements.

Example

div>p*4>span{This is my own text $$}

generates the following html.

<div>
    <p>
        <span>This is my own text 01</span>
    </p>
    <p>
        <span>This is my own text 02</span>
    </p>
    <p>
        <span>This is my own text 03</span>
    </p>
    <p>
        <span>This is my own text 04</span>
    </p>
</div>

 

9). “()” – Grouping the expression and generate HTML accordingly.

Example

table>(tr>th{column $$}*3)+(tr>td*3)

generates the following html.

<table>
    <tr>
        <th>column 01</th>
        <th>column 02</th>
        <th>column 03</th>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>

 

Apart from all of the above, you can now also add Lorem Ipsum text in your HTML Code. Simply type lorem and hit TAB and a 30 words of Lorem Ipsum text is inserted. If you want to add only 4 words of Lorem Ipsum then just type below code and hit “Tab” key.

div>p*4>lorem4

<div>
    <p>Amet, consectetur adipiscing elit.</p>
    <p>Fusce vel sapien elit.</p>
    <p>In malesuada semper mi.</p>
    <p>Id sollicitudin urna fermentum.</p>
</div>

You can find many more other features from its official web site.

That’s it for now. Hope you enjoy this post.