May 2009 - Posts
.Net Framework 4.0 provides us with a new class called Lazy<T>. As documentation sais then Lazy<T> provides support for several common patterns of lazy initialization, including the ability to initialize value types and to use null values. So it is construct that helps us implement lazy loading.
I wrote a little code example on Visual Studio 2010 that illustrates how to use Lazy<T>.
C#
static void Main(string[] args)
{
var lazyString = new Lazy<string>(
() =>
{
// Here you can do some complex processing
// and then return a value.
Console.Write("Inside lazy loader");
return "Lazy loading!";
});
Console.Write("Is value created: ");
Console.WriteLine(lazyString.IsValueCreated);
Console.Write("Value: ");
Console.WriteLine(lazyString.Value);
Console.Write("Value again: ");
Console.WriteLine(lazyString.Value);
Console.Write("Is value created: ");
Console.WriteLine(lazyString.IsValueCreated);
Console.WriteLine("Press any key to continue ...");
Console.ReadLine();
}
VB.NET
Private Shared Sub Main(ByVal args As String())
Dim lazyString = New Lazy(Of String)(Function() Do
' Here you can do some complex processing
' and then return a value.
Console.Write("Inside lazy loader")
Return "Lazy loading!"
End Function)
Console.Write("Is value created: ")
Console.WriteLine(lazyString.IsValueCreated)
Console.Write("Value: ")
Console.WriteLine(lazyString.Value)
Console.Write("Value again: ")
Console.WriteLine(lazyString.Value)
Console.Write("Is value created: ")
Console.WriteLine(lazyString.IsValueCreated)
Console.WriteLine("Press any key to continue ...")
Console.ReadLine()
End Sub
When we run this code we will get the following output.
Is value created: False
Inside lazy loader
Value: Lazy loading!
Value again: Lazy loading!
Is value created: True
Press any key to continue …
The value of our Lazy<string> will be initialized when we first ask it and then it will be stored for subsequent calls. Notice that there is one Console.WriteLine inside lazy initialization function and if you look at output you can see that this function is run only once. So only thing you have to do is to write initialization function and Lazy<T> makes all the work automatically.
I found also one example that may give you better explanations about internals of Lazy<T>: Lazy Computation in C#.
Guys from Typemock are giving away some free licenses. The text below is taken from their web site. I hope I get some licenses too. :)
Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.
Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers.
The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.
Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET,MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.
The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement.
Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.
Go ahead, click the following link for more information on how to get your free license.
NB! This blog is moved to gunnarpeipman.com
SharePoint
Entity Framework
LINQ To SQL
Azure
Other software and .Net topics
Yesterday I wrote about Repeat extension method for strings. Today I offer you shorter and faster version of it.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
One quick posting. I wrote yesterday extension method for SharePoint that checks if view exists. I think it is far better for performance if I don’t live on try…catch to find out if view exists or not. Extension method that you have to put in static class is here.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
I needed str_repeat() functionality in one of my C# methods. As .Net currently doesn’t offer it I wrote my own Repeat() extension method for strings. Here you can find C# and VB.NET versions. Make sure you put the method inside static class.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
I wrote web part that uses Repeater control to create some repeating blocks of output. Each of these blocks has Button control in it. When I ran web part under SharePoint I got the following error: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. Here is the solution.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
A little mystery when updating publishing pages: page is checked out, some attributes are changed and now I want to update this page. All I get is error that even Google has never seen before: System.Runtime.InteropServices.COMException (0x81020069). As I got error message in my own language I try to translate it: Attributes of checked out and orphan regime document cannot be edited. To edit attributes you have to use special software for this type of file.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
I you get Access Denied errors in SharePoint then take a look at Event Viewer to see if there are some DCOM 10016 Event Errors in System log: The application-specific permission settings do not grant Local Launch permission for the COM Server application with CLSID {A9E69610-B80D-11D0-B9B9-00A0C922E750} to the user NT AUTHORITY\NETWORK SERVICE SID (S-1-5-20) from address LocalHost (Using LRPC). This security permission can be modified using the Component Services administrative tool. If there is such error then follow the steps below to get this error fixed.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
I tried to put up SharePoint development environment on Windows 7 and guess what – I succeeded. I took smaller challenge and installed WSS 3.0 SP2. Of course, you can also install SharePoint Server instead of WSS. On my machine WSS 3.0 uses SQL Server 2008 Express edition as database. For MOSS 2007 you should have SQL Server 2008 Developer edition.
NB! This blog is moved to gunnarpeipman.com
Click here to go to article
More Posts
« Previous page