Layers in Visual Studio 2010

Visual Studio 2010 has a new featured called Layer Diagrams.  In the Ultimate edition you can create layer diagrams that model the logical layers in your software system. What is a Layer? I’m glad you asked what a layer is.  A layer is a logical grouping of types with similar external assembly dependencies.  Dependencies between layers occur only in one direction from a higher-level layer to a lower-level layer.  I.e. a higher-level layer can use types in a lower-level layer, but not vice versa. A canonical example is the Data Access Layer (or DAL).  The DAL contains all the types … Continue reading Layers in Visual Studio 2010

Modify VS 2010 Template to Reference System.Configuration

Almost every project I create in Visual Studio, I invariably have to add System.Configuration to the references for that project.  As soon as I want to do much with app.config, I need to use something in System.Configuration.  Well, rather than continue to add that reference to future projects, I’ve decided to change the project template so I don’t have to.  The following is a description of how to do that. The project templates are located at C:\Program Files\Microsoft Visual Studio 10.0\Common7\ide\ProjectTemplates\ (replace "Program Files" with "Program Files (x86)" if you’re using a 64-bit version of Windows).  For this example I’m … Continue reading Modify VS 2010 Template to Reference System.Configuration

Becoming a Visual Studio Jedi Part 1

Becoming a Visual Studio 2008 (and often Visual Studio 2005) Jedi In much the same grain as James’ Resharper Jedi posts, I’m beginning a series of posts on becoming a Visual Studio Jedi.  It involves getting the most out of Visual Studio off-the-shelf, doing things as quickly as possible and with as little friction as possible.  I think it’s useful for all users; but especially useful for those who are in situations where they can’t install refactoring tools like Refactor Pro! or Resharper. First, familiarize yourself with Sara’s Visual Studio Tips blog; then subscribe to her blog. I’ll attempt to … Continue reading Becoming a Visual Studio Jedi Part 1

Fundamentals of OOD Part 3: Method Cohesion

Single Responsibility Principle (SRP) helps us write more cohesive types and methods.  Cohesion is the relatedness of the members of a type to each other and the relatedness parts of a method’s code to other parts. Method cohesionOften times a method is not very cohesive, meaning the code that it executes relates to more than one thing.  This can often be seen with a method that contains a large switch statement.  For any invocation of the method only one case statement may be executed; meaning that blocks of code within the method don’t relate to all the other blocks.  Switch … Continue reading Fundamentals of OOD Part 3: Method Cohesion

Fundamentals of OOD, Part 2 – Encapsulation Scope

Let’s look at the ubiquitous Person concept.  It might seem logical that an application that deals with people should have a Person interface for classes to implement.  For example: public interface IPerson{                String GivenName { get; set; }                String SurName { get; set; }                IAddress Address { get; set; }} At first glance this seems fine.  The IPerson interface defines attributes that the application uses with most scenarios dealing with types of IPerson, it’s “well encapsulated”.  But, the person concept is much more broad than what IPerson is modeling.  IPerson hasn’t fully encapsulated the person concept.  A person could have … Continue reading Fundamentals of OOD, Part 2 – Encapsulation Scope

Fundamentals of Object-Oriented Design (OOD) Part 1

With increased usage of patterns and situationally specific strategies, people sometimes lose sight of the concepts and principles behind these patterns and strategies and fail to follow them when they’re not using patterns or strategies.  I feel it’s good to periodically review the fundamental concepts and first principles. Object Oriented Design (OOD) attempts to help with the complexity of designing, writing, and maintaining software.  It attempts to allow building of software by modeling real-world objects.  As with any tool, it can be used improperly, but OOD attempts to facilitate simplicity, robustness, flexibility, etc..  OOD has many fundamental concepts.  Some of … Continue reading Fundamentals of Object-Oriented Design (OOD) Part 1

Overcoming problems with MethodInfo.Invoke of methods with by-reference value type arguments

I ran into an interesting problem on the Forums recently.  Basically, when you use MethodInfo.Invoke to invoke a method with by-reference value type arguments you can’t have the invoked method update a variable/argument.  The problem is, when you invoke the method the parameter is passed to the MethodInfo.Invoke via an object array.  Since we’re dealing with a value type, the original value type is boxed and the invoked method actually updates the array element, not the original object (as it would with reference types).  For example: using System; using System.Reflection.Emit; using System.Reflection; using System.Diagnostics;   namespace InvokeTesting {     class Program     { … Continue reading Overcoming problems with MethodInfo.Invoke of methods with by-reference value type arguments

Upcoming C# 3 Guidance From Microsoft

Mircea Trofin has some design guidelines with regard to some C# 3 language additions (that I assume will make it into a revised Framework Design Guidelines of some sort).  They more less agree with the guidelines I published in Code Magazine a while ago.  There are some slight differences: Consider using extension methods in any of the following scenarios: to provide helper functionally relevant to every implementation of an interface… and, Do define extension methods in the same namespace as the extended type, if the type is an interface, and if the extension methods are meant to be used in most … Continue reading Upcoming C# 3 Guidance From Microsoft

"Object is currently in use elsewhere" error.

I was debugging what I thought was a strange exception the other day.  The exception was an InvalidOperationException and the message was “Object is currently in use elsewhere”.  Unless you’re familiar with this exception, it really doesn’t offer much as to why the exception is occurring.  There seems to be several stale threads on the Web about this issue, so I’d thought I’d post about it. As it turns out it had to do with some code that was PInvoking some native graphics functions and the interaction with the WinForm that was hosting the drawing surface was to blame. What’s … Continue reading "Object is currently in use elsewhere" error.

Formatting/parsing for a specific culture redux

In recent blog post I detailed how creating a culture via the CultureInfo constructor could actually create a user-overridden culture–which could be completely different than the culture that you’ve requested by name.  Fortunately there’s a way of overriding the user override (apologies for overloading “override”) by supplying the boolean value “false” in a CultureInfo overload. As Greg Beech commented, there’s another method to create a culture–System.Globalization.CultureInfo.CreateSpecificCulture.  This sounds like it does exactly what you might expect and creates a “specific” culture.  Unfortunately, this method too violates the principle of least astonishment and creates a culture that uses the user-overridden values when the culture … Continue reading Formatting/parsing for a specific culture redux