Geeks With Blogs

News

Currently Reading

CLR via C#m
Under The Influence(of code) Abhijeet Patel's blog C#
Maximize your keystrokes before you die
Today’s topic/rant is not a technical topic but very relevant to techies in particular. Someone wise that I respect, one said “You have a finite number of keystrokes left in your hands before you die” and created this. Morbidity aside, this is a true statement. However, the upper and lower limits of the finiteness of your keystrokes is controlled by no one else but you and the choices you make for yourself. “How so?”, you might ask. The short answer is the the nature of the work we do and motivation.By ......

Posted On Friday, July 15, 2016 9:14 PM

Mutexes, Semaphores, Monitors, ReaderWriterLocks, AutoResetEvents & ManualResets oh my!
Mutexes, Semaphores, Monitors, ReaderWriterLocks, AutoResetEvents & ManualResets oh my! Up until a few years ago, mainstream programming for the most part did not involve asynchrony and/or parallelism except for the most performance demanding applications. In the multi core world we live in today async and parallel programming have become common place. Libraries such as TPL which provide first class framework level support and keywords like async/await built on TPL provide language support for ......

Posted On Sunday, July 15, 2012 6:58 PM

MEF 101 - Part2
This is part 2 of a 2 part series exploring the MEF.We covered some of the basics in Part 1 In this part we'll cover the following: Catalogs Recomposition Export Providers Catalogs: Catalogs provide one way for MEF to discover components that it can compose. In it's most basic form it contains a registration of types. A container by itself is just an empty repository. The catalog is the one that collects and returns ComposablePartDefinitions i.e (objects of types that you have registered with MEF) ......

Posted On Sunday, October 30, 2011 3:27 PM

MEF 101 - Part1
The Managed Extensibility framework or MEF is a framework released as part of .NET 4 and Silverlight that provides a mechanism to dynamically compose an application at runtime out of loosely coupled parts and provides a variety of mechanisms to discovering these parts. You can think of this as an implementation of a plugin framework that enables plugging in functionality into a container, based on information available at runtime. The most notable example of a MEF client is the Visual Studio 2010 ......

Posted On Saturday, July 3, 2010 9:03 AM

A simple Dynamic Proxy
Frameworks such as EF4 and MOQ do what most developers consider "dark magic". For instance in EF4, when you use a POCO for an entity you can opt-in to get behaviors such as "lazy-loading" and "change tracking" at runtime merely by ensuring that your type has the following characteristics: The class must be public and not sealed. The class must have a public or protected parameter-less constructor. The class must have public or protected properties Adhere to this and your type is magically endowed ......

Posted On Sunday, April 4, 2010 3:41 PM

Chunking a List - .NET vs Python
Chunking a List As I mentioned last time, I'm knee deep in python these days. I come from a statically typed background so it's definitely a mental adjustment. List comprehensions is BIG in Python and having worked with a few of them I can see why. Let's say we need to chunk a list into sublists of a specified size. Here is how we'd do it in C# static class Extensions { public static IEnumerable<List<T>... Chunk<T>(this List<T> l, int chunkSize) { if (chunkSize <0) { throw ......

Posted On Sunday, March 7, 2010 12:36 PM

Showing Progress in a .NET Console Application
Showing the progress of an operation is trivial in rich client applications using a progress bar control of some sort. But what about .NET console apps? The Console class offers 2 static methods Console.Write and Console.WriteLine to write out to the Console, but any subsequent calls to these methods will write the content out at the current location of the cursor.This doesn't really help when you want the progress percentage to refresh in place instead. The trick is to write out the "\r" character ......

Posted On Sunday, February 21, 2010 10:50 AM

Covariance and Contravariance in C# 4.0
C# 4.0 introduces the notion of Covariance and Contravariance of generic type parameters for interfaces and delegate types. Eric Lippert has put together a bunch of posts that goes into details of the why and how, an excellent read but not for the faint of heart. I would strongly suggest reading these posts to get a firm grounding and a better appreciation of this feature. It took me a while to get my head wrapped around this, especially since none of the VS2010 Betas were not out at the time and ......

Posted On Sunday, January 10, 2010 12:50 PM

T4 template consuming a WCF service
It’s been a while since I’ve blogged since things have been crazy busy. I finally decided to kick myself and do a post so without further ado, the problem at hand: A co-worker of mine asked me whether its possible to dynamically create a type from some data he is receiving from a WCF service (over HTTP). My natural response was, why would you need to do that since Visual Studio creates a proxy from the metadata exposed by the service and the return types would typically be DataContracts. Well, it ......

Posted On Sunday, October 18, 2009 12:06 AM

Why no Anonymous Iterators in C#
Yes I’m a language geek and yes the awesome Eric Lippert is my idol when it comes to anything C# and the CLR, I’ve been following his blog for a long time and recently Eric wrote an awesome series on the how and why of Iterator Blocks. The entire series is very well worth a read, but then again I’d read almost anything Eric writes from why the sky is blue to why the Falkirk Wheel has horns. I had posed a question on this post about why there are no anonymous iterators in C#. Eric provided a very ......

Posted On Monday, August 24, 2009 9:55 PM

Creating Higher Level APIs with TPL
The BCL has a bunch of APIs which provide asynchronous versions of operations in the form of either Beginxxx/Endxxx or XyzCompleted, the former known as Asynchronous Programming Model(APM) and the latter known as Event Based Asynchronous Pattern (EAP). TPL provides a nice abstraction for performing asynchronous operations as Tasks using delegates, but what can you do for working with these existing BCL classes which already have an async version? Here is an example. Let's say we need to download ......

Posted On Sunday, August 23, 2009 3:46 PM

CLR via C# 3rd Edition
Some book news…Jeff Richter is one of my favorite authors and I’ll read just about anything he writes. His last book CLR via C#, 2nd Edition left me wanting more, luckily Jeff has signed into a contract for writing a 3rd edition of the book updated for C#3.0/4.0 and .NET 4.0 ......

Posted On Sunday, August 16, 2009 3:06 PM

Retrieving Added Entities from the ObjectStateManager to avoid duplication
Let's say we neede to process a bunch of entities coming from an XML file, create EDM entites for each XML element and eventually persist them. Very trivial to accomplish using a little bit of LINQ. <Roles> <Role name="Admin"/> <Role name="Reader"/> <Role name="Writer"/> </Roles> var doc = XDocument.Load("Roles.xml"); var roles = doc.Element("Roles").Elemen... => new Role { RoleName = (string)x.Attribute("name") }); PermissionsAPIContext context = ......

Posted On Thursday, July 23, 2009 1:41 AM

Inside TPL video
No code today. Instead, here is a link to a great video from Channel9 I found, which takes a deep dive into the underlying mechanics of how TPL does what it does and some of the enhancements that the thread pool has undergone. Albeit a year old, this video goes into details of the work stealing algorithm and local thread queues that helps increase throughput. From the perspective of being a consumer of TPL, you can be blissfully ignorant of what happens behind the scenes, but understanding the mechanics ......

Posted On Friday, July 17, 2009 2:36 PM

Performing multiple Asynchronous Actions using TPL
I finally had a chance to install VS2010 Beta 1.Full instruction on how to here VS2010 has undergone a major overhaul since VS 2008, a peek are some of the cool new eye candy can be found here I've been waiting to get my hands on the TPL/PLINQ stuff and the Beta was definitely worth the wait. In a previous post we talked about using asynchronous actions for performing long running operations. I was anxious to see how the new TPL stuff would help in achieving the same thing. Interestingly enough, ......

Posted On Wednesday, June 17, 2009 12:50 AM

Passing Continuations
Last Time we saw how to perform Actions asynchronously and I promised to talk about continuations. Continuations also known as CPS or Continuation Passing Style is a mechanism for passing in code that would normally execute after a function call to the callee function as an argument, perhaps most well known in Scheme. For example if you were to call a function Sum which returns an int and print out the result like so: public static int Sum(int n) { //sure we could use (n*(n+1))/2 but this is just ......

Posted On Wednesday, April 15, 2009 11:47 PM

Object equality in Except and other set operators in LINQ
Here is an interesting issue I noticed when using the Except extension method. I have list of users from which I want to exclude some users: The list of users is coming from an XML file: <Users> <User id="1" name="Jack"/> <User id="2" name="Jim"/> <User id="3" name="Joe"/> <User id="4" name="James"/> <User id="5" name="Tom"/> <User id="6" name="Matt"/> <User id="7" name="Jon"/> <User id="8" name="Jill"/> </Users> The code goes like this: ......

Posted On Saturday, March 28, 2009 4:23 PM

Some Extension Methods for working with the ADO.NET Entity Framework
Recently I've started working more and more with the ADO.NET Entity framework and I must say that I like it a lot. There are folks who absolutely love EDM and folks that think its really evil. It's still a v1.0 product and I treat it as such. The designer does leave a lot to be desired.A lot of bells and whistles will be added to the VS 2010 release to address a lot of the designer "desirables". It's not been an easy buy in at work for adopting EDM, but my team has been among the first to have adopted ......

Posted On Thursday, March 5, 2009 7:43 PM

Dynamic Attributes in C#
Things have been busy and it's been a while since my last post, so I decided to write up a post today related to something I have been working on recently where in I needed a way to associate an attribute with a class and some of it's properties, but the values I wanted to pass in to the attribute constructor needed to be dynamic. We all know that attributes are; classes that can be tagged onto code artifacts such as Methods,Properties,Events,D... etc. When you tag a code element with ......

Posted On Saturday, January 10, 2009 9:37 PM

LINQ to XML on assembly embedded XML files
LINQ to XML rocks. I use it pretty much day in day out to read mock up data from XML files for prototyping. Today I needed a way to work with an XML file that was embedded in an assembly as an Embedded Resource. Following are the contents of the file: <Users> <User ID="1" Name="Jim"/> <User ID="1" Name="Jon"/> <User ID="1" Name="jack"/> <User ID="1" Name="Matt"/> </Users> LINQ to XML makes working with this very easy. public static XDocument GetDocument(string ......

Posted On Tuesday, March 31, 2009 9:57 PM

Performing multiple Asynchronous Actions for long running operations
The Action delegate in .NET is pretty handy when you need to invoke a method that takes no arguments. You assign the method name or an anonymous delegate/lambda to an Action delegate variable and just call the delegate variable to have your method invoked. But, what if you needed to invoke multiple actions simultaneously or wait for all actions to complete before the main thread can proceed? Maybe these actions are responsible for pre-fetching data from a service that the main thread requires or ......

Posted On Sunday, March 29, 2009 5:19 PM

Interfaces Vs. Delegates
This is a constant battle I fight in my head when I'm designing something. MSDN has an answer: When to use Delegates Instead of Interfaces, but it is nowhere as complete as I thought it should be. Good Object Oriented Design dictates using interfaces to decouple contracts from implementations.The premise is to enable substituting different implementations of an interface.Nothing new here just plain old good design guidelines. With the introduction of delegates in .NET 2.0 and lambdas in .NET 3.5 ......

Posted On Monday, November 24, 2008 9:52 PM

C# Method overloading trivia
The overload resolution rules in C# can be a bit tricky. Following is a code snippet which finds the Max between 2 numbers. using the System.Math libraries. public double Max(double d1, double d2) { Console.WriteLine("In Max(double,double)"); return Math.Max(d1, d2); } public int Max(int i1, int i2) { Console.WriteLine("In Max(int,int)"); return Math.Max(i1, i2); } public T Max<T>(T t1, T t2) where T : IComparable<T> { Console.WriteLine("In Max<T,T>"); return t1.CompareTo(t2) > ......

Posted On Thursday, November 20, 2008 9:43 PM

Visitor.. What's in a visit anyways...
Recently I was reading Judith Bishop's C# 3.0 Design Patterns. The book has been a good read so far.The Visitor pattern implementation in the book however is implemented "classically" wherein each element of the structure being iterated over has an Accept operation which accepts an instance of a Visitor type. When the client invokes the operation on the Visitor which is supposed to kick start the visiting,the Visitor iterates over each element of the structure in some way and invokes the Accept operation ......

Posted On Wednesday, November 12, 2008 10:48 PM

Copyright © Abhijeet Patel | Powered by: GeeksWithBlogs.net