Sponsors

The Lounge

Wicked Cool Jobs

Current Bloggers

Partners

 

Wikis

  • You have not yet contributed to any pages.

Versioning REST Services

Posted by Scott Seely, Saturday, March 13, 2010 (13 views)

I’ve always been intrigued by the versioning problem. The versioning problem works like this: you build an API (Application Programming Interface) to get some work done. That API is successful and consumers ask for enhancements to the API. At this point, you have a problem: how do you support everyone who uses the current API while adding enhancements? If you have been down this road, you know that you have to solve the problem early—before success.

The way you handle versioning varies. With compiled code and libraries, you send out a new DLL/Assembly/JAR file and tell folks to convert existing code when they have a chance. With Web services, including SOAP, REST, and just HTTP accessible ones, you have other issues. You typically want a single code base to consume the old and updated messages. Given that many of us are now writing REST services, I talked to InformIT about the need for an article explaining how to version these JSON/XML speaking endpoints. That article is now live and available for your reading at http://www.informit.com/articles/article.aspx?p=1566460.

Discuss (0)

ASP.NET: Cannot use a leading .. to exit above the top directory

Posted by Michael C. Neel, Friday, March 12, 2010 (209 views)

image

This is a really quick post, mostly for myself so the next time I have this issue I can find the answer (yes, I often search my blog before google).

The error “Cannot use a leading .. to exit above the top directory” can be thrown by ASP.NET when you use relative paths incorrectly.  If you generate a url with too many “../../../” levels in it that would take the user above the root directory, you can generate the exception.

Why the exception?  Security I guess, but it’s a client URL and the server should know better than let the internet walk the C drive, but at one time a company in Redmond had servers with just such issues.  My problem isn’t with the unneeded exception (after all, it would just be a 404 link worst case), but with the bug in ASP.NET that causes it.

If you are using Server.Transfer or HttpContext.RewritePath to redirect a request (say map it to a template page) and have a HyperLink control with the ImageUrl property set, you win an extra “../” by the framework.  The fix is to wrap the HyperLink control around an Image control. 

In code, if you have:

<asp:HyperLink runat="server" NavigateUrl="~/FlyPage.aspx"
ImageUrl="~/Images/DeadFly.png" Width="200" Height="200"/>

Change it to:

<asp:HyperLink runat="server" NavigateUrl="~/FlyPage.aspx">
<asp:Image runat="server" ImageUrl="~/Images/DeadFly.png"
Width="200" Height="200" />
</
asp:HyperLink>

(I said it was a quick post… now go register for CodeStock!)

Filed under:
Discuss (3)

dotTrace 3.1 64-bit disabled inside Visual Studio 2008

Posted by sergiopereira, Friday, March 12, 2010 (130 views)

I work on a web application and I use dotTrace when some profiling is needed. The problem is that I cannot fire off doTrace directly from inside Visual Studio 2008 because the commands and toolbar icons to launch it are permanently disabled. Well, not anymore.

Background

The web application I work on is a 64-bit application. I use a 64-bit version of Windows (namely, Win2008 x64). I installed the 64-bit version of dotTrace 3.1 and it does work stand-alone but it never worked integrated with VS 2008, which is a shame because it contains some neat features like it's enabling the ReSharper test runner to run the chosen test(s) directly under dotTrace profiling. The screen shot below shows how it should look like but on my box these dotTrace commands were simply disabled.

The fix

After much disappointment with the missing features, my fellow Devlicio.us blogger Hadi Hariri put me in contact with Oleg Stepanov.

The first suggestion was trying the 32-bit version of dotTrace, which wasn't an option for me because, as I said, my application and all the supporting utilities are 64-bit, it runs under a 64-bit process, so that's ultimately how I need to profile it.

Then Oleg explained that the problem is that VS is a 32-bit application and it was looking for a registry key in the wrong place, not finding it, and then it was assuming dotTrace wasn't available.

Hmmm... That sounded eerily familiar, no? Once again the little annoyances of developing 64-bit code with 32-bit tools come to bite us.

To fix the problem, just like the other time it happened, we just need to copy the right registry key to its corresponding place under the Wow6432Node key.

Open the Registry Editor and go to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ key. One its GUID-named subkeys will contain the dotTrace information. Just search for dotTrace and find the right subkey. Now just copy (or export/edit/import) that key under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ and you're ready to restarat Visual Studio and see all the integrated dotTrace features come alive.

Discuss (0)

Advanced Castle Windsor – generic typed factories, auto-release and more

Posted by Krzysztof Koźmic, Thursday, March 11, 2010 (260 views)

This post is a playground for me, to try out some ideas I want to include in my talk about Windsor at KGD.NET meeting later this month.

Scenario

We have a messaging application built around two interfaces:

public interface Command
{
}
 
public interface Handler
{
    void Execute();
}
 
public interface Handler<T> : Handler where T : Command
{
    T Command { get; set; }
}

Hopefully I don’t have to explain how they work. The idea is, application receives commands from somewhere, then it pulls all handlers registered for this command and let them handle the command. Split of Handler interface into generic and non-generic part is there to make up for lack of co/contra-variance in .NET 3.5.

Commands and Handlers

Handlers are quite simple classes implementing closed version of Handler<> interface. For example to change client’s address we’d have the following command

[Serializable]
public class UpdateClientCorrespondenceAddressCommand : Command
{
    private readonly AddressDto address;
    private readonly Guid clientId;
 
    public UpdateClientCorrespondenceAddressCommand(Guid clientId, AddressDto address)
    {
        this.clientId = clientId;
        this.address = address;
    }
 
    public AddressDto Address
    {
        get { return address; }
    }
 
    public Guid ClientId
    {
        get { return clientId; }
    }
}

and its handler:

public class UpdateClientCorrespondenceAddressHandler : Handler<UpdateClientCorrespondenceAddressCommand>
{
    private readonly Repository<Client> clientRepository;
 
    public UpdateClientCorrespondenceAddressHandler(Repository<Client> clientRepository)
    {
        this.clientRepository = clientRepository;
    }
 
    public UpdateClientCorrespondenceAddressCommand Command { get; set; }
 
    public void Execute()
    {
        var command = Command;
        if (command == null) return;
 
        var client = clientRepository.Get(command.ClientId);
        client.ChangeCorrespondenceAddress(command.Address);
        clientRepository.Update(client);
    }
}

Nothing earth shattering here. We would have similar set up for other business events in the application. We assume we can have more than one handler for single command (for example another handler would update shipping costs and promotions available for the new address of the client).

The ability to pull multiple services from the container via typed factory is not available in Windsor 2.1.1 – you need the trunk version to take advantage of it.

Registration

To register the code we create an installer:

public class Installer : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<TypedFactoryFacility>()
            .Register(
                Component.For<ITypedFactoryComponentSelector>().ImplementedBy<HandlerSelector>(),
                Component.For<AutoReleaseHandlerInterceptor>(),
                AllTypes.FromAssemblyContaining<Program>()
                    .BasedOn(typeof(Repository<>))
                    .WithService.Base()
                    .Configure(c => c.LifeStyle.Singleton)
                    .BasedOn(typeof(Handler<>))
                    .WithService.Base()
                    .Configure(c => c.LifeStyle.Is(LifestyleType.Transient)
                                        .Interceptors<AutoReleaseHandlerInterceptor>()),
                Component.For<HandlerFactory>().AsFactory());
    }
}

There are a couple interesting things here. First we register typed factory facility, which we’ll use later on to pull handlers for commands we receive. Then we register custom selector for typed factory (discussed below), and an interceptor (discussed below). Then we  register all repositories and all handlers from given assembly, configuring handlers with transient lifestyle and with the interceptor we registered above. Lastly we also register typed factory for handlers:

public interface HandlerFactory
{
    Handler[] GetHandlersForCommand(Command command);
}

Typed factory selector

The handler factory has to do quite a lot of work for us. Given an instance of a command, it has to pull from the container all the handlers for the command’s type. To do this we need to use a custom selector (and trunk version of Windsor).

public class HandlerSelector:ITypedFactoryComponentSelector
{
    public TypedFactoryComponent SelectComponent(MethodInfo method, Type type, object[] arguments)
    {
        Debug.Assert(arguments.Length == 1);
        var message = arguments[0];
        var handlerType = typeof(Handler<>).MakeGenericType(message.GetType());
        return new TypedFactoryComponentCollection(handlerType.MakeArrayType(), new Arguments(arguments));
    }
}

Based on the command’s type we create closed Handler<> type and return TypedFactoryComponentCollection (new type that pulls all components for given service) passing down the command as typed argument to the resolution.

Putting it all together

We can now use the code like this:

private static void Main()
{
    using(var container = new WindsorContainer().Install(new Installer()))
    {
        var factory = container.Resolve<HandlerFactory>();
 
        DoActualWork(factory);
        Console.ReadKey(true);
    }
}
 
private static void DoActualWork(HandlerFactory factory)
{
    var command = ImmitateCommandArrived();
 
    var handlers = factory.GetHandlersForCommand(command);
    foreach (var handler in handlers)
    {
        handler.Execute();
    }
}
 
private static Command ImmitateCommandArrived()
{
    return new UpdateClientCorrespondenceAddressCommand(Guid.NewGuid(), GetSomeAddress());
}

Notice how simple the calling code is. It has no knowledge of the (quite complex) process that takes place behind the scenes to create strongly typed instances of appropriate classes. It does not even know what actual type of command it got.

What about releasing transient handlers?

Isn’t this code too simple though? It resolves handlers, which are transient components and it does not release them. And we all know transient components need to be released in Windsor, right?

Well – it does release them actually, it just doesn’t do it explicitly. Remember we registered an interceptor with all handlers. Here’s how that interceptor looks like:

[Transient]
public class AutoReleaseHandlerInterceptor : IInterceptor
{
    private static readonly MethodInfo Execute = typeof(Handler).GetMethod("Execute");
    private readonly IKernel kernel;
 
    public AutoReleaseHandlerInterceptor(IKernel kernel)
    {
        this.kernel = kernel;
    }
 
    public void Intercept(IInvocation invocation)
    {
        if(invocation.Method!=Execute)
        {
            invocation.Proceed();
            return;
        }
 
        try
        {
            invocation.Proceed();
        }
        finally
        {
            kernel.ReleaseComponent(invocation.Proxy);
        }
    }
}

The interceptor releases the component after the call to Execute for us. Thanks to this we take the burden (no pun intended) of releasing the components from the callers, and we make sure our handlers won’t leak memory. This is quite a useful trick actually, and while I have precisely zero knowledge of NServiceBus, I think it could be used to fix the issue Davy discussed, without having to mess with release policy.

Filed under:
Discuss (1)

Zen Coding and ReSharper PowerToys

Posted by Hadi Hariri, Thursday, March 11, 2010 (729 views)

Yesterday the ReSharper Twitter Account tweeted about a Zen Coding Plug-in available as a PowerToy. If you’re not familiar with Zen Coding, check out this link. I received a couple of emails from people asking about the PowerToys and what exactly they were, and to be honest, I wasn’t really surprised. I don’t think many have actually heard of them. So I decided to follow Hanselman’s advice of minimizing key strokes, thus the post.

 

What are they?

The Power Toys are a series of plug-ins that have been written by the ReSharper developer team, and have been used in fact internally as samples of ReSharper’s API.

 

Where can I get them and how do I install them?

You can download the PowerToys for ReSharper 5 from the Early Access Program page (for previous versions, see What next?), which is here.

image 

 

Unless there’s some API change, there are normally compatible from one build to the next, but just to be safe, download the one corresponding to the nightly build you have.

 

image

They are packaged up in a zip file that contains MSI installers for each individual plug-in, as well as a PowerToys Pack which includes them all (and you can pick and choose which one you want during the installation process). Best option is to just pick the full pack. Each MSI is also suffixed with a version, 8 corresponding to Visual Studio 2005, 9 to 2008 and 10 to 2010.

The zip file also contains another zip with the source code, which you can compile and install, if you don’t want to use an installer. If you’re doing this, compile them and place them under the %Program files%\JetBrains\ReSharper\Bin\Plugins folder. Normally best to create a folder for each plug-in (If you’re just interested in having the source but prefer to use the MSI, the installer will also install the source for you).

Once you install them, they should appear as plug-ins under the ReSharper menu:

 

image

 

As you can see from the list, there are quite a few goodies in there, not only Zen Coding!

 

What next?

The PowerToys are not only valuable in terms of the functionality they provide, but also serve as an example of how to write ReSharper plug-ins and interact with the ReSharper API, but they’ve never been released passed Early Access Programs. That is one reason 4.5 PowerToys are not available.

However, we want to change that. We not only want to make them available, but also to document them and offer them as a true OSS project, where the developer community can contribute to them, be it with additional features, bug fixes or providing completely new plug-ins.

This is something that I personally think would be valuable for the community. If you like this idea or have other suggestions, please feel free to give me feedback.

Filed under: ,
Discuss (1)

Sharing Code Analysis files between projects in Visual Studio 2010

Posted by Derik Whittaker, Wednesday, March 10, 2010 (186 views)

The ability to have static code analysis (aka run FXCop inside the IDE) is not something that is new to Visual Studio 2010.  What is new is the simplicity in which you can share the exact same configuration across all of your projects at once.

Prior to VS2010 if you wanted to share your config it was not possible (from what I have ever seen).  With VS2008 each .proj file contained a section in the file which laid out each rule that should/should not be turned on.  If you wanted to share these what you needed to do was open up each .proj file in a text editor and copy/paste the rules.  As you could image, copy and pasting this could get old real fast.  In fact I would even argue that I may even prevent you from wanted to use the built in analysis all together.

Here comes Visual Studio 2010 to the rescue.  In 2010 you can save your rules to a .ruleset file and you can import this same file into all your projects.  No more need for copy/paste by hand.

Steps To Do This:

Step 1: Open up the Code Analysis tab in the project properties.

image

Step 2: In the drop down choose Browse

image

Step 3: Find your .ruleset file on disk

image

Step 4: Rinse and Repeat for all your other projects.

Now that having the ability to share your rule set across projects is painless you have NO excuse not to utilize this feature.

Till next time,

Discuss (1)

Help Save King’s Quest!

Posted by Rob Eisenberg, Tuesday, March 09, 2010 (213 views)

On February 28, 2010, Activision issued a Cease and Desist order against Phoenix Online Studios, creators of “The Silver Lining,” an unofficial sequel to the King's Quest series  The sequel had, in 2005, been approved for non-commercial release by the owner of the King’s Quest IP, Vivendi Universal.  The team has been working on the 5-part episodic King’s Quest Sequel for eight years (they started before 2005).  They had just completed their final milestone and were preparing to submit the game for approval to Vivendi Universal right before Activision and Vivendi merged. Immediately upon acquisition of the King’s Quest IP, Activision shut down Phoenix Studios, with no intent to uphold the previous agreements.

As a long time fan of adventure games, and someone who has always wanted a real sequel to KQ6, I am really saddened by this.  Personally, I plan not to have anything to do with Activision until they make this right.  I hope you will join me in this.  If you have a few minutes, please sign the online petition and consider sending a letter to Activision.  Resources are below.  Thank you.  Now, on with your regularly scheduled programming…

Online Petition:
http://www.petitionspot.com/petitions/savetsl/
Facebook Group:
http://www.facebook.com/group.php?gid=382202612795&ref=mf
MySpace Page:
http://www.myspace.com/savetsl
Form Letter for Contacting Activision:

http://www.tsl-game.com/forum/index.php?topic=8406.0

Discuss (5)

[ANN] Chicago ALT.NET shows Rake and Albacore

Posted by sergiopereira, Tuesday, March 09, 2010 (86 views)

I haven't mentioned our meetings here in a while but our group has been going strong and enthusiastic all this time.

Tomorrow, March 10th our topic will be build scripts for .Net projects using Rake and Albacore. I've been using Rake and a little bit of Albacore in my own projects and I'm ready to say that it will take a very serious event to make me go back to NAnt or MSBuild.

Introduction to Rake with Albacore.NET

6:00 pm
Pizza and networking time

6:30 pm

How would you to write your build scripts using a scripting language instead of XML? In this month's meeting we will see how the ease of programming in Ruby can be used to create a much more pleasant and extensible build script.

Rake isn't just for Rubyists or Alphageeks anymore. Albacore helps bring the power and expresiveness of the Ruby language to the world of .NET build automation. Using Rake it's never been easier to handle build automation, test execution, continuous integration and just about any task you need to automate for your build.

Michael D. Hall has been developing software on the Microsoft platform for over a decade. He's been an Alt.NETter for years and is really enjoying the exposure to different ideas and concepts beyond the safe confines of the .NET world. Currently he's a consultant working with Obtiva and has started a Cloud Developer's Group that meets monthly in McHenry county.

Register for Introduction to Rake with Albacore.NET in Chicago, IL  on Eventbrite
Discuss (0)

Silverlight Challenge: Creating a DependencyProperty

Posted by Christopher Bennage, Tuesday, March 09, 2010 (256 views)

The following code throws an exception. Can you spot the bug?

public class MyControl : Control
{
    public static DependencyProperty MyPropertyProperty = DependencyProperty.Register(
        "MyProperty",
        typeof (double),
        typeof (MyControl),
        new PropertyMetadata(0));

    public double MyProperty
    {
        get { return (double) GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }
}
Filed under: ,
Discuss (5)

Profiling Apps 1 of N: The MVC ActionLink

Posted by Hadi Hariri, Tuesday, March 09, 2010 (823 views)

 

I’m starting a new series of blogs posts on profiling, where we’ll try and cover common bottlenecks and how to identify them in your applications. However, before delving deeper into the subject, let me make a small but important observation:

Your bottleneck is probably not your for loop

Now, replace that for loop with switch statement, an older version of some outdated algorithm that you feel needs optimizing, or that retched collection of classes that would perform better if you were using an array to loop through them, and you’ll end up with the same observation.

Premature Optimization

When dealing with business applications, it is unusual for major performance problems to be pinpointed down to specific portions of code or a concrete implementation of an algorithm. Usually most of the issues are bottlenecks at the data level, network level or purely down to how a business decision is made.

Whether we use an ORM or use SQL directly, incorrectly formulated queries are one of the most predominant causes of bad performance. Not understanding concepts such as Lazy or Eager loading when using an ORM can be disastrous to the performance of an application, and are usually portrayed as “ORM XYZ sucks at performance”. 

Network bandwidth and latency are other issues; when dealing with web applications for instance, having large pages (i.e. ViewState) or rendering Javascript directly without using script files, are a common problem for performance penalties. Making heavy calls to the server when very little information is required (i.e. UpdatePanel used incorrectly) are again main causes for concern.

In many cases, design decisions we make early can affect the performance of our applications, and it is important to identify these concerns and address them correctly. Using an ORM, profiling it, understanding how Ajax really works and not worrying about working with Javascript, or using an asynchronous architecture when dealing with long running business processes are many ways to avoid bad performance in the long run.

On the other hand, what we shouldn’t do is focus on micro-optimizations, on trying to make the most efficient, yet completely undecipherable algorithm to calculate the probability of winning money when buying lottery tickets, when the underlying problem is a bottleneck caused by a bad query. This kind of approach is often referred to as Premature Optimization, and can be disastrous for a project.

When it does boil down to code

Having said all that, there are times when we need better performance after having eliminated all the obvious causes, and need to discover why something is not performing as well as it should be. Of course, these concerns are greater when the nature of our application demands highly optimized code. In these cases, it is crucial to understand how things work in order to solve the problem. As a old-school boy, before we had managed libraries and drag-n-drop, I’m also a firm believer that it is always important to understand how things work under the covers, even if it is to just improve one self's knowledge.

Therefore, in these series of blog posts about performance, I’m going to focus on the latter, examining the details of code and how some things can perform better than others. So given the disclaimer, let’s get down to business.

In order to do performance tuning, you need to use a tool. Setting stopwatches doesn’t work, because as Christian Gross so rightly pointed out during one of his talks, and I semi-quote: ‘if you’re using a stopwatch, you think you know where the bottleneck is. Most of the time, you’re wrong’.  If you are setting using a manual approach of setting calling Start and Stop, trying to time something, you’re assuming you know that the performance problem is located in a particular point, and many times it is not that point. So you end up having to place these kind of diagnostic codes in various places in your code, and soon it becomes a maintenance nightmare. Fortunately, there are tools that can profile your application in a non-invasive manner. When talking about SQL profiling, there’s NHProfiler for instance. When it comes to code performance profiling, the two most known ones are ANT Profiler and dotTrace. I’m going to be using dotTrace. I used it before joining JetBrains and continue to use it now that I’m at JetBrains. I’ll be using version 4.0 which is currently (at the time of writing this post), in Early Access Program and with Beta being released very soon.

To Express or not Express

Those that are familiar with it ASP.NET MVC know it relies heavily on the use of strings in many areas. For instance, when defining ActionLinks, you write

Html.ActionLink(“Home”, “Index”, “Home”)

where the first parameter is the link text, the second the Action and the third parameter is the Controller. The problem with this of course is that if you type either the second or third parameter incorrectly, you won’t know until runtime. Even if you build your views it won’t help.

An alternative is to use Expression based Html Helpers (another option is to use ReSharper of course :)). These are strongly-typed ActionLinks that do not ship out of the box with ASP.NET MVC, but are available as a separate download in the MVC Futures assembly, which can be thought of as a kind of sandbox for Microsoft to play with. Some of the functionalities in this library have eventually made it to the main core, such as RenderPartial, which was in fact there from pretty much the early Previews of MVC 1, and didn’t get all the excitement until it made it into the core in version 2. Other functionality, including the expression based ActionLinks haven’t made it in yet. When using these helpers, the previous link would be:

Html.ActionLink<HomeController>( a => a.Index, “Home”)

In principle this looks good, and begs the question of why it is not in the main core. Well I don’t know the exact reason, but one could potentially be due to it’s performance. Several people have talked about the difference in terms of rendering when using this version as opposed to the standard string based one. You can find one of those posts here. What I thought I’d do, is actually see how much difference in speed there is between one and the other.

The Project

I’m using a very simple project for this profiling. It’s your standard ASP.NET MVC 2 application. On Index page, I’ve added two blocks of code:

  1. <% for (int i = 0; i < 1000; i++)
  2. {%>
  3.   <%= Html.ActionLink("Link " + i, "Index", "Home")%><br />
  4.   <%
  5. } %>
  6.  
  7. <% for (int i = 0; i < 1000; i++)
  8. {%>
  9.   <%= Html.ActionLink<HomeController>(a => a.Index(), "Link " + i)%><br />
  10.   <%
  11. } %>

The first for loop will render out 1000 links using the string based ActionLink version, whereas the second loop will do the same using the expression-based one.

What we want to do now is run this and see how long it takes for each loop to complete.

Setting up dotTrace Profiler

Working with dotTrace is as easy as it gets. There are two ways to profile an application: Standalone or integrated within Visual Studio. In the case of the former, you can start up dotTrace outside of Visual Studio and point to an application to profile. On the other hand if you have it integrated inside Visual Studio, then all you need to do is click on the Profile menu option:

image

Once we do that, we get a dialog box that provides us a series of options, mainly to do with the type of profiling we are going to perform.

image

Since we’re profiling a web application, we can either use the Development Server or Internet Information Server. In our case we’re going to use the former. dotTrace will automatically pick up the server settings as well as fill out the physical path for our application.

Next come the profiling options. The basic settings are Profiling Type and Meter Kind. The first parameter indicates how profiling will take place. It can be:

  • Sampling: dotTrace will do frequent analysis of calls stacks. It’s the least intrusive, has very little impact on performance, but gives approximate timing.
  • Tracing: dotTrace receives notifications from the CLR on entry/exit of methods. More precise timing and call information.
  • Line-by-Line: dotTrace logs times for every statement in methods. Most precise but also has higher impact on performance.

Tracing is normally the recommended option. Meter Kind defines how dotTrace logs the time: CPU instruction or Performance counter (uses the Windows API and samples are hardware independent).

Profiling our application

Once we have everything setup, we can start profiling our app. dotTrace will launch a small panel that allows us to control data sampling.

image

dotTrace does not by default however launch the browser. In order to do so, we need to either click on the WebDev server and Open in Browser or just type the URL directly in the browser.

image

The next step is to perform the operations we want to profile and then click on GetSnapshot.

image

Since in our case, having rendered the Index action performs these operations, once the page has been loaded, we can click on GetSnapshot and have the profiler launched.

image

I’m not going to get into all of the details of dotTrace in this first post because otherwise it would never end;  we’ll cover some of the aspects in future posts. For now, lets focus on our performance test at hand which is the difference between the two types of ActionLinks (string and expression based).

The easiest way to find what we are looking for is to use the…you guessed it, Find feature. Ctrl+F will bring up a dialog box similar to ReSharper’s Type location. We can then type ActionLink to filter the list of functions down to the ones that interest us

image

We can see that there are two versions, as expected. Let’s drill in to the second one first, the string based one. Hitting Enter will find the first location. We can then press F3 until we find the one that interests us. Remember, Site.Master and other references to this call also exist. We’re specifically looking for the loop, the one with 1000 calls

image

We can see that the ActionLink call takes 121ms for 1000 calls. Drilling in, we can see exactly where the time is spent, and 104ms of that is calling GenerateUrl. Now let’s take a look at the Expression based ActionLink

image

For the same 1000 calls it takes 140ms, which is an increase of approximately 17%. Diving in once again, we can see that 6ms of this is in the parsing of the expression tree, GetRouteValuesFromExpression. What this function does is merely analyze the expression to extract the ActionName from the parameter. The ControllerName it already has since it’s the concrete type the generic method is invoked with, returning both values in a RouteValueDictionary. As such it then needs to call GenerateRouteLink as opposed to GenerateLink since the former takes a RouteValueDictionary as a parameter, whereas the latter takes strings indicating the controller and action. They ultimately both call GenerateUrl.

Summary

From the results, the difference between the two calls is not that significant for 1000 links. As the number of links increment, the difference between the two does not change significantly. For instance, rendering 10.000 links, has a difference of 50ms between one version and another. What’s interesting that having run the same profiling on previous releases, the difference in time was nearly double, so there seems to have been improvement in this area. And as we can see, sometimes what might seem a performance problem, isn't necessarily one.

Filed under: , ,
Discuss (8)

Rule "Previous releases of Microsoft Visual Studio 2008" failed

Posted by sergiopereira, Sunday, March 07, 2010 (284 views)

Today I was trying to install SQL 2008 on my box and the setup stopped after checking a bunch of rules. The error message was the title of this post.

A quick search on the internet revealed that somehow the installer didn't believe I had VS 2008 SP1 installed, which I did. The recommendations in the KB article were kind of insulting. There's no way I'd spend hours of my day uninstalling and reinstalling VS and SQL — sorry, no chance. I also could not accept not installing the Management Tools, for example. I also did not have any Express version of VS or SQL installed in this box.

A little snooping around with ProcMon led me to the following registry key:

HKLM\SOFTWARE\Wow6432Node\Microsoft\DevDiv\VS\Servicing\9.0\IDE\1033

In that key I noticed the suspicious values:

"SP"=dword:00000000
"SPIndex"=dword:00000000
"SPName"="RTM"

Without quitting the SQL server installer validaton screen, I changed these values to what you see below, crossed my fingers and rerun the installer validation, which passed!

"SP"=dword:00000001
"SPIndex"=dword:00000001
"SPName"="SP1"

Now, I didn't really guess those values. I looked in a sibling registry key (...Servicing\9.0\IDE\1033) and saw that it contained those new values, then I copied them.

I think I didn't break anything. So far all seems to be working. But, as usual with anything related to manual registry hacking, you have to be really insane to change your settings because you read on a random blog on the 'net. I'm just saying... Don't come crying if your house burns down because of this.

Discuss (2)

The Biggest Driver For Domain Modeling Decisions

Posted by Michael Nichols, Saturday, March 06, 2010 (542 views)

I just read a post from fellow devlicio.us'er Billy McCafferty and was considering what influences my own solutions to these concerns (ie, repositories, data transfer, etc) now.

There has been an abrupt shift/growth in my thinking this year in how I perceive my domain and how I interact with or scale it. CQRS, perceived by some as a shiny new toy, and the discussions it provokes has definitely contributed to the maturity of the project I have been on by increasing its quality and adaptability. Why? My experience has been the simple shift to being concerned about what all these things are doing (behavioral mindset) instead of what they look like (stateful mindset). I now see that the decision to expose domain state at all should be one of the first decisions to make and its implications on the various infrastructural concerns should be well-understood. This seems to drive divers decisions both within the domain and how it is consumed or utilized without.

The first casualty of this shift was the getters and setters (whether exposed as properties or GetX()/SetX() methods). Hiding as much state as possible radically increases the freedom in the domain, especially when going down the event-sourcing route. As you know, the moment you expose a getter you now have a maintenance obligation. What I am discovering is that feels okay initially, but it very quickly acts as a Trojan Horse for forces outside the domain to act on its modeling. That innocent {get;} can make refactoring far more difficult that it needs to be, if only the decision were made earlier to Report state from somewhere other than the domain.

IMHO, this is true for a “small” application, although I am not sure I believe in “small” applications anymore. They all seem to be getting feature-creep hormones from somewhere :).

Providing Metadata to your MEF exports

Posted by Derik Whittaker, Saturday, March 06, 2010 (729 views)

Many months ago I created a post on how to get started with MEF.  Ever since then I have been meaning to get this post (and others) about about how to get up and running quickly with MEF.

In this post will demonstrate how to provide Metadata (custom values which can help provide context) information to your Exported item.  Why should you care about Metedata?  Below is a great reason from the MEF site on codeplex

Declaring Exports explained the basics of parts exporting services and values. In some cases it’s necessary to associate information with exports for a variety of reasons. Commonly it’s used to explain about the capabilities of an specific implementation of a common contract. This is useful to allow imports to either constraint the export that can satisfy it, or to import all available implementations at the time and check their capabilities in runtime before using the export.

Before we get rolling:

What I am going to show in this post is how to create a custom Export Attribute.  It is possible to expose Meta via the build in ExportAttribute, but the built in mechanism has a few short comings:

  1. Not discoverable
  2. Not strongly typed
  3. Compiler cannot validate the data content

Lets get rolling:

Pre-Step 1: Taking a look at the Class which we will mark as a Export

    public interface IPlugin
    {
        string PluginAction();
    }

    [Export(typeof(IPlugin))]
    public class DefaultPlugin : IPlugin 
    {
        public string PluginAction()
        {
            return "This is the default plugin";
        }
    }

Step 1: Defining the interface which will represent your

In order to create discoverable Metadata you will first want to create an interface which will represent your metadata.  Below is my interface.

    public interface IPluginMetaData
    {
        string Name { get; }
        string Version { get; } 
    }

We will later use this interface when we define and use our custom Export Attribute.  The main reason for creating this attribute is now the compiler has a hard wired way to know exactly what type of data is being exposed in your Metadaa

Step 2: Defining your custom ExportAttribute Attribute

Now that we created our interface to give us strong typing and discoverability we need to create a custom attribute which will allows the MEF engine to know we have Metadata to expose

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class PluginMetadataAttribute : ExportAttribute
    {
        public PluginMetadataAttribute( string name, string version)
            : base(typeof(IPluginMetaData))
        {
            Name = name;
            Version = version;
        }

        public string Name { get; set; }
        public string Version { get; set; }
    }

When looking at the code above there are 3 things to pay close attention to

  1. You need to make a call into the base constructor of the ExportAttribute and provide it the type of your interface which defines your Metadata
  2. You need to mark your attribute with the [MetadataAttribute] (which is part of MEF)
  3. You need to mark the usage type of your attribute.  I am not 100% why this is needed (Glenn, you out there with an answer???)

Step 3: Providing your Metdata to your Exported Class

    [Export(typeof(IPlugin))]
    [PluginMetadata("Default", "1.0.0.0")]
    public class DefaultPlugin : IPlugin 
    {
        public string PluginAction()
        {
            return "This is the default plugin";
        }
    }

The code above is the same as the code we saw in pre-step 1, but this time we have marked the class with our custom Metadata attribute.  We have now officially provided our Metadata to our Export…. But how do we get this information out of MEF?

Step 4: Setting up your [ImportMany] to use the Metadata

If you are using an ImportMany (this should also work on an Import, but I have never tried) most likely you are pushing your Exports into a IList or IEnumerable.  If this is the case, your changes are pretty minor.  Below is what you need to do in order to have your Metadata loaded.

[ImportMany(typeof(IPlugin), AllowRecomposition = true)]
private IList<Lazy<IPlugin, IPluginMetaData>> _loadedIPlugins = new List<Lazy<IPlugin, IPluginMetaData>>();

If you have not seen the Lazy keyword, you are not alone.  This is new to .Net 4 and this provides Lazy initialization support to the framework.  (part of the System namespace).  One key thing to understand here though is that the Lazy keyword out the box is only Lazy<T>, but the Lazy we are using is Lazy<T, M>.  This extended version of Lazy lives in the MEF assemblies (System.ComponentModel.Composition) and will need to be referenced to use.

Step 5: Using the Metadata

Once you have the Export loaded into your container (which by the way does not need to change to have this work) you can access your container by selecting the .Metadata property on your instance of the export.  The image below shows it in action.

image

As you can see, exposing and using Metadata via MEF is a snap. 

Till next time,

Discuss (4)

A Few Thoughts on DDD, DTOs, View Models, Repositories and Separation of Concerns

Posted by Billy McCafferty, Saturday, March 06, 2010 (1,602 views)

What I've loved most about developing an open-source project is the ideas that I get from others who look at the work and either A) validate ideas, B) suggest that something stinks, or C) call a royal WTF and force people (e.g., me)  to explain ideas more fully.  It's usually during these explanation attempts that light bulbs start to come on and ideas are refactored and become more substantiated (or at least more defendable).  The S#arp Architecture forums have been a gold mine, IMO, for the discussion of the practical application of balanced DDD techniques on real-world projects.  After feedback that I've already gotten on my recent post concerning application services and CQS and listening to and adding my two cents concerning ideas on the S#arp forums, I'd like to share a few ideas (that I shared on the forum) concerning drawing the line among application layers, the management of DTOs and View Models, and a general discussion of separation of concerns.  (As Chris Carter correctly (and a tad bluntly) explained, don't take these ideas at face value...use them as a point of discussion and contemplation to come to your own conclusions with your project team...and let me know what you think.)

Every great developer knows that any problem can be solved by adding another layer of abstraction.  ;)

Obviously, with additional layers of abstraction and indirection, we also introduce complexity and additional objects which need to be maintained.  Over the past year, I have had struggles concerning just how far the "DTO paradigm" should be taken to effectively separate layers from the domain and "best practices" for how they should be used.  I don't think the answer can be found in a best practice (but I also hate to leave it simply at "it depends").  I truly feel that the most important practice is for a project development team to decide, and firmly agree upon, which approach will be taken on a project and for the agreed upon approach to be followed with discipline (and enforced via code reviews) by the team.

On a couple of recent projects, the biggest problems we ran into weren't concerning which approach to leveraging DTO for layer separation was "best," but on coming to a firm layering decision and having the team discipline to adhere to those practices.  Because of these "gray" areas, which we didn't come to a firm team agreement on, there were inconsistent practices used by the developers which led to rot, particularly between the controller (or code-behind) and application services layer.

So while we can discuss which approach is best or most effective, I think the greater concern is for the team to pick a direction, to stick to it, and to diverge or refactor as a team decision.

But as I said, I don't want to leave it at "it depends." ;)  So after reading everyone's ideas here and contemplating further on past projects and the ideas I recently put forth in http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/05/better-application-services-and-cqs-with-s-arp-architecture-1-0-q3-2009.aspx, here are a couple of ideas that I've been trying to adhere to (and/or will be adhering to on my next project).  The layers referenced below may be viewed in a package diagram here for better clarity of what's discussed.

  • Usually, one object should be used to populate a form while a second should be used to collect data from a form.  The former would contain information for default/selected values along with drop-down values, for examples, while the latter would encapsulate which options were selected when the user submitted the form.  Personally, I use a <EnityName>FormViewModel to populate the form and use an entity to collect from the form...but I see it equally valid to instead use an intermediary DTO to collect from the form if further separation is warranted.  Additionally, I feel it is OK for a FormViewModel object to have references to domain objects to make it easier to transfer data to the form for input population.  This is an arguable point and would be just as valid to only pass DTOs in the view model for a cleaner separation between view and domain, with the acceptance that there will be more objects and maintenance that comes with that approach.
  • I believe that view models should be maintained either in the application services layer OR in a separate ViewModels class library if a project team feels the additional separation is warranted (although I can't envision much reason to do so).  (I'll be modifying my sample project post to reflect the former.)  Either way, they should not be maintained in .Core to keep a very clean separation from domain concerns.  While the name "ViewModel" implies that they are a view concern, they only describe what should be displayed, not how it should be displayed.  Accordingly, there is still a very clear separation of concerns between the view layer (which the user interacts with) and the view model classes.  Furthermore, the application services expose methods which should be usable by any type of client.  In line with this, a ViewModel or FormViewModel class does not impart a particular mechanism for the implementation of the view layer - again, it only describes what data should be available to the view.  So even if view model classes are maintained in app services, this does not preclude there use by various client types; e.g., they could be equally consumed by ASP.NET, ASP.NET MVC, Spark, SilverLight, etc.  (The only caveat is that if the view model classes maintain references to domain objects, then it will be more difficult to expose them for serialization via web services and WCF. Accordingly, a project team should firmly decide if ViewModels may contain references to domain objects.)
  • I believe that DTOs should be maintained in a separate assembly and have no reference to .Core or be maintained in .Core if warranted with the acceptance of decreased "shareability."  DTOs should almost always be serializable; i.e., transferable via web services and WCF.  This allows the DTOs class library to be easily shared with a consumer of web services which may expose these objects, for easier deserialization.  Some of the motivations for deciding when to use DTOs would be to provide a clean separation between the view and app services (if a project team feels its warranted), to expose data to web service consumers, and to better enable Command/Query Separation.
  • Repositories may return Entities, Value Objects or DTOs, in addition to other "basic" types; e.g., primitives, arrays, etc.  Ideally, repositories would not be used by domain objects (even via their interfaces) and would have their interfaces maintained in the application services layer.  If the repository interfaces are maintained in .Core, then this forces DTOs to be maintained in .Core as well with the possibility that domain references may "leak" into DTOs.  If the repository interfaces are maintained in .ApplicationServices, then DTOs may be maintained in a cleanly separated library without worrying about the line between DTOs and domain objects being grayed.  But data interfaces (and DTOs) may be maintained in .Core if the team feels the simplification is warranted.

With all this said, I still think the most important decision is deciding a path to take for your project, having the project team agree upon it, and maintaining the discipline to stay on course.

Your feedback on these ideas is most welcome!

Billy McCafferty

Discuss (13)

Better Application Services and CQS using S#arp Architecture 1.0 Q3 2009

Posted by Billy McCafferty, Friday, March 05, 2010 (876 views)

Updated 2010.03.09 to reflect small modifications that were decided through subsequent discussions on S#arp forum and other DDD posts.

Obviously, S#arp Architecture is the bee's knees when it comes to developing ASP.NET MVC applications. ;)  But as a project evolves and gets larger, "out of the box" S#arp Architecture 1.0 guidance runs into a few pain points.  Particularly, there's poor use of the application services layer, the separation between controllers and application services is not very clear, entity listing pages become performance bottlenecks as the domain model gets sizable, there is no command/query separation (CQS) "out of the box", and unit tests require re-occurring maintenance to deal with changes in the number of constructor parameters to controllers and application services.  While the amicable and adroit Alec Whittington (who is taking over the lead role from me on S#arp Architecture) is hard at work upgrading S#arp Architecture to accommodate recent dependency upgrades and accommodating ASP.NET MVC 2, I wanted to take a stab at resolving some of the architectural issues that I've run into, on S#arp projects over the past year.

I've developed and included a sample project, built on S#arp Architecture 1.0 Q3 2009, for the following key reasons:

  • To resolve some "pain points" that develop as S#arp projects grow to large sizes,
  • To demonstrate better use of the application services layer,
  • To demonstrate better command/query separation of the entity listing pages for dramatically better performance as the domain model grows,
  • To create an architectural spike for a new project I'm working on, and
  • To collect feedback from the S#arp community to determine if this is an appropriate architectural direction for the next release of S#arp Architecture.

Please post any feedback and/or suggestions you may have in the comments below or, more preferably, to the S#arp Architecture forums at http://groups.google.com/group/sharp-architecture.


Setup instructions

  1. Unzip the sample project to a BetterAppServices folder
  2. Create a new database called BetterAppServices
  3. Using SQL Enterprise Manager, run:
    1. /BetterAppServices/db/Schema/CreateBetterAppServicesDb_ChangesWillBeLost.sql
    2. /BetterAppServices/db/StoredProcedures/CreateGetProductCategorySummaries.sql
    3. /BetterAppServices/db/StoredProcedures/CreateGetProductSummaries.sql
  4. Open /BetterAppServices/BetterAppServices.sln with VS 2008
  5. In VS 2008, open BetterAppServices.Web/NHibernate.config and change the connection string to point to your BetterAppServices database
  6. Right click the BetterAppServices.Web project and "Set as StartUp Project"
  7. Run (F5) the project to see everything in action.

Changes from "out of the box" S#arp Architecture 1.0 Projects

This project is more of an architectural spike more than anything else at the moment. Accordingly, the CRUD scaffolding generator has been removed and non-essential unit tests have been removed to focus on the architecture itself.  Many of the changes will be incorporated into the S#arp CRUD scaffolding generator; this will either be available in the next release, or will be provided as an add-on, as this new approach does add complexity and introduces a major breaking change to existing 1.0 projects.

Major changes include:

  • /db Folder Changes
    • Added /db/Schema/CreateBetterAppServicesDb_ChangesWillBeLost.sql. This gets auto-regenerated when unit tests are run. The motivation was to have the DB schema automatically maintained while developing.
    • Added /db/StoredProcedures/CreateGetEntityNamePluralSummaries.sql. These SPs provide command/query separation for the entity listing (Index.aspx) pages, which frequently became a performance bottleneck.
  • Cross-Project Changes
    • Moved /BetterAppServices.Core/DataInterfaces/* to /BetterAppServices.ApplicationServices/DataInterfaces/. This is the only major, possible breaking change for existing applications. This was done to further remove the potential of domain objects using data repositories directly, and to allow the repositories to return objects from a new Dtos project for command/query separation, among other benefits (dicussed below).  Decided not to do this to support domain services which may require the use of repositories and to make upgrading from previous version much simpler in some cases.  Having the interfaces in .Core doesn't necessitate that domain objects use them; in fact, my rule of thumb is for all domain objects to avoid the use of repositories unless an exceptive case exists.
    • Replaced all uses of "using BetterAppServices.Core.DataInterfaces;" to "using BetterAppServices.ApplicationServices.DataInterfaces;" to support the above mentioned change.
    • Replaced all uses of IRepositoryEntityName with IEntityNameRepository. Be default, all entities now have an explicit repository. This avoids the need to manually change from the generic to the explicit, when the need arose, in unit tests and in constructors; which was frequently occurring.
  • /BetterAppServices.Core/QueryDtos (/BetterAppServices.Dtos)
    • Added a Dtos class library to provide an appropriate location for View Models and DTOs. While this project currently has a dependency on BetterAppServices.Core, so that the View Models can contain references to domain objects, this dependency could be severed for better separation between the view and the domain. The caveat is that much more work would be required maintaining a more complete DTO layer and transferring data via DTOs in all of the CRUD pages.  Decided that a separate assembly was overkill.  Consequently, added this namespace to contain query DTOs for transferring results of "report" queries into objects.  With this example project, only the listing page uses "pure" DTOs for much better performance. The other CRUD pages still communicate directly with domain objects to keep things much simpler. This is something that can be argued either way and it should depend on the project needs to determine if a more separated approach is warranted.
    • Added EntityNameDto.cs to act as a summary object to be bound to results from stored procedures (e.g., the entity listing pages), or other DTO needs.
  • /BetterAppServices.ApplicationServices/ViewModels
    • Added this namespace and EntityNameFormViewModel.cs to hold data related to adding and updating the EntityName. This object gets populated and passed to the entity form pages, accordingly.
  • /BetterAppServices.ApplicationServices
    • Added reference to BetterAppServices.Dtos so that app services can return DTOs.
    • Added EntityNameManagementService.cs. This app service class removes the CRUD logic from the controllers, makes the logic more reusable, and creates an appropriate class to add additional application service logic to.
    • Added IEntityNameManagementService.cs. This app service interface makes unit testing more maintainable because you can mock the service interface and not worry about when the concrete class' constructor arguments change.
    • Added /DataInterfaces/IEntityNameRepository.cs. As described above, every entity now has an explicit repository interface to avoid changes down the road when custom interfaces would inevitably be introduced.
  • /BetterAppServices.Data
    • Added /BetterAppServices.Data/EntityNameRepository.cs to implement the associated interface. An important thing to note is that method provided invokes a stored procedure to act as a reporting means in line with command/query separation. This is useful for entity listing pages which would frequently, previously, end up loading a huge portion of the domain model to show summary information.
    • Added /BetterAppServices.Data/NamedQuery/GetEntityNameSummaries.hbm.xml and set its compile action to "Embedded Resource." This provides the "short cut" for invoking the stored procedure. It could easily be modified to accept paging parameters.
    • Added reference to BetterAppServices.Dtos so that repositories can return DTOs for better command/query separation.
  • /BetterAppServices.Web
    • Added reference to BetterAppServices.Dtos to be able show DTOs on web pages.
    • Changed ComponentRegistrar.AddCustomRepositoriesTo "BetterAppServices.Core" to "BetterAppServices.ApplicationServices" to reflect the new location of the data repository interfaces.
  • /BetterAppServices.Web.Controllers
    • Added reference to BetterAppServices.Dtos.
  • /BetterAppServices.Tests
    • Modified MappingIntegrationTests.CanGenerateDatabaseSchema to save DB schema to /db/Schema/CreateBetterAppServicesDb_ChangesWillBeLost.sql every time the unit test is run.

To reiterate, many of the changes above can be incorporated into a CRUD scaffolding generator; the focus with this example project is on providing an archtectural spike of the proposed architectural revisions.

Even if you don't use S#arp Archtiecture, this sample project should serve as a good example of using application services and basic use of command/query separation (CQS).  Although the CQS in the sample project could be taken much further, I felt that the sample provides a good balance between practical maintainability and a more austere separation of concerns.

Enjoy!

Billy McCafferty

Discuss (4)
More Posts Next page »
CodeBetter.Com