This developer's chronicles

Web development using Scrum, C#, AngularJS and the like

Month: February 2015 (page 1 of 2)

View project dependencies with Resharper

Today while learning about Dependency Injection I found out a very neat way to view project dependencies with Resharper.

Using Resharper

For the developers who aren’t familiar with Resharper it is an awesome Visual Studio extension that will help you code faster and it will suggest code improvements as well as tons of shortcuts, navigation features, code refactoring, code smells, etc, etc (I even met a Senior developer who wouldn’t help me with my code unless I installed Resharper on my machine)

Ok, back to the project dependency analysis !

Resharper can build a dependency graph based on the references found on each of your projects, this can be a huge advantage as your solution grows or if you’d like to have a bird’s eye view of the current state of the project and whether projects depend on (or have references to) projects that they shouldn’t.

The next project dependency graph was taken after following an Introduction to Dependency Injection (DI) using the Onion Architecture by Adam Stephensen

Resharper project dependency graph


From the above we can see that there are no circular references in our solution and that there are not unintended references either.

Resharper will also let you visualize transitive dependencies or coupling between projects amongst other useful features.

To visualize your solution dependencies go to Resharper -> Architecture -> View Project Dependencies.

Using Visual Studio

Visual Studio (Premium or Ultimate editions) also bring dependency graphs to the table, the following is a dependency graph for a similar type of project (using the Onion Architecture)

Visual studio dependency graph


For more comprehensive information about these architectural tools be sure to read Adam Stephensen’s blogpost as well as Channel’s 9 video on Understanding your code dependencies through visualization

What other tools do you use for identifying code dependencies?

Using $scope.$apply in AngularJS

In order to understand what $scope.$apply() gives us and why we would need it  is necessary to take a step back and look at they way AngularJS checks if there has been any changes to its model or its views so that it can update its bindings (and the whole process that goes behind it).

Digest cycle

In AngularJS there is a concept of digest cycle, which is pretty much a loop in which AngularJS checks for any changes to its bindings plus anything else that is being watched and lives inside its $scope (note that not everything that lives inside the $scope is being watched by Angular, only variables used in expressions such as  <div>{{message}}</div>and also when $scope.$watch is being used.)

Once a watch is fired Angular starts a new cycle ( $digest cycle) in which it checks all current variables and compare them with their previous values, if it finds any change it will then update the correspondent bindings,  if it doesn’t it will then wait until the next watch fires.

Why do I need $scope.$apply() then?

Angular only monitors variables used in expressions and anything inside of a $watch living inside the $scope. So if you are changing the model outside of the Angular context you will need then to call $scope.$apply() for those changes to be propagated, otherwise Angular will not know that they have been changed thus the binding will not be updated.

Let’s look at the below example were I’m changing the model from outside the $scope therefore any changes that I make are not visible

And this is the code in my view:

The above JavaScript code will update the model two seconds after the page has been loaded, but as this is not being watched by AngularJS (the $scope.message is but not the timeout function) no changes will be visible on the screen

So if we want the changes to be propagated we’ll need to call the $scope.$apply():


If you are changing your model outside of your AngularJS context (or the variable that you want isn’t being watched) then you will need to use $scope.$apply in AngularJS as a way of telling it that it will need to fire a new $digest cycle so that it can detect your changes.

What is the difference between IEnumerable and IQueryable?

IEnumerable and IQueryable may seem like the same at a quick glance, but there are important differences that us, as developers must be aware so that we not only get a working product out the door but also something that performs well.

What is IEnumerable?

Simply put, is something that let’s you go from one element of your collection over to the next (an interface that will provide you with an iterator that moves forward), an example of how you can declare and use IEnumerable is as follow:

As long as you are using a type that implements IEnumerable you will have the advantage of being able to iterate over your collection.

When can I use IEnumerable?

  • When you want to iterate over a collection (List, Arrays, etc.)
  • When you want to get all the results from your query and apply any further filtering once your collection has been loaded in memory.
  •  When you are using LINQ to XML or LINQ to Object.

What is IQueryable?

IQueryable is an interface that implements IEnumerable (and because it implements IEnumerable it means that it allows you to iterate over a collection plus some more)

IQueryable takes Expression objects instead of delegates (which is what IEnumerable takes)

When can I use IQueryable?

  •  When you want to iterate over a collection (same as IEnumerable)
  • When you want to work with data from out-memory, such as a database, a web service etc
  • When you want to apply a filter to your query BEFORE the query is executed (this can save you heaps of resources)
  • When Lazy loading is needed (you only get what you need, not the entire list of records)

So… What’s is the main difference between IEnumerable and IQueryable ?

The main difference is that with IEnumerable you get all the records at once while with IQueryable you only get the records that you want, let’s see that in action with an example:

Let’s assume that you have a client table with all your client’s information (approximately 1000 clients)

  • If you use IEnumerable to get the first 5 customers you end up loading all 1000 and then selecting the first 5.
  • With IQueryable you only select the first 5 (saving a lot of resources !)

For some other useful resources you can check out some stack overflow answers and this dotnet-trick’s article on IEnumerable vs IQueryable


Thank you for reading, I hope you have now a clearer idea of IEnumerable and IQueryable and when each of them can be used best.

Understanding delegates and when to use them in C# ASP.NET

This blog post is aimed at beginner developers or someone who have just not fully grasped delegates or don’t really understand what problem do they solve and a good example of when they can be used.

What is actually a delegate?

A delegate is a method signature (pretty much a reference to a method) hence it will have to match the signature of the method it wants to reference, an example below:

Let’s say we have a method called MergeCustomerDetails which takes a couple of strings and returns a string, pretty simple:

Now, if we want to use a delegate to call that method we need to create a delegate whose signature matches the signature of the method (that means that the return type and the parameter types must be the same):

If you see the delegate definition above, it matches the MergeCustomerDetails’ method signature (both return a string, and both take 2 arguments of type string). parameter names don’t have to match though (I’ve used different parameter names to highlight that)

The next step is to initialize the delegate and ‘point’ it to the require method:

So instead of calling MergeCustomerDetails we can call our delegate!

When can I use delegates?

We can use delegates whenever we need to pass methods as parameters to another functions so that they can be executed at runtime from there (although a bit complex to grasp an example may be the best way to understand it).

I found the following example in Stack Overflow where there is an excellent use for delegates, which will be explained below. Let’s see the problem and how delegates helped solve it.

We have three functions with the exact same signature (IsChild, IsAdult and IsSenior), if we wanted to execute any of those methods from inside another method we would have to do it manually as there is no way to pass a method as parameter to another method (without using delegate, that is). That would mean writing almost the same code again and again every time we want to call one of the methods.

Fortunately with the use of delegates we can ‘point’ them to those methods and then pass them as parameters.

Let’s start by defining a Person class and the three methods (note that they all have the same method signature):

Now  let’s define a delegate to see how it matches the methods’ signature:

And the method that will take a delegate as parameter:

Finally let’s add the code to the Main function which will call the ‘DisplayPeople’ method passing all the different methods (IsChild, IsAdult and IsSenior)


Thanks, any feedback is appreciated !

Bundling and Minification in ASP.NET MVC

Bundling and Minification are two tools available since  ASP.NET 4 that will make your application load faster as they reduce the number of files to be downloaded (bundling) as well as removing all white spaces / comments and shortening the variable names (when possible) so that file sizes get smaller.

This is specially important as when a page is loaded most browsers will limit the amount of resources that are retrieved down to 6 (so it retrieves maximum 6 files simultaneously) so by reducing the number of files to be downloaded we can guarantee a faster load time (and we all love that, don’t we?)

Let’s see what a page load looks like in chrome when no bundling or minification occurs:

bundling and minification ASP.NET MVC

As expected chrome will only download a maximum of 6 resources at the time  and anything else is put into a queue and it will be downloaded once one of the 6 files is completed. in the above example we can see how some .js files had to wait for previous files to be finished downloading.

And to make it worse, as the number of files grow in your application this will cost your visitors valuable time waiting for all files to be downloaded (even worse they may not even come back!).

How can I speed up my website load?

One of the first steps to optimize your website is to set up bundling and minification, fortunately this is really easy in .NET.

Before we continue there are a couple of files that will have to be modified, as follow:

  • Web.config: this is the file where most of the configuration is done for a web site/application
  • Bundle.config: Found in the App_Start folder (web applications)

The first thing you’ll need to do is enable bundle and minification, this can be done by setting the value of the compilation element to true in your web.config file

Alternatively you can specify the Bundle.EnableOptimizations property to true inside the Bundle.Config file (which takes precedence over web.config file):

Once bundling and minification has been set up we now need to tell the ASP.NET framework which files we want bundled and minified, very similar to the above code but now adding the to-be bundled/minified files:

The above will bundle the specified 12 files into one and will minimize it was well (awesome !)

Lastly we’ll need to add a reference to it on the page (master page / layout) where we want those scripts to be rendered:

Now let’s run the application and open up the developer tools (F12) to check out the new bundles:

bundling and minification enabled ASP.NET MVC

Neat !, now we have it bundled, one a closer inspection (clicking on the bundled file) we can see what a minified file looks like:

ASP.NET minified javascript file

It is important to note that you would only want to do this on your production site, as you don’t want to debug your code on a minified file, ewww


Older posts