Matlus
Internet Technology & Software Engineering

Categorized Under: C#

C# to Html Syntax Highlighter using Roslyn

Ever since Roslyn was announced I’ve had a few ideas about what I’d want to do with Roslyn. Primarily there are two things I’d love to use Roslyn for:
  1. Code generation – Generating code in such a way that I would have more semantic information about the existing code and thus be able to generate code in a “smarter” way.
  2. C# to Html syntax highlighting – There are a few solutions out there but for one reason or another, I’m just not happy with them.
At the time of this writing the Roslyn project does not support attributes or partial methods[...]

HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc

The Asynchronous Programming Model (or APM) has been around since .NET 1.1 and is still (as of .NET 4.0) the best/recommended solution for asynchronous I/O. Many people go down the route of using a multi-threaded approach to try get speedup when the workload is I/O bound. Using multiple threads to do I/O bound tasks such as Disk I/O and Network I/O, won't (in most cases) give you any speed up and in fact could end up hurting performance. If the workload is compute bound (rather than I/O bound) then it makes complete sense to use multiple threads (provided you have multiple cores and the work is fairly long running and so justifies the overhead of spawning and managing multiple threads), as discussed in this post [...]

Linq - SelectMany

The linq SelectMany method is probably one of the most powerful method on the Enumerable class but sadly also of of the most difficult to understand. As per the the MSDN documentation for Enumerable.SelectMany, this method is described as:
Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence
[...]

Data Parallel – Parallel Programming in C#/.NET

In .NET 4.0 Microsoft have introduced a slew of support for parallel programming. Parallel programming is not something you can ignore any more since CPUs are not getting any faster but instead the number of cores per CPU is increasing and unless we as software engineers begin to use all cores available on a given machine our software is not going to get any faster. There was a time when multiple CPUs were only available on servers and server side application platforms such as IIS are inherently multi-threaded so if we were building an ISAPI or ASP[...]

Getting the No Of CPUs and Cores

Using WMI (Windows Management Instrumentation) it is really simple to get the all kinds of weird and useful information about the hardware of a computer. Here I use from the assembly/namespace to get at information on the CPU. In particular:
  • No of Cores
  • No of Logical Processors
  • No of Physical Processors
  • Hardware Bitness (32/64 bit etc.)
  • CPU Architecture (x86, x64, Itanium etc.)
Categorized Under:  
Tagged With:   

Code Gem: Snippets 

Posted on

IAsyncResult–Making Existing methods Asnchronous

The APM or Asynchronous programming model has been in .NET since the beginning. There are quite a few BCL classes that use this programming model to effectively provide asynchronous methods to methods that also have synchronous counterparts. But what if you want to make one of your existing methods asynchronous? In this post I show you how simple it is to make an existing synchronous method available as an asynchronous method. Before we go down that route, let me say that I assume you're somewhat familiar with using asynchronous methods[...]

Stop/Start Windows Service

There are two code snippets presented in this post. Both of them deal with starting and stopping Windows Services. One option is to use the command line option via C# and so as to make the usage of this code more object oriented while the other option is to use the class that's part of the .NET framework. Nothing spectacular really, but the purpose of me posting these code snippets is so its out there for those who are trying to figure this out and for me if/when I need to do this kind of thing again[...]

Linq Group By - Finding Duplicates

This post is a little snippet on using Linq to find duplicates in a list of objects where a specific property of the object is used to find the duplicates. In this particular example, the objects are and in addition to other properties each lesson has a property and what we need to do is find all lessons where the is a duplicate of another lesson. In order to solve this we use and we group by the [...]

Data Access Layer CodeGen

In an earlier article (DataReader Wrappers – Type Safe) I talked about using a DbDataReader wrapper class that allowed you to use a DbDataReader in a strongly typed way giving you the following additional benefit: The ability to use a DbDataReader as an IEnumerable<T>, giving is a performance advantage over other options because there is only ever one instance of the wrapper class created no matter how many records you've got[...]

OAuth C# Library

OAuth is a security protocol that enables a user to grant a third-party access to her web resources without sharing her password. OAuth 1.0 was published in December 2007 and quickly become the industry standard for web-based access delegation. Then in June 2008 a minor revision (OAuth 1.0 Revision A) was published, which fixed a security hole in the previous specification. OAuth 1[...]

C# Class Factory - High Performance

There are many scenarios in software design where a factory comes in handy. Essentially, when you use a family of classes polymorphic-ally, you could benefit from using a Factory. Typically a factory provides the following benefits:
  1. Decouples the client code (your code that uses these classes) from knowing how to create instances of concrete types
  2. Decouples the client code from knowledge of the concrete classes themselves. That is the client code knows only of the abstract base class or an interface but not the derived or concrete class
[...]

The Purpose and function of a Data Access Layer

My inspiration for writing this post comes from answering questions for co-workers, colleagues and others in various forums. The various data access frameworks such as Entity Framework (EF4) and other open source frameworks seem to have confused or fooled people into thinking, either that these frameworks are the data access layer or that the function they perform is in fact the function of a data access layer. I beg to differ (obviously) and so this blog post... There are two primary purposes of a Data Access Layer [...]

DataReader Wrappers - TypeSafe

DataReaders (), in .NET give you the fastest access to your data. There is nothing that beats the speed of DataReaders. However, there are a few drawbacks to using them
  1. No Type Checking
  2. If field ordering or field name changes in your stored procedures or queries will likely break your code
  3. Code that uses DataReaders tends to be not so clear to read
Here is a example of what the typical code looks like: Lets say we have a blog item record that has the following fields: [...]

ChangeType<T> - Changing the type of a variable in C#

Working on Web applications invariably means you need to change the data received (which is generally in the form of a string) into some other data type such as or etc. In the .NET framework there are a few ways in which you can accomplish this (sort of):
  • Using the static class
  • Using the
[...]

Razor Engine Host

The new Razor View Engine that is part of ASP.NET MVC 3 can be hosted outside of any Web related stuff. In other words, the Razor Engine is not dependent on IIS and can be sued in a non-Web application such as a GUI or console app. This makes a whole lot of things possible, since you can use the Razor syntax in any kind of template and generate some text output. This output could be a mail-merged text document or a code file that in turn is compiled into your application[...]

Finding links on a Web page

Let's say you want to either find all links on a certain web page or for a given set of websites, you want to know if they link back to your website. This post is about the later, but essentially you're finding all the links on a website first and then filtering the ones you want. I had a specific need and that was to find figure out which of our registered members was using WordPress for their blog/website, because we want to let them know of WordPress plug-in or updates to plug-ins that will be of interest to them[...]