Joe Feser

My experiences with Linq, .NET and Testing

About the author

Author Name is Joe Feser.
E-mail me Send mail

Recent posts

Recent comments

Don't show

Authors

Hook me up

Technorati Profile

Add to Technorati Favorites

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008 Joe Feser

Linq To NCover Part 2

I can’t begin to explain how excited I get when working with Linq. Until now, there were times that you needed to write a large amount of code to do the same basic operations like determining a child object had certain properties set, or determine the count of how many times a property is set to true.

 

You would have to declare a bunch of variables like “bool found;” and then you would perform a foreach loop on your objects, checking for certain conditions. Since you did the same thing over and over, you would go and find a code block that was close and modify it to fit your needs. Now there is a much more precise, easy to read syntax to perform the basic crud tasks, and they call it Linq.

 

With the power of Linq, you no longer have to write long winded code to walk all the items in a collection only to realize two or three of the twenty items are valid. You can now filter or select the objects that you would like to work with, with straight forward query syntax.

 

NCover stores the coverage file in an xml document. In that document is all the information needed to determine what classes are in a namespace, what methods are contained in a class and so forth. You can think of these as relationships or “joins”. Once this information is loaded into memory, you could perform a foreach loop against every single class in every single namespace until you found a match or you could query the data for exactly what you are looking for.

 

A typical query against an NCover coverage file could look like this:

CodeCoverage
c = CodeCoverage.Load(file);

var
retVal =from mod in c.Modules
            from cls in mod.Classes
            from method in cls.Methods

            where method.Name == ".ctor"
            select method;

In the above example each collection is aliased with a name, which can be used in the where clause with full Intellisense. You could easily add an additional where statements to only find .ctors (constructors) in a class that start with “GCheckout”. This syntax would look like this:

CodeCoverage
c = CodeCoverage.Load(file);

var
retVal =from mod in c.Modules
            from cls in mod.Classes
            from method in cls.Methods
            //only list classes in the GCheckout Namespace
            where cls.NameSpace == "GCheckout"
            where cls.Name.StartsWith("Request")
            where method.Name == ".ctor"
            select method;

You may notice that there are multiple instances where clauses in the Linq query. You could have just as easily replaced the second where keyword with && (in c#). I personally feel it is easier to read the intention of the statement by spelling out where on every “filter”.

 

My thought was the majority of people were going to want to perform queries against the classes and methods. I rolled up all the classes and methods on the CodeCoverage class in order to remove the need to reference the relationships between the modules and the classes and the methods.


CodeCoverage c = CodeCoverage.Load(file);

var retVal =from method in c.Methods
            from param in method.Parameters
            where param.FullType == typeof(decimal).ToString()
            orderby method.Name
            select method;

Download the sample code and tell me what you think.

LinqToNCover.zip (94.82 kb)

The next article will talk about how a Linq query was used to calculate the return value of a property.

kick it on DotNetKicks.com

Currently rated 5.0 by 2 people

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

Tags: , ,
Categories: .NET | Linq | NCover
Posted by joe.feser on Wednesday, February 27, 2008 6:51 AM
Permalink | Comments (6) | Post RSSRSS comment feed

Related posts

Comments

blogs.msdn.com

Tuesday, March 18, 2008 5:38 PM

pingback

Pingback from blogs.msdn.com

Charlie Calvert's Community Blog : Link to Everything: A List of LINQ Providers

dotnet.org.za

Wednesday, March 19, 2008 3:06 AM

pingback

Pingback from dotnet.org.za

LINQ To ... - Hilton Giesenow's Jumbled Mind

jacquessnyman.co.za

Wednesday, March 19, 2008 6:02 AM

pingback

Pingback from jacquessnyman.co.za

Jacques Snyman » LINQ To …

blogs.msdn.com

Wednesday, March 19, 2008 5:11 PM

pingback

Pingback from blogs.msdn.com

Charlie Calvert's Community Blog : Links to LINQ

blog.windows2.webhome.at

Thursday, April 10, 2008 6:34 AM

pingback

Pingback from blog.windows2.webhome.at

LINQ to [AnyWhere]

manicprogrammer.com

Wednesday, May 07, 2008 9:31 PM

pingback

Pingback from manicprogrammer.com

DeusExMachina : Linq to NCover

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Friday, August 08, 2008 5:29 PM