Scott Hanselman

RFC: Web Platform Installer (WebPI) with an offline feed mode for bandwidth savings

September 24, '11 Comments [36] Posted in Tools
Sponsored By

My wife is in the Kingdom of Swaziland this evening. If she was a developer over there and wanted to use Web Platform Installer to get Visual Studio or some large download she would have to patiently bring the bits down over 3G. If she was very well off, she might have ASDL with a maximum speed of 512kbps down, most likely with a cap. I've talked about limited connectivity in African countries and other nations (*cough* New Zealand *cough*) before on this blog and it's no fun. I've told the wife in the past that I'm happy to live overseas but I needs me some bandwidth. It sucks when tools you like don't conserve your bandwidth.

Making sure products are available and convenient for customers without a "fat pipe" is a passion of mine and I'm always the guy in the meetings at work to remind people within the Redmond Reality Distortion Field that not everyone in the world has 35 megabit fiber to their house.

We've also been trying to push for transparency and openness in all things (no more secrets, I like to say) and it usually comes down to this: They don't like to announce stuff if they aren't sure they are going to ship it. That's cool, and I get it.

But, when I saw that the folks on the WebPI team have heard you and I and told me they are working on an offline experience for WebPI today I had to share it with you, Dear Reader. Here's the deal, I'm looking at daily builds off the developer's machine. This IS NOT the final experience and this IS NOT anything other than a daily. They are looking at shipping this soon, but they told me to put lots of disclaimers. Disclaim! There it is. As this is a daily build, there are likely more questions than answers, so I'll show you how it works now, with my commentary throughout.

Web Platform Installer Offline

The problem is, of course, that the Web Platform Installer assumes that you and the 50 people at your company sharing internet are cool with downloading the same x gigs 50 times. It also assumes that your system IS connected to the internet in some way. It's a wasteful way for us to get you bits and everyone loses.

I think the ideal use case is that someone who has the bandwidth and the time downloads all the apps they want then puts them on a DVD or a network share and then all the consumers point to that offline cache. Companies could have a share or feed that's refreshed monthly, for example.

Products in WebPI have an "AppID" or Product ID. You'll need to know the AppID for the thing or things you want to install. For example, with the online Web Platform Installer if you want to install "all the web stuff" in one click, you'd use this URL http://www.microsoft.com/web/gallery/install.aspx?appid=VWDorVS2010SP1Pack.

See the "VWDorVS2010SP1Pack" part? That refers to an AppID inside of the Feed that Web Platform Installer consumes. Everything in the feed has an AppID and you can string them together. For example, you can say http://www.microsoft.com/web/gallery/install.aspx?appid=MVC to get ASP.NET MVC 3 for VS2010. You can add lots of AppIDs separated by semicolons ";" to get multiple products and all their dependencies.

I am testing a spike of an offline command line tool called wpiutil.exe but the plan is to merge it with the Web Platform Installer Command Line tool. So, even though I'm showing two tools, pretend there are one.

You can find an AppID inside the Atom Feed that the Web Platform Installer consumes. The main feed is at https://go.microsoft.com/?linkid=9767054 which isn't really discoverable, so you can use the command line tool to find what you want.

I can run WebpiCmdLine.exe /list:all and get a big list of products and applications, then collect the IDs I want to install or cache offline for later.

Current language of installers is English

--Previously Installed Products
ID Title
----------------------------------------
AppFabric Windows Server AppFabric
AppFabricSetup Windows Server AppFabric Setup
*SNIP*
MVC2 ASP.NET MVC 2
MVC3 ASP.NET MVC 3 (Visual Studio 2010)
MVC3Loc ASP.NET MVC 3 Tools Update Language Packs
MVC3Runtime ASP.NET MVC 3 Runtime
*SNIP*
--Applications
ID Title
----------------------------------------
AcquiaDrupal Acquia Drupal
Atlas Atlas
*SNIP*
Umbraco Umbraco CMS
WordPress WordPress

Scott Feedback: I told the team that I thought these AppIDs could be also on this page in the GUI. We could add "Product ID:" to screens like this?

An example Product Details page that could potentially include more data

So now I can do this:

C:\>wpiutil.exe /create-shadow /shadow-path:c:\webpicache /product-ids:MVC3;IISExpress

Loading products in webpi feeds ...

Creating shadow for following products:
MVC3
NETFramework4
WindowsImagingComponent
MVC3Installer
IISExpress

Processing feed: 'https://go.microsoft.com/?linkid=9767054'

Processing resources ...

Processing product entries ...
Processing product entry: 'MVC3Installer'.
Processing product entry: 'MVC3'.
Processing product entry: 'NETFramework4'.
Processing product entry: 'IISExpress'.
Processing product entry: 'WindowsImagingComponent'.

Done.

See how the tool got the the dependencies as well? Here's the folder structure that was created locally:

The folder structure created by WebPI that has all the installers and custom feed

Now I can point the command line tool at my new custom feed. My custom feed contains ONLY what I've downloaded. It's a subset, although I suppose a company COULD make a complete (or at least a large) shadow copy of everything and keep it on a server.

C:\>WebpiCmdLine.exe /xml:"C:\webpicache\feeds\shadowfeeds\shadow-webproductlist.xml" /List:All

Successfully loaded metadata from feed C:\webpicache\feeds\shadowfeeds\shadow-webproductlist.xml

Current language of installers is English

--Available Products
ID Title
----------------------------------------
MVC3Installer ASP.NET MVC 3 Tools Update Installer
IISExpress IIS 7.5 Express
MVC3 ASP.NET MVC 3 (Visual Studio 2010)
NETFramework4 Microsoft .NET Framework 4

Then I can install the products I want like this:

C:\>WebpiCmdLine.exe /xml:"C:\webpicache\feeds\shadowfeeds\shadow-webproductlist.xml" /Products:MVC3

Here's some of the my personal feedback to the team in no order:

  • There should be as much visual feedback as possible from the command line tool, given that it'll be run over slow networks (even 3G) so something like a (345kb of 56 megs) updating text area...
  • A way to switch the Web Platform GUI to be completely offline by pointing it to a local feed.
  • The command line tool should be smart about relative vs. absolute paths, as well as UNC, or even running your company's custom internal Web PI server.
  • Maybe consider using BITS (background internet transfer service) like Windows Update so folks could run it in the background over many days.
  • "Shadow" isn't a good name as it sounds like the Shadow Copying feature in Windows. Just call it offline.
  • More documentation on how customers can make custom feeds for their products. Just as NuGet gets libraries for Visual Studio 2010 projects, WebPI consumes feeds of MSIs and their dependencies.
  • It must be smart about resuming after a dropped connection or call and must retry corrupt downloads.
  • Group policy support so that IT Administrators could control how WebPI works inside a large enterprise and where it loads files from.
  • Should have a smart default path like c:\webpicache or .\webpicache if it's run without a full path.

Now, this is your (and my) opportunity to provide comments and suggestions to the team and affect the final direction of the product or future versions.

From what I've shown you, Dear Reader, my questions to YOU are:

  • Will this be a good feature?
  • Will it help you?
  • Is it useful or useless?
  • Is it important to be a GUI or is a Command Line good too?
  • What has been forgotten?
  • What's a "must have" and what's a "don't need?"

Discuss, they are listening!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

This Developer's Life 2.0.5 - Typo

September 19, '11 Comments [10] Posted in Podcast
Sponsored By

Episodes 2.0.5 - Typo

Who cares about typefaces and why should you? Well, these guys do and you should start caring. Rob and Scott explore the world of reading online with one of the godfathers in the world of typeface and fonts.

In this episode we talk to Bill Hill, the bearded Scotsman who created ClearType and Geoffrey Grosenbach, notable typeface enthusiast.

I'd also like to encourage you to check out the last two episodes of This Developer's Life. We realize this isn't your typical podcast. It's not very technical, and it's not packed with information. We mean this show as an antidote to the usual technical talk shows as we try to dig more into issues and emotions that affect developers. We try to get to the center core of the developer and what makes them tick. Give us a listen and if you like the show, please review us on iTunes and Subscribe.

Download Episode 2.0.5 "Typo" here or listen online.

Also, please check out our last two shows you might have missed. We're very proud of how they turned out.


Episode 2.0.4 - Taste

2.0.4 Taste

What is taste? What is style? Do you have it? Scott and Rob have no idea what it is or how to get it - but they know it's important. In this show they talk to a designer who flexxes his good taste for a living - and a developer who is committed to spreading good taste where he can.

 


Episode 2.0.3 - Education2.0.3 Education

Scott and Rob discuss the value of a degree - and talk to two developers who used their passion to pull them through school and into their careers.

 


I hope you enjoy the show as much as Rob and I enjoy making it.

Again, big thanks to DevExpress. The bandwidth and other costs are picked up DevExpress and CodeRush! Visit them and thank them on Twitter.

This Developer's Life is brought to you by CodeRush for Visual Studio.

This Developer's Life is brought to you by CodeRush for Visual Studio.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

New Tools and New Content - ASP.NET, Visual Studio 11 Web and .NET 4.5 Developer Preview (with commentary)

September 16, '11 Comments [48] Posted in ASP.NET | ASP.NET Ajax | ASP.NET MVC | IIS | Mobile
Sponsored By

ASPNET_vNextWhile all of you Build attendees are making me feel bad because you have a fancy Tablet and I don't (sell me yours!) the folks over here in the "Angle Brackets Team" (I'm trying out some new names. One will stick.) have been busy.

Here is a summary of all the items from our team that have been announced and shown at Build this week, all of this will be publicly available today (9/16) along with a some narrative and asides from yours truly.

They are listening

One of the things I am am personally enjoying in working daily with this build of Visual Studio is that there's dozens (hundreds) of little "mental speed bumps" that are smoothed over. A lot of thought was put into workflow and common scenarios in order to, well, get out of a developer's way.

Another constant source of happiness on my part is the team's realization that not everyone can upgrade their Visual Studio, so you can round trip your projects and solutions. I can use Visual Studio 11 Developer Preview and open a .NET 4 application, save the project, and then open it in Visual Studio 2010 and it'll just work. As it always should have. Teams can mix and match and the beta guy can  be wacky without checking in solution files that will break the other folks.

By the way, have you seen our new org chart?

Our new org chart

Big Aside: Changing how we release software

Scott Aside: It's also worth noting that while there is a bunch of stuff in the next version of Visual Studio, there's an equally compelling amount of stuff being released from the Azure/Web Team on NuGet. This gets to my LEGO analogy where we developers have more choice of what we snap together. Most importantly many things will only live on NuGet. I'm looking at the possibility of  NuGet Feed of Supported Microsoft Products. Folks sometimes assume that NuGet is a place to throw binaries up because it's convenient, but we also need a way to know if a NuGet package is actually a support product, kind of like "Verified" is on Twitter. I'm interested in your thoughts on this in the comments, or even better, in the Discussion Tab on the NuGet project site.

Shipping via NuGet is also significant because it helps level the playing field with Open Source libraries. Microsoft's open source libraries will "compete" for our developer-attention by all being in NuGet, and folks should use the ones that make them happy. If you want to use Microsoft's Ajax Minifier or any of a half dozen others, then just get the one you want from NuGet. Microsoft's won't ship out of the box. It'll be on NuGet.

Visual Studio 11 Developer Preview

A new Solution ExplorerTuesday the "Windows 8 Express" version of Visual Studio was released with the Windows 8 Developer Preview release which supports Windows 8 development. Wednesday during the keynote with Satya Nadella,  Scott Guthrie, Jason Zander  Visual Studio 11 Developer Preview and .NET 4.5 were announced. The bits are available today to the public.

ASP.NET 4.5 Developer Preview includes new core functionality like WebSockets, anti-XSS encoding, granular request validation. It also includes Web Forms improvements like model binding, support for HTML5 and unobtrusive JavaScript. And there have many performance improvements in ASP.NET when combined with Windows 8 Server can decrease startup time by 30% and reduce the memory footprint by 30%.

Scott Aside: I continue to put gentle pressure on all the teams about semantic versioning. In this case, .NET 4.5 is the right version number. It's using the .NET 4 CLR and is an additive (with bug fixes) release to .NET 4. Because it does that, it's very compatible. Your existing .NET 4 stuff won't (shouldn't) break. Check out the Target framework dropdown in this version of Visual Studio. See how you can target from 2.0 to 4.5 with the same IDE? That will be useful for corporate customers who need to develop 2.0 apps but still want the IDE improvements.
All the frameworks from 2 to 4.5 living together in a single dropdown. I never thought I'd see the day.

Visual Studio 11 Developer Preview includes new lots of new functionality including full editor support for CSS 3, full editor support for HTML 5, full editor support for JS, and many niceties like how Web Forms events can be wired up in source view, Web Forms designers can be launched directly from source view, how Live Inspector (code named "Eureka") makes editing the HTML of your document live much easier by automatically finding the HTML in your document as you select it in the Browser and much more.

Scott Aside: They've leveled the playing field across all the editors so things like regions, collapsing hierarchies, commenting, formatting, etc are all the same between CSS, JS, HTML and code. One more reason to just turn off all your foolbars and embrace the text editor and code.

You can read more about all the new features that are enabled in this release by reading the What's new in ASP.NET 4.5 and Visual Studio Web Developer whitepaper here: http://www.asp.net/vnext/whats-new.

NuGet 1.5 w/Dev11 Support Released

We just published an update to NuGet 1.5 that supports Visual Studio 11 Developer Preview. This allows developers on the Preview builds to continue to use NuGet with the preview releases of Visual Studio.

Note: NuGet does not support the "Windows 8 Express" release that was released yesterday. Support was just recently added to that SKU of Visual Studio and will appear in the next public preview release of "Windows 8 Express".

You can download the new release here: http://www.nuget.org/

ASP.NET 4.5

There's a lot of new improvements in ASP.NET and WebForms in particular in this coming release. ScottGu is doing a series and has done these posts already:

Model Binding will be familiar to MVC folks ,as is Routing. Both features now existing in both WebForms and MVC. So much time is spent in WebForms pulling data out of the request, via Forms, or QueryStrings, or Controls. Sometimes it seems like 30% of my WebForms code is just moving data from the Request into a variable. Now I can do this:

public IQueryable<Product> GetProducts(
[QueryString("q")]string keyword,
[Control("categories")]int? categoryId)
{
IQueryable<Product> query = _db.Products;

if (!String.IsNullOrWhiteSpace(keyword))
{
query = query.Where(p => p.ProductName.Contains(keyword));
}

if (categoryId.HasValue && categoryId > 0)
{
query = query.Where(p => p.CategoryID == categoryId);
}

return query;
}

This method supports a GridView that can sort, filter by category, and search via URL. Note the use of a nullable int as well. It makes WebForms code a lot cleaner, and it's just one example.

Scott Aside: ASP.NET 4.5 is additive to ASP.NET 4, so your existing apps will run, of course, but you can just start using these new features once you're using 4.5. I've been able to "refactor via subtraction" and yank out a bunch of tedious monkey code.

One other thing I wanted to say. It's becoming clearer to me that it's less about WebForms or MVC or WCF WebAPI Services or OData or IHttpHandlers or SingalR and more about ASP.NET. I used to call mix-and-match apps "hybrid" applications, but now I'm realizing they are just ASP.NET applications. I'll use whatever LEGO piece(s) I want and you should also.

MVC 4 and Web Pages 2 Developer Preview Released

The new responsive design of the MVC4 HTML5 default templateASP.NET MVC 4 Developer Preview is the latest release of our MVC framework. This release includes built-in support for mobile sites, new, fresh HTML5 project templates as well as jQuery Mobile. It has enhanced support for asynchronous methods, and custom code generation.

Also included are Web Pages 2 (like MVC with the V and C in the same file) a lightweight framework for creating dynamic, data-driven websites. The latest version expands support for mobile devices and for integrating client scripts, and adds helpers for tasks like mapping. You can download the new release for Visual Studio 2010 and Visual Studio 11 here: www.asp.net/mvc/mvc4

Scott Aside: Web Pages is what Rob and I used to make http://thisdeveloperslife.com and I'll be doing http://hanselminutes.com over again in Web Pages in the next week or so. If you love Razor just want a simple site, write code, hit F5, and deploy, WebMatrix and Web Pages is a nice change of pace. Also, the fact that they were able to integrate node.js into WebMatrix is not only cool but it brings up interesting questions about WebMatrix and what is can be used for. You can do PHP on it too, you know. Hm.

WCF 4.5 Developer preview AND "Web API" shipping out of band on .NET 4 and up!

Introduced Wednesday as part of .NET 4.5, includes new core WCF functionality like WebSockets, UDP multicast, improved streaming and better async support leveraging  Tasks and C# async improvements. We continued our commitment to simplicity with a number of configuration improvements (read "reductions"), making WCF throttles and quotas work for developers by default, better manageability. The WCF Client programming model is included in .NET Core profile available to Windows-tailored app developer.

Scott Aside: I've used WCF for years, begrudgingly. It is powerful, no one would disagree, but it's notoriously complex and scary. I'm impressed with what they've done in this release because they've effectively used the infinite configurability of WCF and pluged in a re-imagination of what WCF works like in a world of REST and JSON. When it's time for WS-* and the Enterprise, that part of WCF is still there, but when you move to the RESTful open web of 2011-2012, the new Web API is clean and light, nicely integrated with ASP.NET and able to create simple and lightweight web services using things like JSON and conventions that make sense.

RIA with HTML5/JavaScript

Dinesh announced the developer preview for RIA for JS/HTML5 in his BUILD talk. It's a set of jQuery plugins that let you easily work with server data on the client. By bridging the different tiers, RIA/JS lets you quickly build rich forms-over-data applications with HTML/JQuery. The libraries are available via RIAServices.jQuery NuGet package as well as codeplex. The WCF RIA Services for Silverlight were also updated via WCF RIA Services V1.0 SP2 and WCF RIA Services Toolkit Aug 2011 update.

Scott Aside: RIA was another project that I had checked out very early on and said, meh. I'm zero for two on judging their projects because the WCF team has embraced jQuery, JSON and a more open web in a big way. I'm impressed with their efforts and open attitude. Take a look at their "BigShelf" starter project and check out how they are using jQuery to retrieve and bind, filter and sort data on the client side. Again, fewer black boxes since it's all HTML and JavaScript. Be sure to scroll down on that page as it's a complete walkthrough.

Here's a rollup of other related links for you, Dear Reader, with downloads at the very bottom.

Announcement Links

Download Links

Enjoy!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

WebMatrix and node.js: The easiest way to get started with node on Windows

September 16, '11 Comments [24] Posted in IIS | nodejs | WebMatrix
Sponsored By

Tomasz Janczuk and Steve Sanderson are having entirely too much fun. I posted just two weeks ago on Installing and Running node.js applications within IIS on Windows - Are you mad? when Tomasz and the team got node.js running rather nicely under IIS. Now they've got a nice little development environment in WebMatrix.

Cats and Dogs, living together, mass hysteria

Then, Steve and Tomasz decide to share clever thing with us. Well, let me just show you. Remember, this is all in-progress goofing around v0.5 stuff at best but the concepts are sound.

node.js inside WebMatrix. You may start freaking out now.

And then:

Node.js Express Site1 - Microsoft WebMatrix (76)

Hit Run:

Node.js Express Site running on Windows

How can you use WebMatrix to develop node.js applications yourself? With these easy steps:

Isn't it lovely when LEGO pieces snap together so nicely?

Enjoy, Dear Reader.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Back to Basics: Big O notation issues with older .NET code and improving for loops with LINQ deferred execution

September 15, '11 Comments [58] Posted in Back to Basics | Learning .NET | LINQ
Sponsored By

Earlier today Brad Wilson and I were discussing a G+ post by Jostein KjĂžnigsen where he says, "see if you can spot the O(n^2) bug in this code."

public IEnumerable<Process> GetProcessesForSession(string processName, int sessionId)
{
var processes = Process.GetProcessByName(processName);
var filtered = from p in processes
where p.SessionId == sessionId
select p;
return filtered;
}

This is a pretty straightforward method that calls a .NET BCL (Base Class Library) method and filters the result with LINQ. Of course, when any function calls another one that you can't see inside (which is basically always) you've lost control. We have no idea what's going on in GetProcessesByName.

Let's look at the source to the .NET Framework method in Reflector. Our method calls Process.GetProcessesByName(string).

public static Process[] GetProcessesByName(string processName)
{
return GetProcessesByName(processName, ".");
}

Looks like this one is an overload that passes "." into the next method Process.GetProcessesByName(string, string) where the second parameter is the machineName.

This next one gets all the processes for a machine (in our case, the local machine) then spins through them doing a compare on each one in order to build a result array to return up the chain.

public static Process[] GetProcessesByName(string processName, string machineName)
{
if (processName == null)
{
processName = string.Empty;
}
Process[] processes = GetProcesses(machineName);
ArrayList list = new ArrayList();
for (int i = 0; i < processes.Length; i++)
{
if (string.Equals(processName, processes[i].ProcessName, StringComparison.OrdinalIgnoreCase))
{
list.Add(processes[i]);
}
}
Process[] array = new Process[list.Count];
list.CopyTo(array, 0);
return array;
}

if we look inside GetProcesses(string), it's another loop. This is getting close to where .NET calls Win32 and as these classes are internal there's not much I can do to fix this function other than totally rewrite the internal implementation. However, I think I've illustrated that we've got at least two loops here, and more likely three or four.

public static Process[] GetProcesses(string machineName)
{
bool isRemoteMachine = ProcessManager.IsRemoteMachine(machineName);
ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(machineName);
Process[] processArray = new Process[processInfos.Length];
for (int i = 0; i < processInfos.Length; i++)
{
ProcessInfo processInfo = processInfos[i];
processArray[i] = new Process(machineName, isRemoteMachine, processInfo.processId, processInfo);
}
return processArray;
}

This code is really typical of .NET circa 2002-2003 (not to mention Java, C++ and Pascal). Functions return arrays of stuff and other functions higher up filter and sort.

When using this .NET API and for looping over the results several times, I'm going for(), for(), for() in a chain, like O(4n) here.

Note: To be clear, it can be argued that O(4n) is just O(n), cause it is. Adding a number like I am isn't part of the O notation. I'm just saying we want to avoid O(cn) situations where c is a large enough number to affect perf.

image

Sometimes you'll see nested for()s like this, so O(n^3) here where things get messy fast.

Squares inside squares inside squares representing nested fors

LINQ is more significant than people really realize, I think. When it first came out some folks said "is that all?" I think that's unfortunate. LINQ and the concept of "deferred execution" is just so powerful but I think a number of .NET programmers just haven't taken the time to get their heads around the concept.

Here's a simple example juxtaposing spinning through a list vs. using yield. The array version is doing all the work up front, while the yield version can calculate. Imagine a GetFibonacci() method. A yield version could calculate values "just in time" and yield them, while an array version would have to pre-calculate and pre-allocate.

public void Consumer()
{
foreach (int i in IntegersList()) {
Console.WriteLine(i.ToString());
}

foreach (int i in IntegersYield()) {
Console.WriteLine(i.ToString());
}
}

public IEnumerable<int> IntegersYield()
{
yield return 1;
yield return 2;
yield return 4;
yield return 8;
yield return 16;
yield return 16777216;
}

public IEnumerable<int> IntegersList()
{
return new int[] { 1, 2, 4, 8, 16, 16777216 };
}

Back to our GetProcess example. There's two issues at play here.

First, the underlying implementation where GetProcessesInfos eventually gets called is a bummer but it's that way because of how P/Invoke works and how the underlying Win32 API returns the data we need. It would certainly be nice if the underlying API was more granular. But that's less interesting to me than the larger meta-issue of a having (or in this case, not having) a LINQ-friendly API.

The second and more interesting issue (in my option) is the idea that the 2002-era .NET Base Class Library isn't really setup for LINQ-friendliness. None of the APIs return LINQ-friendly stuff or IEnumerable<anything> so that when you change together filters and filters of filters of arrays you end up with O(cn) issues as opposed to nice deferred LINQ chains.

When you find yourself returning arrays of arrays of arrays of other stuff while looping and filtering and sorting, you'll want to be aware of what's going on and consider that you might be looping inefficiently and it might be time for LINQ and deferred execution.

image

Here's a simple conversion attempt to change the first implementation from this classic "Array/List" style:

ArrayList list = new ArrayList();
for (int i = 0; i < processes.Length; i++)
{
if (string.Equals(processName, processes[i].ProcessName, StringComparison.OrdinalIgnoreCase))
{
list.Add(processes[i]);
}
}
Process[] array = new Process[list.Count];
list.CopyTo(array, 0);
return array;

To this more LINQy way. Note that returning from a LINQ query defers execution as LINQ is chainable. We want to assemble a chain of sorting and filtering operations and execute them ONCE rather than for()ing over many lists many times.

if (processName == null) { processName = string.Empty; }

Process[] processes = Process.GetProcesses(machineName); //stop here...can't go farther?

return from p in processes
where String.Equals(p.ProcessName, processName, StringComparison.OrdinalIgnoreCase)
select p; //the value of the LINQ expression being returned is an IEnumerable<Process> object that uses "yield return" under the hood
Here's the whole thing in a sample program.
static void Main(string[] args)
{
var myList = GetProcessesForSession("chrome.exe", 1);
}

public static IEnumerable<Process> GetProcessesForSession(string processName, int sessionID)
{
//var processes = Process.GetProcessesByName(processName);
var processes = HanselGetProcessesByName(processName); //my LINQy implementation
var filtered = from p in processes
where p.SessionId == sessionID
select p;
return filtered;
}

private static IEnumerable<Process> HanselGetProcessesByName(string processName)
{
return HanselGetProcessesByName(processName, ".");
}

private static IEnumerable<Process> HanselGetProcessesByName(string processName, string machineName)
{
if (processName == null)
{
processName = string.Empty;
}
Process[] processes = Process.GetProcesses(machineName); //can't refactor farther because of internals.

//"the value of the LINQ expression being returned is an IEnumerable<Process> object that uses "yield return" under the hood" (thanks Mehrdad!)

return from p in processes where String.Equals(p.ProcessName == processName, StringComparison.OrdinalIgnoreCase) select p;

/* the stuff above replaces the stuff below */
//ArrayList list = new ArrayList();
//for (int i = 0; i < processes.Length; i++)
//{
// if (string.Equals(processName, processes[i].ProcessName, StringComparison.OrdinalIgnoreCase))
// {
// list.Add(processes[i]);
// }
//}
//Process[] array = new Process[list.Count];
//list.CopyTo(array, 0);
//return array;
}

This is a really interesting topic to me and I'm interested in your opinion as well, Dear Reader. As parts of the .NET Framework are being extended to include support for asynchronous operations, I'm wondering if there are other places in the BCL that should be updated to be more LINQ friendly. Or, perhaps it's not an issue at all.

Your thoughts?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.