<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Gaute's .net weblog</title>
    <link>http://gaute.amende.no/</link>
    <description>thoughts on programming and computer related stuff</description>
    <language>en-us</language>
    <copyright>gaute</copyright>
    <lastBuildDate>Fri, 12 Jun 2009 11:18:17 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>gaute@amende.no</managingEditor>
    <webMaster>gaute@amende.no</webMaster>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=2a040d40-6e4d-4411-b3c4-c57abfaec18a</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=2a040d40-6e4d-4411-b3c4-c57abfaec18a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">This blog post is about changing the DefaultControllerFactory
to a custom one to allow constructor dependency injection. In this post I use <a href="http://structuremap.sourceforge.net/Default.htm">Structuremap</a>, <a href="http://commonservicelocator.codeplex.com/">CommonServiceLocator</a> and
I use a slightly changed version of the <a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=StructureMap%20Adapter">CommonServiceLocator.StructureMapAdatpter</a>. 
<br /><br />
The DefaultControllerFactory needs a constructor without any input parameters. I want
to get rid of dependencies in my code, so I put all my interface dependencies in the
constructor and want to use structuremap to handle this for me. I have used this in
a semi-large project, but in this blog post I will use an example with only one dependency
and only one page.<br /><br />
Lets get started:<br /><br />
I start by creating a new ASP.NET MVC project. I then delete all controllers and views
except for my home controller and my index view.<br /><br />
Then I add a Tools folder and a PersonRepository class in my new folder. This only
has a default constructor and a get method that returns a Person, a simple object
with only a name Property. I also extract an interface from PersonRepository called
IPersonRepository.<br /><br />
In my homecontroller I create a constructor that takes a Ipersonrepository as a parameter.
This is my HomeController now:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Web.Mvc; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> DIControllerFactoryExample.Tools; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Controllers
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> HomeController
: Controller { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">readonly</span> IPersonRepository
personRepository; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> HomeController(IPersonRepository
personRepository) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.personRepository <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> personRepository;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> ActionResult
Index() { var personid <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> 1;
ViewData.Model <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> personRepository.Get(personid); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> View();
} } }</span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /></span></pre>I compile, run and get a: "No parameterless constructor defined for this
object." exception. Just as planned.<br />
Now, the fun begins. Getting this to work.<br /><br />
I start by adding a solution folder called Lib, and adding StructureMap.dll and Microsoft.Practices.ServiceLocation.dll
and reference them in the solution. These can be found at the locations in the top
of this blog post.<br /><br />
Then I add 3 new files to my Tools folder.<br />
StructureMapServiceLocator.cs, DefaultStructureMapRegistry.cs, CommonServiceLocatorControllerFactory.cs<br /><br />
These files look like this:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> StructureMap.Configuration.DSL; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> DefaultStructureMapRegistry
: Registry { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> DefaultStructureMapRegistry()
{ ForRequestedType&lt;IPersonRepository&gt;().TheDefaultIsConcreteType &lt;PersonRepository&gt;();
} } }<pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Collections.Generic; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.Practices.ServiceLocation; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> StructureMap; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> StructureMapServiceLocator
: ServiceLocatorImplBase { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> IContainer
container; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> StructureMapServiceLocator(IContainer
container) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.container <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> container;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> DoGetInstance(Type
serviceType, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> key)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>.IsNullOrEmpty(key)
? container.GetInstance(serviceType) : container.GetInstance(serviceType, key); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span> IEnumerable&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span>&gt;
DoGetAllInstances(Type serviceType) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> obj <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> container.GetAllInstances(serviceType))
{ yield <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> obj;
} } } } </span></pre></span></pre><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Web.Mvc; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.Practices.ServiceLocation; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> StructureMapServiceLocatorControllerFactory
: DefaultControllerFactory { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span> IController
GetControllerInstance(Type controllerType) { var controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> ServiceLocator.Current.GetInstance(controllerType) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> Controller; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> controller;
} } } </span></pre><br />
I also add the following to the Application_Start method in my Global.asax.cs:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">var
registry <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> DefaultStructureMapRegistry();
var container <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Container(registry);
ServiceLocator.SetLocatorProvider(() =&gt; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StructureMapServiceLocator(container));
var locator <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StructureMapServiceLocatorControllerFactory();
ControllerBuilder.Current.SetControllerFactory(locator);</span></pre><br />
I compile, reload and everything works. That's is. Good luck;) 
<br /><br />
EDIT: Did a small fix in the code i global.asax, check out this <a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx">post</a><br /><br />
The full Visual Studio solution can be downloaded here:<br /><p></p><a href="http://gaute.amende.no/content/binary/DIControllerFactoryExample.zip">DIControllerFactoryExample.zip
(463.5 KB)</a><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2a040d40-6e4d-4411-b3c4-c57abfaec18a" /></body>
      <title>Changing the ASP.NET MVC DefaultControllerFactory with a custom using StructureMap and Common Service Locator.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</guid>
      <link>http://gaute.amende.no/ChangingTheASPNETMVCDefaultControllerFactoryWithACustomUsingStructureMapAndCommonServiceLocator.aspx</link>
      <pubDate>Fri, 12 Jun 2009 11:18:17 GMT</pubDate>
      <description>This blog post is about changing the DefaultControllerFactory to a custom one to allow constructor dependency injection. In this post I use &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;Structuremap&lt;/a&gt;, &lt;a href="http://commonservicelocator.codeplex.com/"&gt;CommonServiceLocator&lt;/a&gt; and
I use a slightly changed version of the &lt;a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=StructureMap%20Adapter"&gt;CommonServiceLocator.StructureMapAdatpter&lt;/a&gt;. 
&lt;br&gt;
&lt;br&gt;
The DefaultControllerFactory needs a constructor without any input parameters. I want
to get rid of dependencies in my code, so I put all my interface dependencies in the
constructor and want to use structuremap to handle this for me. I have used this in
a semi-large project, but in this blog post I will use an example with only one dependency
and only one page.&lt;br&gt;
&lt;br&gt;
Lets get started:&lt;br&gt;
&lt;br&gt;
I start by creating a new ASP.NET MVC project. I then delete all controllers and views
except for my home controller and my index view.&lt;br&gt;
&lt;br&gt;
Then I add a Tools folder and a PersonRepository class in my new folder. This only
has a default constructor and a get method that returns a Person, a simple object
with only a name Property. I also extract an interface from PersonRepository called
IPersonRepository.&lt;br&gt;
&lt;br&gt;
In my homecontroller I create a constructor that takes a Ipersonrepository as a parameter.
This is my HomeController now:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Web.Mvc; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; DIControllerFactoryExample.Tools; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Controllers
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; HomeController
: Controller { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;readonly&lt;/span&gt; IPersonRepository
personRepository; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; HomeController(IPersonRepository
personRepository) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.personRepository &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; personRepository;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; ActionResult
Index() { var personid &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; 1;
ViewData.Model &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; personRepository.Get(personid); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; View();
} } }&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;I compile, run and get a: "No parameterless constructor defined for this
object." exception. Just as planned.&lt;br&gt;
Now, the fun begins. Getting this to work.&lt;br&gt;
&lt;br&gt;
I start by adding a solution folder called Lib, and adding StructureMap.dll and Microsoft.Practices.ServiceLocation.dll
and reference them in the solution. These can be found at the locations in the top
of this blog post.&lt;br&gt;
&lt;br&gt;
Then I add 3 new files to my Tools folder.&lt;br&gt;
StructureMapServiceLocator.cs, DefaultStructureMapRegistry.cs, CommonServiceLocatorControllerFactory.cs&lt;br&gt;
&lt;br&gt;
These files look like this:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; StructureMap.Configuration.DSL; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; DefaultStructureMapRegistry
: Registry { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; DefaultStructureMapRegistry()
{ ForRequestedType&amp;lt;IPersonRepository&amp;gt;().TheDefaultIsConcreteType &amp;lt;PersonRepository&amp;gt;();
} } }&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Collections.Generic; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.Practices.ServiceLocation; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; StructureMap; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; StructureMapServiceLocator
: ServiceLocatorImplBase { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; IContainer
container; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; StructureMapServiceLocator(IContainer
container) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.container &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; container;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; DoGetInstance(Type
serviceType, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; key)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;.IsNullOrEmpty(key)
? container.GetInstance(serviceType) : container.GetInstance(serviceType, key); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt;&amp;gt;
DoGetAllInstances(Type serviceType) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; obj &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; container.GetAllInstances(serviceType))
{ yield &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; obj;
} } } } &lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Web.Mvc; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.Practices.ServiceLocation; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; StructureMapServiceLocatorControllerFactory
: DefaultControllerFactory { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; IController
GetControllerInstance(Type controllerType) { var controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; ServiceLocator.Current.GetInstance(controllerType) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; Controller; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; controller;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
I also add the following to the Application_Start method in my Global.asax.cs:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var
registry &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; DefaultStructureMapRegistry();
var container &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Container(registry);
ServiceLocator.SetLocatorProvider(() =&amp;gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; StructureMapServiceLocator(container));
var locator &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; StructureMapServiceLocatorControllerFactory();
ControllerBuilder.Current.SetControllerFactory(locator);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
I compile, reload and everything works. That's is. Good luck;) 
&lt;br&gt;
&lt;br&gt;
EDIT: Did a small fix in the code i global.asax, check out this &lt;a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx"&gt;post&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
The full Visual Studio solution can be downloaded here:&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;a href="http://gaute.amende.no/content/binary/DIControllerFactoryExample.zip"&gt;DIControllerFactoryExample.zip
(463.5 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2a040d40-6e4d-4411-b3c4-c57abfaec18a" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>ASP.NET MVC</category>
      <category>Patterns</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=f77906f7-da3e-4a40-9742-572aa24db274</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=f77906f7-da3e-4a40-9742-572aa24db274</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I have recently been working on a project
where we reimplement an old web application with a new one. Since there has been an
page on the same address before, there are also some old applications in other places
linking to pages on the old site. For instance, the login.aspx file is linked to in
a lot of places. 
<br /><br />
I will in my example use a link to the page login.aspx in my solution.<br /><br />
To get these links to work, I had (as far as I'm aware of) two options. To make a
login.aspx and redirect in this file and add login.aspx to global.asax IgnoreRoute,
or to map a new route in global.asax and redirect to my new login. The latter is the
solution i went for.<br /><br />
To accomplish this all I had to do was to add the following to RegisterRoutes method
in global asax:<br /><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br />
routes.MapRoute(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Handle
login"</span>, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"login.aspx"</span>, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> {
controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Login"</span>,
action <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Index"</span>,
id <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">""</span> }); </span><br /><br />
Very easy, and now, every request for http://myapp/login.aspx is handled by http://myapp/login/index/<br /><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=f77906f7-da3e-4a40-9742-572aa24db274" /></body>
      <title>Adding a custom route to ASP.NET MVC to allow old .aspx urls to work.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</guid>
      <link>http://gaute.amende.no/AddingACustomRouteToASPNETMVCToAllowOldAspxUrlsToWork.aspx</link>
      <pubDate>Fri, 05 Jun 2009 16:43:20 GMT</pubDate>
      <description>I have recently been working on a project where we reimplement an old web application with a new one. Since there has been an page on the same address before, there are also some old applications in other places linking to pages on the old site. For instance, the login.aspx file is linked to in a lot of places. &lt;br&gt;
&lt;br&gt;
I will in my example use a link to the page login.aspx in my solution.&lt;br&gt;
&lt;br&gt;
To get these links to work, I had (as far as I'm aware of) two options. To make a
login.aspx and redirect in this file and add login.aspx to global.asax IgnoreRoute,
or to map a new route in global.asax and redirect to my new login. The latter is the
solution i went for.&lt;br&gt;
&lt;br&gt;
To accomplish this all I had to do was to add the following to RegisterRoutes method
in global asax:&lt;br&gt;
&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
routes.MapRoute(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Handle
login"&lt;/span&gt;, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"login.aspx"&lt;/span&gt;, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; {
controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Login"&lt;/span&gt;,
action &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Index"&lt;/span&gt;,
id &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;""&lt;/span&gt; }); &lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
Very easy, and now, every request for http://myapp/login.aspx is handled by http://myapp/login/index/&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=f77906f7-da3e-4a40-9742-572aa24db274" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>ASP.NET MVC</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=bc11a90e-99fb-48fc-bf57-128ff1864ad4</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,bc11a90e-99fb-48fc-bf57-128ff1864ad4.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,bc11a90e-99fb-48fc-bf57-128ff1864ad4.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=bc11a90e-99fb-48fc-bf57-128ff1864ad4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">A while back I decided I wanted to try
Windows 7 Beta, but since I had Windows Server 2008 installed already, I decided to
try Windows Server 2008 R2 instead and just upgrade my existing installation.<br /><br />
So I did an upgrade on my work laptop, a HP Compaq 6910p. The upgrade went without
any problems. I have been using the machine every day for about 6 weeks now, and not
one bluescreen and I can't remember having any other problems either. The only "problem"
I had, is after upgrading, I had to do the same hacks to get bluetooth working again
as I did on Server 2008.<br /><br /><b>Conclusions:</b><br /><br />
I'm very happy with Windows Server 7 or 2008 R2. Things have only become better since
Vista and Server 2008. I must say I don't notice any performance differences, but
I hope they will come after a fresh install. I have been using the OS every day in
my job as a programmer, and there has been no complications. I think this is the best
OS I have ever used. 
<br /><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=bc11a90e-99fb-48fc-bf57-128ff1864ad4" /></body>
      <title>Updating to Windows Server 2008 R2</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,bc11a90e-99fb-48fc-bf57-128ff1864ad4.aspx</guid>
      <link>http://gaute.amende.no/UpdatingToWindowsServer2008R2.aspx</link>
      <pubDate>Sun, 08 Feb 2009 17:59:30 GMT</pubDate>
      <description>A while back I decided I wanted to try Windows 7 Beta, but since I had Windows Server 2008 installed already, I decided to try Windows Server 2008 R2 instead and just upgrade my existing installation.&lt;br&gt;
&lt;br&gt;
So I did an upgrade on my work laptop, a HP Compaq 6910p. The upgrade went without
any problems. I have been using the machine every day for about 6 weeks now, and not
one bluescreen and I can't remember having any other problems either. The only "problem"
I had, is after upgrading, I had to do the same hacks to get bluetooth working again
as I did on Server 2008.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Conclusions:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
I'm very happy with Windows Server 7 or 2008 R2. Things have only become better since
Vista and Server 2008. I must say I don't notice any performance differences, but
I hope they will come after a fresh install. I have been using the OS every day in
my job as a programmer, and there has been no complications. I think this is the best
OS I have ever used. 
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=bc11a90e-99fb-48fc-bf57-128ff1864ad4" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,bc11a90e-99fb-48fc-bf57-128ff1864ad4.aspx</comments>
      <category>Windows Server</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=0d310d04-8b74-4ef6-b6e3-da92b7bee665</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,0d310d04-8b74-4ef6-b6e3-da92b7bee665.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,0d310d04-8b74-4ef6-b6e3-da92b7bee665.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=0d310d04-8b74-4ef6-b6e3-da92b7bee665</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Often I have had problems logging in to
remote desktops because the two user sessions Microsoft allows you to have are occupied.
This is very annoying and until Windows Server 2008, you got no choice to log of the
other users. There is however a smart little command line tool to do this.<br /><br />
To see logged in users, type: qwinsta /server:SERVERNAME<br />
To log out an existing session, type: rwinsta /server:SERVERNAME SESSIONNAME<br /><br />
Here is an example from my machine:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">C:\Users\gm&gt;qwinsta
/server:kraftwerk SESSIONNAME USERNAME ID STATE TYPE DEVICE services 0 Disc console
1 Conn rdp-tcp#0 gm 2 Active rdpwd rdp-tcp 65536 Listen</span></pre><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">C:\Users\gm&gt;rwinsta
/server:kraftwerk rdp-tcp#0</span></pre><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=0d310d04-8b74-4ef6-b6e3-da92b7bee665" /></body>
      <title>Remote desktop - Kill other connections</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,0d310d04-8b74-4ef6-b6e3-da92b7bee665.aspx</guid>
      <link>http://gaute.amende.no/RemoteDesktopKillOtherConnections.aspx</link>
      <pubDate>Tue, 09 Dec 2008 17:35:12 GMT</pubDate>
      <description>Often I have had problems logging in to remote desktops because the two user sessions Microsoft allows you to have are occupied. This is very annoying and until Windows Server 2008, you got no choice to log of the other users. There is however a smart little command line tool to do this.&lt;br&gt;
&lt;br&gt;
To see logged in users, type: qwinsta /server:SERVERNAME&lt;br&gt;
To log out an existing session, type: rwinsta /server:SERVERNAME SESSIONNAME&lt;br&gt;
&lt;br&gt;
Here is an example from my machine:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;C:\Users\gm&amp;gt;qwinsta
/server:kraftwerk SESSIONNAME USERNAME ID STATE TYPE DEVICE services 0 Disc console
1 Conn rdp-tcp#0 gm 2 Active rdpwd rdp-tcp 65536 Listen&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;C:\Users\gm&amp;gt;rwinsta
/server:kraftwerk rdp-tcp#0&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=0d310d04-8b74-4ef6-b6e3-da92b7bee665" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,0d310d04-8b74-4ef6-b6e3-da92b7bee665.aspx</comments>
      <category>Windows Server</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=1068f7c7-3062-4a1b-9731-bb587163d213</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,1068f7c7-3062-4a1b-9731-bb587163d213.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,1068f7c7-3062-4a1b-9731-bb587163d213.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=1068f7c7-3062-4a1b-9731-bb587163d213</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Last week I had to make a SQL Procedure
go faster and a new join in the same operation. From the execution plan I could see
that the thing that cost most time was a call to an external function made as a .NET
CLR that got all the groups a person was member of, and returned it as a comma separated
string. This tok a couple of milliseconds, each time, and with some of the users input
resulting in over 8000 calls in a single statement, the procedure took to long time.
I also had the requirement to add an other column in the result, showing a comma separated
list of functions each persons had. Doing this the same way as before, would turn
the procedure almost twice as sluggish as before.<br /><br />
To get the result wanted, a small trick was used. Here is a simplified version of
what I wrote:<br /><pre><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"></span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SELECT</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">TOP</span> 10 <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">substring</span>(O.Functions,2,500) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">FROM</span> dbo.Tp_Person
P <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">CROSS</span> APPLY
( <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SELECT</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">','</span> + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CAST</span>(FunctionName <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">AS</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">varchar</span>) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">AS</span> [<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">text</span>()] <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">FROM</span> dbo.Tp_Function
F <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">WHERE</span> F.Personid
= P.Personid <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">FOR</span> XML
PATH(<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">''</span>)
) O( Functions )</span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /><br /></span><br />
This trick actually reduced the time of the procedure on 5000 people from about 30
seconds to 1,5 seconds. Made my day. 
<br /></pre><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=1068f7c7-3062-4a1b-9731-bb587163d213" /></body>
      <title>SQL to make a comma separated string from a resultset,</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,1068f7c7-3062-4a1b-9731-bb587163d213.aspx</guid>
      <link>http://gaute.amende.no/SQLToMakeACommaSeparatedStringFromAResultset.aspx</link>
      <pubDate>Mon, 08 Dec 2008 18:55:56 GMT</pubDate>
      <description>Last week I had to make a SQL Procedure go faster and a new join in the same operation. From the execution plan I could see that the thing that cost most time was a call to an external function made as a .NET CLR that got all the groups a person was member of, and returned it as a comma separated string. This tok a couple of milliseconds, each time, and with some of the users input resulting in over 8000 calls in a single statement, the procedure took to long time. I also had the requirement to add an other column in the result, showing a comma separated list of functions each persons had. Doing this the same way as before, would turn the procedure almost twice as sluggish as before.&lt;br&gt;
&lt;br&gt;
To get the result wanted, a small trick was used. Here is a simplified version of
what I wrote:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SELECT&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;TOP&lt;/span&gt; 10 &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;substring&lt;/span&gt;(O.Functions,2,500) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;FROM&lt;/span&gt; dbo.Tp_Person
P &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CROSS&lt;/span&gt; APPLY
( &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SELECT&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;','&lt;/span&gt; + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CAST&lt;/span&gt;(FunctionName &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;AS&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;varchar&lt;/span&gt;) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;AS&lt;/span&gt; [&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;text&lt;/span&gt;()] &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;FROM&lt;/span&gt; dbo.Tp_Function
F &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;WHERE&lt;/span&gt; F.Personid
= P.Personid &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;FOR&lt;/span&gt; XML
PATH(&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;''&lt;/span&gt;)
) O( Functions )&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;br&gt;
This trick actually reduced the time of the procedure on 5000 people from about 30
seconds to 1,5 seconds. Made my day. 
&lt;br&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=1068f7c7-3062-4a1b-9731-bb587163d213" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,1068f7c7-3062-4a1b-9731-bb587163d213.aspx</comments>
      <category>T-SQL</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=d11cb424-586d-422b-8c70-bba0498cfe1b</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,d11cb424-586d-422b-8c70-bba0498cfe1b.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,d11cb424-586d-422b-8c70-bba0498cfe1b.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=d11cb424-586d-422b-8c70-bba0498cfe1b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">This week I finally got around to install
Windows Server 2008 on my laptop. I have a HP Compaq 6910p laptop, which is a good
laptop with 64 bit dual core processor and 4 gb of RAM.<br /><br />
Since I have a 64-bit CPU and have been running Vista 64 for some time without any
problems, I installed the 64 bit enterprise edition.<br /><br />
I followed the blogpost: <a href="http://blogs.msdn.com/vijaysk/archive/2008/02/11/using-windows-server-2008-as-a-super-desktop-os.aspx">Using
Windows Server 2008 as a SUPER workstation OS</a> to get the OS configured. Which
got me pretty far, but not all the way there.<br /><br />
For drivers I downloaded the Vista drivers from HP's support page. They worked good
except for the bluetoouth driver. But apparently, Microsoft has decided that bluetooth
is not for servers, so there is no support. Both my bluetooth mouse and keyboard works
with the supplied dongle, but not with the built in device.<br /><br />
I also turned of Internet Explorer Enhanced Security, which is nice on a production
server, but extremly annoying on your desktop. This is done in Server Manager - Security
Information - Configure IE ESC.<br /><br />
I also use virtual pc's a bit and figured Hyper-V would be cool instead of Virtual
PC or Virtual Server, but when I activated this, the sleep function on my laptop no
longer worked. I did find a good hack for <a href="http://markharrison.co.uk/blog/2008/09/sleep-hibernate-with-hyper-v.htm">enabling
sleep with Hyper-V</a>. Works ok, but it's not 100% ideal solution.<br /><br />
The current version of Live Messenger would not install either<b></b>, but the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=D78F2FF1-79EA-4066-8BA0-DDBED94864FC&amp;displaylang=en">MSI-version
of Messenger 8.1</a> would install. Apparantly you can also copy the installed files
from a Vista machine to get newer versions running, but I haven't tried.<br /><br />
Visual Studio 2008, SQL Server 2008, Office 2007, Firefox, Daemon tools all installed
wihout any problems.<br /><br /><b>Final thoughts<br /></b>All in all, I am very happy with my transition to Server 2008. It seems faster
than Vista, and the fact that you have to turn all the bloat on instead of the opposite
makes it great.<br /><br />
Update: I got bluetooth devices working using this blog post: <a href="http://www.gilkirkpatrick.com/Blog/post/Installing-the-Microsoft-Bluetooth-Stack-on-Windows-Server-2008.aspx">Installing
the Microsoft Bluetooth Stack on Windows Server 2008</a><br /><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=d11cb424-586d-422b-8c70-bba0498cfe1b" /></body>
      <title>Installing Windows Server 2008 on a HP Compaq 6910p laptop</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,d11cb424-586d-422b-8c70-bba0498cfe1b.aspx</guid>
      <link>http://gaute.amende.no/InstallingWindowsServer2008OnAHPCompaq6910pLaptop.aspx</link>
      <pubDate>Fri, 31 Oct 2008 13:45:09 GMT</pubDate>
      <description>This week I finally got around to install Windows Server 2008 on my laptop. I have a HP Compaq 6910p laptop, which is a good laptop with 64 bit dual core processor and 4 gb of RAM.&lt;br&gt;
&lt;br&gt;
Since I have a 64-bit CPU and have been running Vista 64 for some time without any
problems, I installed the 64 bit enterprise edition.&lt;br&gt;
&lt;br&gt;
I followed the blogpost: &lt;a href="http://blogs.msdn.com/vijaysk/archive/2008/02/11/using-windows-server-2008-as-a-super-desktop-os.aspx"&gt;Using
Windows Server 2008 as a SUPER workstation OS&lt;/a&gt; to get the OS configured. Which
got me pretty far, but not all the way there.&lt;br&gt;
&lt;br&gt;
For drivers I downloaded the Vista drivers from HP's support page. They worked good
except for the bluetoouth driver. But apparently, Microsoft has decided that bluetooth
is not for servers, so there is no support. Both my bluetooth mouse and keyboard works
with the supplied dongle, but not with the built in device.&lt;br&gt;
&lt;br&gt;
I also turned of Internet Explorer Enhanced Security, which is nice on a production
server, but extremly annoying on your desktop. This is done in Server Manager - Security
Information - Configure IE ESC.&lt;br&gt;
&lt;br&gt;
I also use virtual pc's a bit and figured Hyper-V would be cool instead of Virtual
PC or Virtual Server, but when I activated this, the sleep function on my laptop no
longer worked. I did find a good hack for &lt;a href="http://markharrison.co.uk/blog/2008/09/sleep-hibernate-with-hyper-v.htm"&gt;enabling
sleep with Hyper-V&lt;/a&gt;. Works ok, but it's not 100% ideal solution.&lt;br&gt;
&lt;br&gt;
The current version of Live Messenger would not install either&lt;b&gt;&lt;/b&gt;, but the &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=D78F2FF1-79EA-4066-8BA0-DDBED94864FC&amp;amp;displaylang=en"&gt;MSI-version
of Messenger 8.1&lt;/a&gt; would install. Apparantly you can also copy the installed files
from a Vista machine to get newer versions running, but I haven't tried.&lt;br&gt;
&lt;br&gt;
Visual Studio 2008, SQL Server 2008, Office 2007, Firefox, Daemon tools all installed
wihout any problems.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Final thoughts&lt;br&gt;
&lt;/b&gt;All in all, I am very happy with my transition to Server 2008. It seems faster
than Vista, and the fact that you have to turn all the bloat on instead of the opposite
makes it great.&lt;br&gt;
&lt;br&gt;
Update: I got bluetooth devices working using this blog post: &lt;a href="http://www.gilkirkpatrick.com/Blog/post/Installing-the-Microsoft-Bluetooth-Stack-on-Windows-Server-2008.aspx"&gt;Installing
the Microsoft Bluetooth Stack on Windows Server 2008&lt;/a&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=d11cb424-586d-422b-8c70-bba0498cfe1b" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,d11cb424-586d-422b-8c70-bba0498cfe1b.aspx</comments>
      <category>Windows Server</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=6b0672a0-1adf-4322-a84a-8a646686779c</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=6b0672a0-1adf-4322-a84a-8a646686779c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Today I got an error on a web application
after upgrading it from .Net 2.0 and Visual Studio 2005 to .Net 3.5. The project uses
Telerik radcontrol, a few releases old version. After upgrading, every page that uses
the radcontrol DatePicker failed to load, and the dreaded "Yellow screen of death"
was shown instead.<br /><br />
It wasen't my code that failed, it was the radcontrols own prerender-event that threw
the exception. Since I don't have the source code for this control and due to the
fact that the fault only appaired on one test server without Visual Studio installed,
it was hard to debug. Luckily a colleague had experienced a similair problem some
weeks ago, and I remembered that changing the locale settings helped him (He is swedish,
and used a swedish locale in an otherwise homogenic norwegian development team).<br /><br />
I logged in to the server and tried to change the system locale of the admin user.
No help.<br />
I tried to change the user running the application pool from NETWORK SERVICE to the
admin I logged in as. Suddenly everything worked.<br /><br />
I did however want to run the process as NETWORK SERVICE and not administrator or
have to make another account just for this reason. Therefore I wanted to change the
network services locale settings, but since this is a user you can't log in as, this
seemed hard.<br /><br />
Luckily I found this blog post: <a href="http://www.request-response.com/blog/PermaLink,guid,e6ba0263-8825-4a6d-91e8-f5ce7f92111e.aspx">IIS6:
Changing the Locale ID when Regional Settings Won't Work</a><br />
I followed the description to change the settings. For some reason this didn't quite
work. Probably due to a typo. I tried to export the regedit settings for HKEY_USERS\S-1-5-20
and just manually change the sShortDate, from the English setting and to norwegian
settings. Now everything is working again!<p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=6b0672a0-1adf-4322-a84a-8a646686779c" /></body>
      <title>"String was not recognized as a valid DateTime" from Telerik Calendar Datepicker</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</guid>
      <link>http://gaute.amende.no/StringWasNotRecognizedAsAValidDateTimeFromTelerikCalendarDatepicker.aspx</link>
      <pubDate>Tue, 28 Oct 2008 20:20:13 GMT</pubDate>
      <description>Today I got an error on a web application after upgrading it from .Net 2.0 and Visual Studio 2005 to .Net 3.5. The project uses Telerik radcontrol, a few releases old version. After upgrading, every page that uses the radcontrol DatePicker failed to load, and the dreaded "Yellow screen of death" was shown instead.&lt;br&gt;
&lt;br&gt;
It wasen't my code that failed, it was the radcontrols own prerender-event that threw
the exception. Since I don't have the source code for this control and due to the
fact that the fault only appaired on one test server without Visual Studio installed,
it was hard to debug. Luckily a colleague had experienced a similair problem some
weeks ago, and I remembered that changing the locale settings helped him (He is swedish,
and used a swedish locale in an otherwise homogenic norwegian development team).&lt;br&gt;
&lt;br&gt;
I logged in to the server and tried to change the system locale of the admin user.
No help.&lt;br&gt;
I tried to change the user running the application pool from NETWORK SERVICE to the
admin I logged in as. Suddenly everything worked.&lt;br&gt;
&lt;br&gt;
I did however want to run the process as NETWORK SERVICE and not administrator or
have to make another account just for this reason. Therefore I wanted to change the
network services locale settings, but since this is a user you can't log in as, this
seemed hard.&lt;br&gt;
&lt;br&gt;
Luckily I found this blog post: &lt;a href="http://www.request-response.com/blog/PermaLink,guid,e6ba0263-8825-4a6d-91e8-f5ce7f92111e.aspx"&gt;IIS6:
Changing the Locale ID when Regional Settings Won't Work&lt;/a&gt;
&lt;br&gt;
I followed the description to change the settings. For some reason this didn't quite
work. Probably due to a typo. I tried to export the regedit settings for HKEY_USERS\S-1-5-20
and just manually change the sShortDate, from the English setting and to norwegian
settings. Now everything is working again!&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=6b0672a0-1adf-4322-a84a-8a646686779c" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=930e8dc9-8c4f-471e-b272-98a26ed5c640</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,930e8dc9-8c4f-471e-b272-98a26ed5c640.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,930e8dc9-8c4f-471e-b272-98a26ed5c640.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=930e8dc9-8c4f-471e-b272-98a26ed5c640</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;">
Checking if a email addresses is valid in SQL can be very convinient from time to
time. Here are two ways to do it in SQL, and a bit about them.
</p>
        <p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;">
 
</p>
        <p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;">
Here is a full SQL way to do this:
</p>
        <p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;">
 
</p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">
            <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">Create</span>
            <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">function</span> VALIDEMAIL(@s <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">varchar</span>(255)) 
<br /><span style="color: Teal; background-color: transparent; font-family: Courier New; font-size: 11px;">--Returns
true if the string is a valid email address.</span><br />
returns <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bit</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">DECLARE</span> @u <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">VARCHAR</span>(60),
@v <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">VARCHAR</span>(60),
@x <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">VARCHAR</span>(60),
@i <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>,
@j <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>,
@result <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bit</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=1<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">set</span> @s
= <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">rtrim</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">ltrim</span>(@s))<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">len</span>(@s)-<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHARINDEX</span>(<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'@'</span>,<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">reverse</span>(@s))+1<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> @i
&lt;= 1 <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">or</span><span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">len</span>(@s)
&lt; 5<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">begin</span><br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">set</span> @result
= 0<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">goto</span> done<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">end</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @u=<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEFT</span>(@s,@i-1)<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @j=<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">len</span>(@s)-<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHARINDEX</span>(<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'.'</span>,<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">reverse</span>(@s))+1<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @v=<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">RIGHT</span>(@s,<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@s)-@j)<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> @j
&lt; 3 <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">or</span> @j
&lt; @i<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">begin</span><br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">set</span> @result
= 0<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">goto</span> done<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">end</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @x=<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">substring</span>(@s,@i+1,@j-@i-1)<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">IF</span><span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@x)&lt;2<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=0<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">GOTo</span> done<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">IF</span> (<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@x)=3) <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">AND</span> (@x <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NOT</span><span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">LIKE</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'[a-zA-Z][a-zA-Z][a-zA-Z]'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">Collate</span> 
Latin1_General_BIN)<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=0<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">GOTo</span> done<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">IF</span> (<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@x)=2) <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">AND</span> (@x <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NOT</span><span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">LIKE</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'[a-zA-Z][a-zA-Z]'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">Collate</span> 
Latin1_General_BIN)<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=0<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">GOTo</span> done<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=1<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">WHILE</span> (@i&lt;<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@u))<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">IF</span><span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">SUBSTRING</span>(@u,@i,1) <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NOT</span><span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">LIKE</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'[a-zA-Z0-9_\-.+=/!#$&amp;''*%?^`{|}"@\\
]'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">escape</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'\'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">Collate</span> 
Latin1_General_BIN<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=0<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">GOTo</span> done<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=@i+1<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=1<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">WHILE</span> (@i&lt;<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@v))<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">IF</span><span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">SUBSTRING</span>(@v,@i,1) <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NOT</span><span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">LIKE</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'[a-zA-Z]'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">Collate</span> Latin1_General_BIN<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=0<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">GOTo</span> done<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=@i+1<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=1<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">WHILE</span> (@i&lt;<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LEN</span>(@x))<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">IF</span><span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">SUBSTRING</span>(@x,@i,1) <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NOT</span><span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">LIKE</span> N<span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'[a-zA-Z\-.0-9]'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">escape</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'\'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">Collate</span> 
Latin1_General_BIN<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BEGIN</span><br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @result=0<br />
     <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">goto</span> done<br />
  <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @i=@i+1<br />
 <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br />
done:<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> @result<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">END</span><br />
GO<br /></span>
        </pre>
        <p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;">
        </p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Here is another way, where you insert .NET CLR code in the database. Here I use the
.NET RegEx.Ismatch, so I can use it for tons of other stuff as well with the right
input.<br /></p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
        </p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">
            <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Collections.Generic; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Text; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.SqlServer.Server; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Data.SqlTypes; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Text.RegularExpressions; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> SQLSignature
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> SqlFunctions
{ [SqlFunction(IsDeterministic <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">true</span>,
DataAccess <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> DataAccessKind.None)] <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span> SqlBoolean
fn_RegexIsMatch(SqlString input, SqlString pattern) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> (SqlBoolean)Regex.IsMatch(input.Value,
pattern.Value); } } } </span>
        </pre>In SQL: 
<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">CREATE</span> ASSEMBLY
SQLSignature <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">FROM</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'C:\CLR
Assemblies\Performance\SQLFunctions\bin\SQLFunctions.dll'</span>; GO <span style="color: Teal; background-color: transparent; font-family: Courier New; font-size: 11px;">--
Create fn_SQLSigCLR and fn_RegexReplace functions</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">CREATE</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">FUNCTION</span> dbo.fn_RegexIsMatch(@input <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">AS</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">NVARCHAR</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">MAX</span>),@pattern <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">AS</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">NVARCHAR</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">MAX</span>))RETURNS <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BIT</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">WITH</span> RETURNS <span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NULL</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">ON</span><span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;">NULL</span> INPUT
EXTERNAL NAME SQLSignature.SQLFunctions.fn_RegexIsMatch; GO </span></pre>This can
be run with a command like:<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">select</span> *
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">from</span> [AW].[person].emailaddress <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> p
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">where</span> dbo.fn_RegexIsMatch(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">RTRIM</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LTRIM</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">lower</span>(P.emailaddress))),<br /><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"> '^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$'</span>)=1 </span></pre><br /><p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
I did some timings on the two functions and as you can see below there are big differences.
The .NET CLR one is almost 8 times faster(cpu time) than the T-SQL one. It's also
a whole lot easier to read, and can be used for all other RegEx's needed. You do however
need to get out of management studio to make it, and 1.5 seconds is for processing
almost 20000 email addresses still should be fast enough for most uses.
</p><p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"></p><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">STATISTICS</span> TIME <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">ON</span>     <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">select</span> *
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">from</span> [AW].[person].emailaddress <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> p
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">where</span> dbo.VALIDEMAIL(P.emailaddress)=1
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">select</span> *
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">from</span> [AW].[person].emailaddress <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> p
    <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">where</span> dbo.fn_RegexIsMatch(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">RTRIM</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">LTRIM</span>(<span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">lower</span>(P.emailaddress))),<br /><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$'</span>)=1
(19929 row(s) affected) SQL Server Execution Times: CPU time = 1466 ms, elapsed time
= 1911 ms. (19972 row(s) affected) SQL Server Execution Times: CPU time = 203 ms,
elapsed time = 380 ms. </span></pre>It's pretty cool to see that .NET performs good
within SQL as well. I am primarily a .NET developer but do have to use SQL quite a
bit as well, but the fact that .NET outperforms SQL both in readability and speed
inside SQL on some actions make my job a lot easier.<br /><p></p><br /><p></p><p></p><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=930e8dc9-8c4f-471e-b272-98a26ed5c640" /></body>
      <title>Check valid emails in SQL. .NET CLR vs SQL approach </title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,930e8dc9-8c4f-471e-b272-98a26ed5c640.aspx</guid>
      <link>http://gaute.amende.no/CheckValidEmailsInSQLNETCLRVsSQLApproach.aspx</link>
      <pubDate>Thu, 23 Oct 2008 08:48:20 GMT</pubDate>
      <description>

&lt;p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;"&gt;
Checking if a email addresses is valid in SQL can be very convinient from time to
time. Here are two ways to do it in SQL, and a bit about them.
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;"&gt;
Here is a full SQL way to do this:
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;Create&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;function&lt;/span&gt; VALIDEMAIL(@s &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;varchar&lt;/span&gt;(255)) 
&lt;br&gt;
&lt;span style="color: Teal; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;--Returns
true if the string is a valid email address.&lt;/span&gt;
&lt;br&gt;
returns &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bit&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;DECLARE&lt;/span&gt; @u &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;VARCHAR&lt;/span&gt;(60),
@v &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;VARCHAR&lt;/span&gt;(60),
@x &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;VARCHAR&lt;/span&gt;(60),
@i &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;,
@j &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;,
@result &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bit&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=1&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;set&lt;/span&gt; @s
= &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;rtrim&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;ltrim&lt;/span&gt;(@s))&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;len&lt;/span&gt;(@s)-&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHARINDEX&lt;/span&gt;(&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'@'&lt;/span&gt;,&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;reverse&lt;/span&gt;(@s))+1&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; @i
&amp;lt;= 1 &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;or&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;len&lt;/span&gt;(@s)
&amp;lt; 5&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;begin&lt;/span&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;set&lt;/span&gt; @result
= 0&lt;br&gt;
&amp;nbsp; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;goto&lt;/span&gt; done&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;end&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @u=&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEFT&lt;/span&gt;(@s,@i-1)&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @j=&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;len&lt;/span&gt;(@s)-&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHARINDEX&lt;/span&gt;(&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'.'&lt;/span&gt;,&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;reverse&lt;/span&gt;(@s))+1&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @v=&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;RIGHT&lt;/span&gt;(@s,&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@s)-@j)&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; @j
&amp;lt; 3 &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;or&lt;/span&gt; @j
&amp;lt; @i&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;begin&lt;/span&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;set&lt;/span&gt; @result
= 0&lt;br&gt;
&amp;nbsp; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;goto&lt;/span&gt; done&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;end&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @x=&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;substring&lt;/span&gt;(@s,@i+1,@j-@i-1)&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IF&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@x)&amp;lt;2&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=0&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;GOTo&lt;/span&gt; done&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IF&lt;/span&gt; (&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@x)=3) &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;AND&lt;/span&gt; (@x &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LIKE&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'[a-zA-Z][a-zA-Z][a-zA-Z]'&lt;/span&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;Collate&lt;/span&gt;&amp;nbsp;
Latin1_General_BIN)&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=0&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;GOTo&lt;/span&gt; done&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IF&lt;/span&gt; (&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@x)=2) &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;AND&lt;/span&gt; (@x &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LIKE&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'[a-zA-Z][a-zA-Z]'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;Collate&lt;/span&gt;&amp;nbsp;
Latin1_General_BIN)&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=0&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;GOTo&lt;/span&gt; done&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=1&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;WHILE&lt;/span&gt; (@i&amp;lt;&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@u))&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IF&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SUBSTRING&lt;/span&gt;(@u,@i,1) &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LIKE&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'[a-zA-Z0-9_\-.+=/!#$&amp;amp;''*%?^`{|}"@\\
]'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;escape&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'\'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;Collate&lt;/span&gt;&amp;nbsp;
Latin1_General_BIN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;GOTo&lt;/span&gt; done&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=@i+1&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=1&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;WHILE&lt;/span&gt; (@i&amp;lt;&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@v))&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IF&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SUBSTRING&lt;/span&gt;(@v,@i,1) &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LIKE&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'[a-zA-Z]'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;Collate&lt;/span&gt; Latin1_General_BIN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;GOTo&lt;/span&gt; done&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=@i+1&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=1&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;WHILE&lt;/span&gt; (@i&amp;lt;&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LEN&lt;/span&gt;(@x))&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;IF&lt;/span&gt; &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SUBSTRING&lt;/span&gt;(@x,@i,1) &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LIKE&lt;/span&gt; N&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'[a-zA-Z\-.0-9]'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;escape&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'\'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;Collate&lt;/span&gt;&amp;nbsp;
Latin1_General_BIN&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @result=0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;goto&lt;/span&gt; done&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @i=@i+1&lt;br&gt;
&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
done:&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; @result&lt;br&gt;
&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;END&lt;/span&gt;
&lt;br&gt;
GO&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p style="margin: 0in; font-family: arial; font-size: 10pt; color: black;"&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Here is another way, where you insert .NET CLR code in the database. Here I use the
.NET RegEx.Ismatch, so I can use it for tons of other stuff as well with the right
input.&lt;br&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Collections.Generic; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Text; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.SqlServer.Server; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Data.SqlTypes; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Text.RegularExpressions; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; SQLSignature
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; SqlFunctions
{ [SqlFunction(IsDeterministic &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;true&lt;/span&gt;,
DataAccess &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; DataAccessKind.None)] &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; SqlBoolean
fn_RegexIsMatch(SqlString input, SqlString pattern) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; (SqlBoolean)Regex.IsMatch(input.Value,
pattern.Value); } } } &lt;/span&gt;&lt;/pre&gt;In SQL: 
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CREATE&lt;/span&gt; ASSEMBLY
SQLSignature &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;FROM&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'C:\CLR
Assemblies\Performance\SQLFunctions\bin\SQLFunctions.dll'&lt;/span&gt;; GO &lt;span style="color: Teal; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;--
Create fn_SQLSigCLR and fn_RegexReplace functions&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CREATE&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;FUNCTION&lt;/span&gt; dbo.fn_RegexIsMatch(@input &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;AS&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NVARCHAR&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;MAX&lt;/span&gt;),@pattern &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;AS&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NVARCHAR&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;MAX&lt;/span&gt;))RETURNS &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BIT&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;WITH&lt;/span&gt; RETURNS &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NULL&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;ON&lt;/span&gt; &lt;span style="color: Silver; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;NULL&lt;/span&gt; INPUT
EXTERNAL NAME SQLSignature.SQLFunctions.fn_RegexIsMatch; GO &lt;/span&gt;&lt;/pre&gt;This can
be run with a command like:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;select&lt;/span&gt; *
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;from&lt;/span&gt; [AW].[person].emailaddress &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; p
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;where&lt;/span&gt; dbo.fn_RegexIsMatch(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;RTRIM&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LTRIM&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;lower&lt;/span&gt;(P.emailaddress))),&lt;br&gt;
&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt; '^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$'&lt;/span&gt;)=1 &lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
I did some timings on the two functions and as you can see below there are big differences.
The .NET CLR one is almost 8 times faster(cpu time) than the T-SQL one. It's also
a whole lot easier to read, and can be used for all other RegEx's needed. You do however
need to get out of management studio to make it, and 1.5 seconds is for processing
almost 20000 email addresses still should be fast enough for most uses.
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;STATISTICS&lt;/span&gt; TIME &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;ON&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;select&lt;/span&gt; *
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;from&lt;/span&gt; [AW].[person].emailaddress &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; p
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;where&lt;/span&gt; dbo.VALIDEMAIL(P.emailaddress)=1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;select&lt;/span&gt; *
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;from&lt;/span&gt; [AW].[person].emailaddress &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; p
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;where&lt;/span&gt; dbo.fn_RegexIsMatch(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;RTRIM&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;LTRIM&lt;/span&gt;(&lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;lower&lt;/span&gt;(P.emailaddress))),&lt;br&gt;
&lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$'&lt;/span&gt;)=1
(19929 row(s) affected) SQL Server Execution Times: CPU time = 1466 ms, elapsed time
= 1911 ms. (19972 row(s) affected) SQL Server Execution Times: CPU time = 203 ms,
elapsed time = 380 ms. &lt;/span&gt;&lt;/pre&gt;It's pretty cool to see that .NET performs good
within SQL as well. I am primarily a .NET developer but do have to use SQL quite a
bit as well, but the fact that .NET outperforms SQL both in readability and speed
inside SQL on some actions make my job a lot easier.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=930e8dc9-8c4f-471e-b272-98a26ed5c640" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,930e8dc9-8c4f-471e-b272-98a26ed5c640.aspx</comments>
      <category>.NET</category>
      <category>T-SQL</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=2916dc2c-5419-4415-9cbb-daae00fc1785</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=2916dc2c-5419-4415-9cbb-daae00fc1785</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
All most every ASP.NET website I have seen, uses Databinder.Eval(Container.DataItem,
"colum1") in repeaters to get the data passed in and show it on screen. The problem
with Databinder.Eval is that it uses reflection to evaluate the item. This is time
consuming and unnecessary, so instead, one should use casting to obtain the same effect.
Try not to do it this way:
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
        </p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;asp:repeater
id=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"rptAccounts"</span> runat=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"server"</span>&gt;
    &lt;itemtemplate&gt;         &lt;%#
Databinder.Eval(Container.DataItem, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"colum1"</span>)
%&gt; &lt;br /&gt;     &lt;/itemtemplate&gt; &lt;/asp:repeater&gt; </span>
        </pre>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
But use this one instead:
</p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;asp:repeater
id=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"rptAccounts"</span> runat=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"server"</span>&gt;<br />
    &lt;itemtemplate&gt;<br />
        &lt;%# ((BusinessObjects.Object)Container.DataItem).Column1
%&gt; &lt;br /&gt;<br />
    &lt;/itemtemplate&gt;<br />
&lt;/asp:repeater&gt;<br /><br /></span>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
This will also give you a compiler warning if the members of a class should change,
and<br />
not just runtime fail such as the first example. If you are not using business<br />
objects, but a DataTable, you can<span style="">  </span>cast<br />
to datarow and get the column right out of there like this:<span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /></span></p>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
            <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">
              <br />
            </span>
          </p>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
            <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;asp:repeater
id=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"rptAccounts"</span> runat=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"server"</span>&gt;
    &lt;itemtemplate&gt;         &lt;%#
((DataRow)Container.DataItem)[<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Column1"</span>]
%&gt; &lt;br /&gt;     &lt;/itemtemplate&gt; &lt;/asp:repeater&gt; </span>
          </p>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
This both increases speed and makes the application more robust to runtime errors.
A win-win situation:) 
</p>
          <br />
          <br />
        </pre>
        <p>
        </p>
        <p>
        </p>
        <img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2916dc2c-5419-4415-9cbb-daae00fc1785" />
      </body>
      <title>Stay away from DataBinder.Eval, use explicit  casting</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</guid>
      <link>http://gaute.amende.no/StayAwayFromDataBinderEvalUseExplicitCasting.aspx</link>
      <pubDate>Wed, 03 Sep 2008 18:42:36 GMT</pubDate>
      <description>

&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
All most every ASP.NET website I have seen, uses Databinder.Eval(Container.DataItem,
"colum1") in repeaters to get the data passed in and show it on screen. The problem
with Databinder.Eval is that it uses reflection to evaluate the item. This is time
consuming and unnecessary, so instead, one should use casting to obtain the same effect.
Try not to do it this way:
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;asp:repeater
id=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"rptAccounts"&lt;/span&gt; runat=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"server"&lt;/span&gt;&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;itemtemplate&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%#
Databinder.Eval(Container.DataItem, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"colum1"&lt;/span&gt;)
%&amp;gt; &amp;lt;br /&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/itemtemplate&amp;gt; &amp;lt;/asp:repeater&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
But use this one instead:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;asp:repeater
id=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"rptAccounts"&lt;/span&gt; runat=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"server"&lt;/span&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;itemtemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%# ((BusinessObjects.Object)Container.DataItem).Column1
%&amp;gt; &amp;lt;br /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/itemtemplate&amp;gt;&lt;br&gt;
&amp;lt;/asp:repeater&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
This will also give you a compiler warning if the members of a class should change,
and&lt;br&gt;
not just runtime fail such as the first example. If you are not using business&lt;br&gt;
objects, but a DataTable, you can&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;cast&lt;br&gt;
to datarow and get the column right out of there like this:&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;asp:repeater
id=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"rptAccounts"&lt;/span&gt; runat=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"server"&lt;/span&gt;&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;itemtemplate&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%#
((DataRow)Container.DataItem)[&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Column1"&lt;/span&gt;]
%&amp;gt; &amp;lt;br /&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/itemtemplate&amp;gt; &amp;lt;/asp:repeater&amp;gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
This both increases speed and makes the application more robust to runtime errors.
A win-win situation:) 
&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2916dc2c-5419-4415-9cbb-daae00fc1785" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=dfa52b04-e2df-4982-8fa4-27c7ff01f603</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=dfa52b04-e2df-4982-8fa4-27c7ff01f603</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Third party applications are great, and its very often smart to use open source or
buy modules you need before making your own. However a lot of third party applications
do create some problems. One is that prices and functionality can change and new,
better and cheaper products come to surface.
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
 
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Therefore, you do not want to be to dependent on these applications, so if you find
another tool that does the same thing even better, you should be able to switch it
out easy. A good way to do this is to create a interface and a factory pattern, and
refer to the interface in your code and let the factory class decide what tool to
use.<span style="">  </span>You can also use this pattern to ease mocking of
objects. And let the factory return a mock object if you are unit testing.
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
          <br />
        </p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Example in C#:<br /></p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
        </p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">
            <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> Logger
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
An interface is created</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">interface</span> iLog
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> Log(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> s);
} <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//An
implementation using enterprise libraries logging</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> EntLog
: iLog { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> Log(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> s)
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Implement
logic to save to log</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">false</span>;
} } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//An
implementation using log4net to save logs.</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> log4net
: iLog { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> Log(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> s)
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Implement
logic to save to log</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">false</span>;
} } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
A factory class</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> LogFactory
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
Creates an object, of the right type.</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span> iLog
CreateLog(){ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> log4net();
} } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Program
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Main(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>[]
args) { <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
Get the log object to use.</span> iLog logger <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> LogFactory.CreateLog(); <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Print
the type of logger used</span> Console.Out.WriteLine(logger.GetType()); <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
Log a message and write the result</span> Console.Out.WriteLine(logger.Log(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Something
is happening"</span>)); Console.ReadKey(); } } } </span>
        </pre>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
To change this to use entLog instead of log4net, all you have to do is change the
object created in the logfactory.<span style="">  </span>To add a new logger,
all you have to do is create a new class to implement Ilog and change the creator
to return the new logger. The creator could also have a constructor that takes an
input, so that you can decide in the class using the log what kind of log you want
to use.
</p>
        <p>
        </p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
 
</p>
        <p>
        </p>
        <img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=dfa52b04-e2df-4982-8fa4-27c7ff01f603" />
      </body>
      <title>Creating a Factory class and an Interface for your third party applications.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</guid>
      <link>http://gaute.amende.no/CreatingAFactoryClassAndAnInterfaceForYourThirdPartyApplications.aspx</link>
      <pubDate>Mon, 01 Sep 2008 11:33:16 GMT</pubDate>
      <description>

&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Third party applications are great, and its very often smart to use open source or
buy modules you need before making your own. However a lot of third party applications
do create some problems. One is that prices and functionality can change and new,
better and cheaper products come to surface.
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Therefore, you do not want to be to dependent on these applications, so if you find
another tool that does the same thing even better, you should be able to switch it
out easy. A good way to do this is to create a interface and a factory pattern, and
refer to the interface in your code and let the factory class decide what tool to
use.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;You can also use this pattern to ease mocking of
objects. And let the factory return a mock object if you are unit testing.
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Example in C#:&lt;br&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; Logger
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
An interface is created&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;interface&lt;/span&gt; iLog
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; Log(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; s);
} &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//An
implementation using enterprise libraries logging&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; EntLog
: iLog { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; Log(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; s)
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Implement
logic to save to log&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;;
} } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//An
implementation using log4net to save logs.&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; log4net
: iLog { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; Log(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; s)
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Implement
logic to save to log&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;;
} } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
A factory class&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; LogFactory
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
Creates an object, of the right type.&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; iLog
CreateLog(){ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; log4net();
} } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; Program
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Main(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;[]
args) { &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
Get the log object to use.&lt;/span&gt; iLog logger &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; LogFactory.CreateLog(); &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Print
the type of logger used&lt;/span&gt; Console.Out.WriteLine(logger.GetType()); &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
Log a message and write the result&lt;/span&gt; Console.Out.WriteLine(logger.Log(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Something
is happening"&lt;/span&gt;)); Console.ReadKey(); } } } &lt;/span&gt;&lt;/pre&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
To change this to use entLog instead of log4net, all you have to do is change the
object created in the logfactory.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;To add a new logger,
all you have to do is create a new class to implement Ilog and change the creator
to return the new logger. The creator could also have a constructor that takes an
input, so that you can decide in the class using the log what kind of log you want
to use.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=dfa52b04-e2df-4982-8fa4-27c7ff01f603" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</comments>
      <category>.NET</category>
      <category>Patterns</category>
    </item>
  </channel>
</rss>