<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.Net 2.0-3.5</title>
        <link>http://www.dotnetlog.com/category/9.aspx</link>
        <description>.Net 2.0-3.5</description>
        <language>en-US</language>
        <copyright>Bigyan Rajbhandari</copyright>
        <managingEditor>bigyanr@gmail.com</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>Using XML to perform database operations rather than parameterized query?</title>
            <link>http://dotnetlog.com/archive/2008/08/24/using-xml-to-perform-database-operations-rather-than-parameterized-query.aspx</link>
            <description>&lt;p&gt;There's no doubt that today xml is becoming more and more prevalent as platform-independent data format. Starting SQL server 2005, Microsoft also added tons of support for xml and a new native data type XML, since then I have started to see lots of applications that utilize this data type to run very complex solutions. A new trend is starting to emerge, as one of my colleagues calls it a "paradigm shift", where applications use xml serialized objects (that map to tables) to perform inserts/updates to table rather than having to pass each parameter independently.  However, the question remains if this approach is necessarily better than previous? &lt;/p&gt;
&lt;p&gt;For example consider the following scenario: &lt;br /&gt;
In middle-Tier (.Net Code) my object looks like the following&lt;/p&gt;
&lt;p&gt;&lt;img width="171" height="164" src="/images/dotnetlog_com/082408_1855_UsingXMLtop1.png" alt="" /&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt; 		&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;My serialized xml looks like below:&lt;/strong&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Customer&lt;/span&gt;&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;customerID&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;12&lt;/span&gt;"&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;customerName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Bigyan&lt;/span&gt;"&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;address&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;123 Redmond Way&lt;/span&gt;"&lt;span style="color: blue;"&gt; 			&lt;/span&gt;&lt;span style="color: red;"&gt;phone&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;123-123-3434&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;And my call to database stored proc like:&lt;/strong&gt;&lt;br /&gt;
&lt;span style="color: rgb(0, 112, 192);"&gt;exec&lt;/span&gt; Customer_UpdateInformation @customerXml &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In parameterized query I would have done something like this:&lt;br /&gt;
&lt;/strong&gt;&lt;span style="color: rgb(0, 112, 192);"&gt;exec&lt;/span&gt; Customer_UpdateInformation @customerID, @customerName, @addresss, @phone&lt;strong&gt;     &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are both pros and cons to this approach, which I'll explain next. But I also want to clarify that I am NOT advocating one approach vs other, but actually want your opinion on this approach.  &lt;/p&gt;
&lt;h3&gt;Pros: &lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt;Instead of passing 4 parameters, I need to pass only 1. Thus my signature for the database contract is much more succinct. &lt;/li&gt;
    &lt;li&gt;If I need to add or modify field, I have less code to deal with and do NOT need to change my signature &lt;/li&gt;
    &lt;li&gt;If I am just performing updates on certain fields (not all), I might be able to pass only updated fields and reduce my message size &lt;/li&gt;
    &lt;li&gt;Serialization and Deserialization on my .Net objects can be automated, thus reducing effort to map those fields with database fields. &lt;/li&gt;
    &lt;li&gt;If I wanted to perform bulk updates, then I have ability to pass multiple elements in my xml; this greatly reduces chatting between .Net and Sql Server. &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Cons: &lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt;First and foremost, I'll lose type safety on the parameters passed. &lt;/li&gt;
    &lt;li&gt;Xml parsing is little expensive (not a whole lot), compared to primitive data types. Bulk process might make up ground in this area. &lt;/li&gt;
    &lt;li&gt;
    &lt;div&gt;Data contract I am exposing through the stored procedure is not self explanatory. Just looking at the parameter, no one will know what to pass.  &lt;/div&gt;
    &lt;p&gt;&lt;br /&gt;
     &lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://dotnetlog.com/aggbug/59.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2008/08/24/using-xml-to-perform-database-operations-rather-than-parameterized-query.aspx</guid>
            <pubDate>Sun, 24 Aug 2008 18:55:16 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/59.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2008/08/24/using-xml-to-perform-database-operations-rather-than-parameterized-query.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/59.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/59.aspx</trackback:ping>
        </item>
        <item>
            <title>Best practice and effective way of using DataContext in Linq to SQL?</title>
            <link>http://dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx</link>
            <description>&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;NOTE: See my &lt;/strong&gt;&lt;a href="http://www.dotnetlog.com/archive/2008/03/20/follow-up--best-practice-and-effective-use-of-datacontext.aspx"&gt;&lt;strong&gt;follow up post here&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; for more info.&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;At work, Jeff and I have been throwing around ideas to find a best way to implement DataContext in Linq so that we can integrate it into the base class in our framework while achieving following goals. &lt;/font&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;font size="2"&gt;Implementation should be easy and non-redundant, so that we do not need to do &lt;span style="COLOR: blue"&gt;new DataContext(), &lt;/span&gt;every time we have to use one. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font size="2"&gt;Portability of the DataContext should be such that we do not need to pass it around as parameters from one tier to another. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font size="2"&gt;"Unit of work" pattern should be well represented and "very" transparent. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font size="2"&gt;Context should not be persisted without intent, or be open to manipulation from other methods, such that "unit of work" is compromised or a transaction is ill-represented &lt;/font&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt;These seem like simple goals given the features and flexibility of LINQ, however, in reality this has become much more difficult and annoying to achieve than we previously thought. Here are several ideas we have been kicking around, and problem it presents. &lt;strong&gt;I would like to hear from people who have effectively used LINQ Datacontext in these scenarios or in a pattern that's most effective. &lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;1. Creating and disposing LINQ as required.&lt;/strong&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;font face="Arial"&gt;&lt;span style="COLOR: #2b91af"&gt;DbDataContext&lt;/span&gt; myContext = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DbDataContext&lt;/span&gt;(); &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: green"&gt;&lt;font face="Arial"&gt;//your code goes here &lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;font face="Arial"&gt;myContext.SubmitChanges(); &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Creating datacontext everytime we need to use is cumbersome and in my opinion just verbose. &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;
    &lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;If you have to use datacontext in any of the method this code calls, you'll have to pass the datacontext with it. This is one- too many parameters to pass around &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
    &lt;p&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;2. Creating a static DataContext and using it throughout the application.&lt;/strong&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;DataLayer.DataContext.Current&lt;/span&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Creating static object isn't always a good thing. In this case, the context is available for manipulation at any level. And unit of work quickly becomes very hard to maintain &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;If manipulated by other threads or methods context isn't clean anymore and cannot be trusted&lt;br /&gt;
    &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;3. Ambient DataContext (Currently using this)&lt;/strong&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;The idea behind ambient datacontext is that context is created for a particular thread or a httpcontext(in asp.net) as soon as there is a first call for the DataContext. Then the same context is used for that thread or request unless it is disposed manually. This is done by storing the context in the request/thread data store. The approach to this pattern is similar to static DataContext, however, separation is provided for each thread and requests. This works really well in Asp.Net, however, is again plagued by some of the problems of static context.&lt;strong&gt; &lt;/strong&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;The context is available for manipulation at any level. And quickly becomes very hard to maintain unit of work &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Portability across thread is very hard &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;Unit of work pattern is not transparent enough&lt;br /&gt;
    &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;4.  Finally, the last idea we've been trying to come up with is concept similar to TransactionScope.&lt;/strong&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;In this pattern, you would used &lt;strong&gt;using&lt;/strong&gt;{ } block to initialize your context and represent a "Datacontext Scope". Any method below that will automatically be able to figure out the proper context, through thread/request datastore. Then just before the dispose happens, you manually call the &lt;strong&gt;SubmitChanges&lt;/strong&gt;() method. This is the one I like so far, however, has few practical problems. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;&lt;strong&gt;Problems with this pattern:&lt;/strong&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 72pt"&gt;
    &lt;li&gt;&lt;span style="FONT-SIZE: 12pt"&gt;&lt;font size="2"&gt;If sub method needs to implement a new context, we need to figure out how to exclude it from the parent context's using block. If allowing multiple contexts in multiple using blocks, how do you determine which context to use in sub-sub methods? &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;Please do comment on what you think is the best approach to solving this problem.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://dotnetlog.com/aggbug/53.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx</guid>
            <pubDate>Wed, 19 Mar 2008 01:51:33 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/53.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2008/03/18/best-practice-and-effective-way-of-using-datacontext-in-linq.aspx#feedback</comments>
            <slash:comments>14</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/53.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/53.aspx</trackback:ping>
        </item>
        <item>
            <title>Useful Utility functions in .Net a.k.a ‘Hidden Treasures’</title>
            <link>http://dotnetlog.com/archive/2007/10/17/useful-utility-functions-in-.net-a.k.a-hidden-treasures.aspx</link>
            <description>&lt;p&gt;It's no secret that Microsoft's is releasing the source for some of its core .Net classes in upcoming visual studio 2008. Before that arrives, the option to look at some of core classes is probably the .Net Reflector. Every now and then, when I want to find out how Microsoft implemented some of the functionalities in core classes, or if I want to learn better way to program, I use the reflector to dig through the core components. During these adventures, I have bumped into various classes and functions, that I thought were just too cool, and also unfortunate that most were internal only. One of my favorites is the VirtualPath class. However, during this quest I have also found that in some way or other the framework team did expose some great utility functions. Most developers aren't aware of this, including myself and make the mistake of rewriting it. Thus, here's a shot at exposing some of the cool Utility Classes and Functions (I have been using) that every .Net developer should know. (I hope to keep adding to this list, if anyone has other suggestions please feel free to add.) &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div&gt;
&lt;table style="BORDER-COLLAPSE: collapse" border="0"&gt;
    &lt;colgroup&gt;&lt;col style="WIDTH: 298px" /&gt;&lt;col style="WIDTH: 341px" /&gt;&lt;/colgroup&gt;
    &lt;tbody valign="top"&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: #4f81bd 1pt solid; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-BOTTOM: #4f81bd 2.25pt solid"&gt;
            &lt;p&gt;&lt;span style="FONT-SIZE: 13pt; COLOR: #17365d"&gt;&lt;strong&gt;ASP.NET&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: #4f81bd 1pt solid; PADDING-LEFT: 7px; BORDER-BOTTOM: #4f81bd 2.25pt solid; BORDER-LEFT-STYLE: none"&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="BACKGROUND: #d3dfee"&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;strong&gt;System.Web.HttpUtility&lt;/strong&gt;&lt;br /&gt;
            &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.httpserverutility.aspx"&gt;&lt;strong&gt;System.Web.HttpServerUtility&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Provides helper methods for processing web requests like HTMLEncode, UrlEncode, HTMLDecode&lt;br /&gt;
            Accessible through 'Server' object in asp.net&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.virtualpathutility.aspx"&gt;&lt;strong&gt;System.Web.VirtualPathUtility&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Provides utility methods for common virtual path operations.&lt;br /&gt;
            Members: GetDirectory(), GetExtension()&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="BACKGROUND: #d3dfee"&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;strong&gt;System.Web.Compilation.BuildManager&lt;br /&gt;
            .CreateInstanceFromVirtualPath()&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;One of my favorites, allows you to create instance of pages/controls just by passing a virtual path.&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;strong&gt;System.Web.Security.UrlAuthorizationModule&lt;br /&gt;
            .CheckUrlAccessForPrincipal()&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Another great utility in asp.net that allows you to check permission on the file even before you use it. Usage? ex: loading sitemap based on the permission set on each file rather than specifying roles in the node.&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="BACKGROUND: #d3dfee"&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;strong&gt;System.Web.Hosting.HostingEnvironment&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Various methods/properties related to hostingenvironment&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.virtualpathprovider.aspx"&gt;&lt;strong&gt;System.Web.Hosting.HostingEnvironment&lt;br /&gt;
            .VirtualPathProvider&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Retrieves virtual path provider for the application&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt; &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="BACKGROUND: #d3dfee"&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;span style="FONT-SIZE: 13pt; COLOR: #17365d"&gt;&lt;strong&gt;.NET&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.io.path.aspx"&gt;&lt;strong&gt;System.IO.Path&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Provides helper methods for file/directory strings &lt;/p&gt;
            &lt;p&gt;Members: ChangeExtension(), GetFileName, Combine…&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="BACKGROUND: #d3dfee"&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;strong&gt;Int? DateTime?&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;These are nullable classes of primitive types.&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
            &lt;p&gt;&lt;strong&gt;Int.TryParse&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; PADDING-LEFT: 7px; BORDER-TOP-STYLE: none; BORDER-BOTTOM: #4f81bd 1pt solid; BORDER-LEFT-STYLE: none"&gt;
            &lt;p&gt;Equivalent to IsNumeric() function in VB.&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE: There are tons of other classes &amp;amp; static functions in .NET, it's a best practice to use those. If you can't find one exposed but is internal/private, I would suggest creating one similar to it. &lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://dotnetlog.com/aggbug/46.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2007/10/17/useful-utility-functions-in-.net-a.k.a-hidden-treasures.aspx</guid>
            <pubDate>Wed, 17 Oct 2007 23:03:21 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/46.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2007/10/17/useful-utility-functions-in-.net-a.k.a-hidden-treasures.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/46.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/46.aspx</trackback:ping>
        </item>
        <item>
            <title>Using MailDefinition object to send templated emails in ASP.Net 2.0</title>
            <link>http://dotnetlog.com/archive/2006/07/25/using-maildefinition-object-to-send-templated-emails-in-asp.net-2.0.aspx</link>
            <description>&lt;p class="MsoNormal"&gt;Sending emails using Asp.Net has never been as easier and manageable than with introduction of &lt;strong&gt;System.Net.Mail &lt;/strong&gt;in ASP.Net 2.0. Though it always has been relatively easy to create a MailMessage and send it through SMTP, I have seen myself and lots of other developers struggle with decisions like if one should create the body for the message in code, put it in config file/database/ flat file. Depending on the project requirement and developer’s intuition, text could go anywhere. And time required to put something together were comparable. &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;One of my favorite choices is putting the template in a text file. Later, merging it with custom fields during runtime.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;br /&gt;
Ex: Template.txt&lt;/strong&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;font color="#0000ff"&gt;This code was generated at &amp;lt;%DATETIME%&amp;gt; from &amp;lt;%WEBSITE%&amp;gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;Ex: Code Substitution&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;font color="#0000ff"&gt;This code was generated at 12:45:00 PM from DOTNETLOG.com&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;This method decouples the message from code and is easier to manager, however, in .Net 1.1 this meant writing code to open file and also writing the substitution method. In 2.0, &lt;strong&gt;System.Web.UI.WebControls.MailDefinition&lt;/strong&gt; object makes life a lot easier. Below is an example where I use MailDefinition and ListDictionary substitution collection to dynamically generate the mail message. This gives me flexibility, if I want to change the template later on.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;
&lt;o:p&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;p&gt;using&lt;/p&gt;
&lt;/font&gt;&lt;font size="2"&gt; System.Web.UI.WebControls;&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;font size="2"&gt; System.Mail.Net;&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;font size="2"&gt; System.Collections.Specialized;&lt;/font&gt;&lt;/o:p&gt;&lt;font size="2"&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt;&lt;font color="#008000" size="2"&gt;//----------------------------------------------&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt;&lt;font color="#008000" size="2"&gt;//Create MailDefinition &lt;br /&gt;
&lt;/font&gt;&lt;font color="#008080" size="2"&gt;MailDefinition&lt;/font&gt;&lt;font size="2"&gt; md = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#008080" size="2"&gt;MailDefinition&lt;/font&gt;&lt;font size="2"&gt;();&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#008000" size="2"&gt;//specify the location of template&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;md.BodyFileName = &lt;/font&gt;&lt;font color="#800000" size="2"&gt;@"c:\template.txt"&lt;/font&gt;&lt;font size="2"&gt;;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;//Build replacement collection to replace fields in template&lt;br /&gt;
&lt;/font&gt;&lt;font color="#008080" size="2"&gt;ListDictionary&lt;/font&gt;&lt;font size="2"&gt; replacements = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; System.Collections.Specialized.&lt;/font&gt;&lt;font color="#008080" size="2"&gt;ListDictionary&lt;/font&gt;&lt;font size="2"&gt;();&lt;br /&gt;
replacements.Add(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;"&amp;lt;%DATETIME%&amp;gt;"&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#008080" size="2"&gt;DateTime&lt;/font&gt;&lt;font size="2"&gt;.Now.ToShortDateString());&lt;br /&gt;
replacements.Add(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;"&amp;lt;%WEBSITE%&amp;gt;"&lt;/font&gt;&lt;font size="2"&gt;, &lt;/font&gt;&lt;font color="#800000" size="2"&gt;"DOTNETLOG.com"&lt;/font&gt;&lt;font size="2"&gt;);&lt;/font&gt;&lt;font color="#008000" size="2"&gt;//now create mail message using the mail definition object&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt;&lt;font color="#008000" size="2"&gt;// the CreateMailMessage object takes a source control object as the last parameter, if the object you are working with is webcontrol then you can just pass "this", otherwise create a dummy control as below.&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;MailMessage msg = md.CreateMailMessage(emailTo, replacements, &lt;/p&gt;
&lt;p&gt;msg.Subject = &lt;/p&gt;
&lt;p&gt;msg.From = &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; System.Web.UI.WebControls.&lt;/font&gt;&lt;font color="#008080" size="2"&gt;Panel&lt;/font&gt;&lt;font size="2"&gt;());&lt;/font&gt;&lt;font color="#800000" size="2"&gt;"Test Message"&lt;/font&gt;&lt;font size="2"&gt;;&lt;br /&gt;
msg.To = &lt;/font&gt;&lt;font color="#800000" size="2"&gt;"test@dotnetlog.com"&lt;/font&gt;&lt;font size="2"&gt;;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;"admin@dotnetlog.com"&lt;/font&gt;&lt;font size="2"&gt;;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;//this uses SmtpClient in 2.0 to send email, this can be configured in web.config file. See example&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;SmtpClient smtp = &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;Ex: Web.Config&lt;/strong&gt; &lt;/p&gt;
&lt;font color="#0000ff" size="2"&gt;
&lt;p&gt;&amp;lt;&lt;/p&gt;
&lt;/font&gt;&lt;font color="#800000" size="2"&gt;system.net&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;br /&gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;mailSettings&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;br /&gt;
      &amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;smtp&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;deliveryMethod&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; Network&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;from&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;admin@dotnetlog.com&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;br /&gt;
         &amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;network&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;host&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; mail.dotnetlog.com&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;port&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;= &lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;25&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;userName&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;test&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;password&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;= &lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;test&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;br /&gt;
      &amp;lt;/&lt;/font&gt;&lt;font color="#800000" size="2"&gt;smtp&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;br /&gt;
   &amp;lt;/&lt;/font&gt;&lt;font color="#800000" size="2"&gt;mailSettings&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;br /&gt;
&amp;lt;/&lt;/font&gt;&lt;font color="#800000" size="2"&gt;system.net&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; SmtpClient();&lt;br /&gt;
smtp.Send(msg);&lt;/font&gt;&lt;img src="http://dotnetlog.com/aggbug/47.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Bigyan Rajbhandari</dc:creator>
            <guid>http://dotnetlog.com/archive/2006/07/25/using-maildefinition-object-to-send-templated-emails-in-asp.net-2.0.aspx</guid>
            <pubDate>Wed, 26 Jul 2006 03:26:00 GMT</pubDate>
            <wfw:comment>http://dotnetlog.com/comments/47.aspx</wfw:comment>
            <comments>http://dotnetlog.com/archive/2006/07/25/using-maildefinition-object-to-send-templated-emails-in-asp.net-2.0.aspx#feedback</comments>
            <slash:comments>10</slash:comments>
            <wfw:commentRss>http://dotnetlog.com/comments/commentRss/47.aspx</wfw:commentRss>
            <trackback:ping>http://dotnetlog.com/services/trackbacks/47.aspx</trackback:ping>
        </item>
    </channel>
</rss>