Linq performance articles

by ChrisB July 17, 2007 11:07
There's a nice series of LINQ performance articles from Rico Mariani.  I haven't run my own speed tests yet, but he claims that they've increased the speed markedly from Beta 1 too Beta 2.Beta 1 was running select queries at about 1/8 the speed of a DataReader (LINQ uses datareaders under the covers), but they have made a 3.8x improvement under the covers with Beta 2.He discusses compiled queries where you can define the query, and execute it later with parameters (sounds a bit like a stored procedure!)  This means that LINQ doesn't need to generate the SQL each time the query is called, thus saving time on each execution.  By using compiled queries you can achieve a 6.6x improvement over beta 1, or a speed of around 93% that of a raw datareader.The series of articles is herePart 1, Part 2, Part 3, Part 4, Part 5.(I've posted this mainly as a bookmark for myself).[tags]LINQ, Orcas, .net 3.5, database[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Archive

Screen corruption in VS2005 dataset designer running in VmWare.

by ChrisB July 17, 2007 10:07
I've recently started to experience a screen corruption issue in Visual Studio 2005 when running in on XP in VMware.   It manifests itself by not drawing the dataset designer tables when a dataset is opened - the screen instead displays the last image shown (e.g. a dialog box or the last screen of code).  If you click around, the individual tables will appear eventually as you click on them, but it's more or less guesswork.VMware vs2005 screen corruption 1This example shows that the datatable is displaying over the last page shown, essentially, the background is not refreshing, and the datatables only appear when you manage to click on them.SolutionThe solution is to disable hardware acceleration  in windows.   R/Click desktop > Properties > Settings > Advanced > Troubleshoot), and change the Hardware acceleration to the third step.Change hardware accelerationThis fixes the display.No corruptionAs it has only started recently, and no software has been installed apart from windows updates, , I can only think that it was a windows update / driver update from windows update that changed things.    Anyway, it's working now, and hopefully this will be useful to someone.[tags]visual studio, vmware, xp, display corruption[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Archive

LINQ overview and new features in beta 2

by ChrisB July 12, 2007 11:07
One of the most exciting things I've been working with recently has been LINQ to SQL, which is available in VS2008 (orcas) .net 3.5 beta. Summary LINQ to SQL is Microsoft's sql mapping framework, created by Anders Hejlsberg (the lead architect of C#, and if my memory serves me correctly, Borland Delphi, both great technologies). The idea of LINQ to SQL is that you can effectively query in memory and database mapped objects, with deferred loading available. You can provide a series of classes, and through either attributes or an xml mapping file, map these classes to your database. Some new syntax Before we start talking about the database side of things lets have a look at some syntax. Suppose you have a List<Order> where the list of orders is populated somehow, and you wanted a new list of orders where order.IsOpen == true. The .net 2 way of doing it would be:

List myOpenOrders = new List();
foreach (Order o in MyOrderList)
{
  if (o.IsOpen == true)
  {
    myOpenOrders.Add(o);
  }
}
or the more concise

List myOpenOrders = orders.Find(delegate (Order o)
           { return o.IsActive == true});
Anyway, LINQ provides an SQL Query like method to query a list.

var myOpenOrders = from order in orders
        where order.IsOpen == true
        select order
var is a new typesafe inferred type, checked at compile time. So in this instance, myOpenOrders represents an IEnumerable<Order>, but you could just as easily declare
var myInt  =5;
But why not just declare an int, or an IEnumerable<Order> I hear you ask! - because using another new feature of .net 3.5, you can also create types on the fly:

var MyNewType  =  new {Name = "Chris", Age="32" };
MyNewType.Name = "Chris Buckett";
This is useful if we consider the myOpenOrders query again:

var myOpenOrders = from order in orders
        where order.IsOpen == true
        select new {
            OrderNum = order.OrderNumber,
            OrderDate = order.OrderDate
        }
This creates an IEnumerable of our new type, which you could access like:
myOpenOrders[0].OrderNum
Or more likely, databind the result to some output. Finally, we can also use the new "Lambda" syntax to return just one item.
Order myOrder = orders.Single(o => o.OrderNumber == 123);
Single is an Extension Method - another new feature whereby you can add functionality to a class without inheriting from it. I'll probably blog about that at some other point. Suffice to say that good old intellisense gives you a nice list of provided extension methods when you type order. The Lambda syntax (o=>o.OrderNumber ...) in the context of single is more or less a shorthand for the following:

foreach (Order o in orders)
{
    if (o.OrderNumber == 123) { return o; }
}
Except that the Single method will generate an exception if nothing is found. If you'd like to return null, rather than generate an exception, then use SingleOrDefault(). Back to LINQ to SQL So, we can see we can query a list of objects - this is where linq to SQL comes in. By defining a DataContext (either using the drag and drop visual designer, or creating your classes and mapping them to the database), and then using that datacontext to retrieve your list of objects, you can easily query the database to retrieve the objects you require. The datacontext also has useful caching, so only the objects that aren't already in the cache are pulled from the database. For example:

MyDataContext dc = new MyDataContext(myConnectionString);
var myOpenOrders = from order in dc.Orders
        order.IsOpen == true
        select order;
And to select a single record:
Order order = dc.Orders.Single(order => order.OrderNumber = 123)
And finally, we can modify that order and save the changes back to the database with the following:

order.IsOpen = false;
dc.SubmitChanges();
One other useful thing that LINQ to SQL provides is the ability to access child objects, for example:

var myExpensiveOrderItems = from orderItem in order.OrderItems
             orderItem.Value > 500
             select orderItem;
Or

OrderItem orderItem = order.OrderItem.Single(oi=>oi.ItemCode == "ABC123");
And you can reference the order from that order item using
orderItem.Order
And so on. There's a load more, and I would recommend either Scott Gu's descriptions, or Mike Taulty's videos to give you some better insight. Getting back to the point. So, the thing that excites me today is the promised functionality in the upcoming Beta2, specifically Entity Object Validation. By creating a partial class for the entity type (say Order, which has been auto-created by the drag and drop designer, for example), you can declare the following (and yes, this is very similar to Scott Gu's example, as it is what I've just read:

public partial class Order 
{
  partial void OnValidate() 
  {
    if (Order.OrderDate < DateTime.Today) 
    {
      throw new Exception("Order date is before today");
    }
  }
}
When you call SubmitChanges , OnValidate is called. If the exception is thrown, everything is rolled back (because all LINQ queries are executed within their own transactions). Note another new piece of syntax: partial methods. This allows you to develop a class with a method stub, e.g.

public partial class MyClass 
{
  partial void DoSomething(string someValue);

  public string MyPrivateProperty
  {
    get
      {
        return _something;
      }
    set
      {
        _something = value;
        DoSomething(value);
      }
    }
  }
}
and then in another file, you can use:

public partial class MyClass 
{
    partial void DoSomething(string someValue)
    {
      //Some code here
    }
}
The main benefit I can see of this is that you can easily use generated code, such as that from the LINQ visual designer, and then extend that code in separate files buy using partial classes and partial methods. That way, if you regenerate the generated code, all you hard work doesn't get wiped out by an over-eager code generator. Further reading: Linq tutorials from Scott Gu More talk about partial methods There is also quite a good piece on phoen blog about whether technologies such as LINQ will affect database best practices - essentially making that point that with LINQ technologies, developers steered towards using the database simply as a "bit bucket" or object repository. One final useful link for those TDD inclined is Ian Coopers post with a useful example of testing your LINQ classes without accessing the database. [tags]LINQ, Orcas, vs2008, .net 3.5, Database[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Load DataSet Error in Visual Studio Designer - solution.

by ChrisB July 11, 2007 13:07
I've just revisited an old project to add a TableAdapter to a Dataset, and received this error when trying to load the VS2005 designer.
Failed to load dataset because of the following error:
Unable to find connection 'MyDatabaseConnectionString (Settings)1' for object 'Settings'. The connection string could not be found in application settings, or the data provider associated with the connection string could be loaded.
The project builds and runs without error, but there was obviously something wrong.  Looking through the settings and app.config reveals no errors. It turns out that the .XSD dataset file (which is represented visually) has a duplicate connection string.  You need to edit it in notepad and remove the extra line, from:
<DataSource ....> <Connection AppSettingsPropertyName="MyDatasetConnectionString"  ..... /> <Connection AppSettingsPropertyName="MyDatasetConnectionString"  ...../> .... etc...
to:
<DataSource ....> <Connection AppSettingsPropertyName="MyDatasetConnectionString"  ..... /> .... etc...
Easy. It's one of those obvious ones with a cryptic error message. [tags]Visual Studio, Dataset, Error[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

MsBuild and CruiseControl configuration examples

by ChrisB July 09, 2007 15:07
I've been asked for some time by readers to put up examples of my MsBuild and CruiseControl configuration files. So here goes, with some comments and explanation. MSBuild I use MsBuild as my build script - NAnt is another one, but I use Visual Studio, so MsBuild is already installed. First I add an XML file as a solution item to my solution (Right click the solution, click New Item , and add an XML file - e.g. MyProject.msbuild Here is the example solution that I am using: MySolution.sln and the msbuild file performs the following functions:
  1. It can clean and rebuild the libraries and websites for Debug or Release configuration
    • Note, to use this feature, you must have the website's Output Folder set in the MSBuild options (right click the website and click properties.
  2. It can run a number of unit tests, code coverage and FxCop tests.
  3. It can create a zip file containing the pre-compiled website.
MSBuildfile. Download MyProject.msbuild To test that it works, you can run the Visual Studio Command Prompt, change to the solution's directory, and use the following command:
msbuild MyProject.msbuild /t:Build_Debug
You can change Build_Debug for any other targets that you require. For example, if you were going to create deploy the website as a zip file, you would use /t:Deploy_Release - and the generated zip file would contain the latest precompiled output of the website. You should run the /t:TestMyProject to make sure that works, as that is what CruiseControl.net will be referencing. CruiseControl On the cruise control server, edit the ccnet.config file. Using the example below, Cruise control monitors a Subversion Source repository and when it detects a change, it checks-out the source tree, runs the MsBuild task, gathers the results of the unit tests, code coverage and FxCop output into a report, and saves the results. The results are viewable on your CruiseControl servers project web page, and I recommend using CCtray to run a tray icon to monitor the server - you can use it to make annoying noises when the project tests fail. CruiseControl Config File Download CruiseControl ccnet.config That is one project in the ccnet.config file. You can have as many projects as you like. Assuming your msbuild file works on your local machine, this should work fine. [tags]MsBuild, CruiseControl, Visual Stuido, NUnit[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Don&#8217;t make me think (2nd ed) - Book review

by ChrisB July 05, 2007 13:07
Don’t Make Me Think I've recently finished reading Don't Make Me Think by Steve Krug, and it's one of those books that makes you reflect on your work prior reading it. It's very short, in fact, it's so short that you have to wonder whether it contains enough useful information, bearing in mind that the usual developer tome runs to over an inch thick. Anyway, the book is pretty much a physical embodiment of it's contents , namely: Use common sense, get to the point, do a job and do it well, without fuss and frippery. Steve Krug provides useful links throughout the book, including links to 3 chapters he removed to keep the book short when he added new content. There's a good section on Accesibility (an accessible site is easy to use for all, and needn't be a compromise), and various other chapters on topics like how to identify when you are going round and round in a developer - designer loop ("Shall we use drop down boxes?" "People like drop down boxes." "Drop down boxes will be too hard to implement," etc...) - the essential message being that it's important to just make a decision, and then get an external person (or people) to use it. This reminds me of Joel Spolesky's "Hallway Usability" testing. The section on how to perform cheap testing is very concise but packed full of useful tips and handy hints. Basically, it involves the development team watching (remotely) how a person uses the site to perform a task. This could be done remotely using VNC or similar, although a cheap video camera would be just as good. To sum up, it's a great book to buy, read in a couple of hours, and then re-read before you start any project with a user interface.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

metaSapiens PageMethods support in Orcas patch

by ChrisB July 04, 2007 12:07
Further to my previous post and some experimenting, I've created a patch that you can dowload from the patches page on the PageMethods page on Codeplex. The patch is for the source files, but you can simply run the two .reg files after installing the standard setup. This will provide PageMethods support in Orcas. [tags]pagemethods, orcas, patch[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

metaSapiens PageMethods in vs &#8220;orcas&#8221;

by ChrisB July 02, 2007 15:07
Edit: This has now been superseded by this post. I've been using the rather good PageMethods from metaSapiens (now open source) which gives you strong typing and intellisense when calling one asp.net page from another.
e.g. Response.Redirect(MyPageMethods.Default.LoadCustomer(customerId));
where Default.aspx is some page that expects a CustomerID as a parameter. Anyway, there is a work item open on codeplex for Orcas support to be added to the installer, but here's how to do it in the mean time:
  1. Install the VS2005 version of pageMethods (available here). (If you have UAC enabled then you will need to run the SETUP.EXE as administrator. Also, when you edit the following files, if you have UAC enabled, it might be worth copying the files to another location (eg: the desktop), edit them, then copy them back.
  2. Browse to c:\Program Files\MetaSapiens\Page Methods for VS2005, and edit (using notepad) PageMethodsAddinVS2005.reg and PageMethodsGeneratorVS2005.reg. You need to change all the registry key entries from 8.0 to 9.0. e.g. [HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio \8.0\AddIns\PageMethods.VSAddin] becomes [HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio \9.0\AddIns\PageMethods.VSAddin] There's 1 in the first file, and 5 in the second file. Save them, and run them (ie, Double Click them, and they will merge into the registry.
  3. Finally, go into the Addin folder, and edit (using notepad) the PageMethods.Addin file. There are two <Version>8.0</Version> occurrences - change these to <Version>9.0</Version>. Save it.
  4. Run Orcas, go to Tools - Addin Manager and confirm that PageMethods Addin is installed (follow the quickstart tutorial from here on)
That's it. There is one caveat, though. I am using VS Orcas within a virtual machine, so I don't have to worry about it also working in VS2005. I think that the above may cause a "package load" error in VS2005 (as VS2005 is version 8, but we have edited the Addin file to be version 9). I haven't tested it to see if it still works with VS2005. I'm sure you could write a quick batch file to switch between two versions of the .Addin file if you needed to! [tags]PageMethods, Visual Studio, Orcas, asp.net[/tags]

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen | Modified by Mooglegiant

About the author

Chris Buckett develops .net applications in C# and VB.Net, and is being persuaded to start developing for Java.

Tag cloud

Page List