Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Friday, January 19, 2018

CommonAssemblyInfo

I'm writing this post since I posted this on Twitter, and within half an hour, was asked to share my knowledge:


In Visual Studio, there's a little known feature that lets you add files as links to a project. Using this trick, we can also ease maintenance slightly by having a CommonAssemblyInfo.cs file where common attributes across the assemblies in a solution can be applied, but maintained in a single file.

Here is the sample demo application to show the steps one normally goes through. We have an existing command line utility to calculate tax based on an entered amount and tax rate. Quite simple.


In the Properties folder there is an AssemblyInfo.cs file listing the various attributes applied to the assembly.


This isn't always set up as expected, because most people forget to look at this file. One thing that stands out for example is the Company and Copyright information is defaulted sometimes to Microsoft or some other vendor based on the system settings. This may be undesirable, and so we can update these settings.


See some links at the bottom of this page for more info on these attributes.



You can see these are shown in the properties on the executable that is built.

Maybe we go back to look at our application and want to separate out the domain aspects of the console application, perhaps because in future we may want to share that with a web application frontend. The first step one would do is creating a new project and moving the classes around, fixing references, etc.


But you'll notice that most of these attributes are the same. The only two that maybe would be different are actually just the title of the assembly and the Guid it uses. If we rename things someday, we'd have to update at least two files (maybe we rename CommonAssemblyInfoDemo to something more useful, for example).

The first thing I usually do when creating a solution with more than one project, is first creating a file called CommonAssemblyInfo.cs that lives in the same directory as the solution file (CommonAssemblyInfoDemo.sln). This is usually done outside of Visual Studio, and then using Visual Studio to add the existing item to the solution, to make it easier to edit. I also setup solution folders that are numbered to keep things nicely organised.


You'll notice that only the common attributes are left in this file. But we still need to incorporate it into the other projects.

To do this, we also need to add it as an existing item to each project, however, there is a trick - we need to add it as a link.




You'll notice in the Solution Explorer that the icon of the file has a link symbol in it, to indicate it's just a reference to an existing file, so that all links refer to the same file.

Now we just update each projects AssemblyInfo to just include the information related specifically to that project.


If we need to update the AssemblyVersion across the entire solution, it's now a simple matter of just updating the CommonAssemblyInfo file to do this, instead of going through each project and remembering to update each one.

Hope this helped someone, the extra effort when setting up a new project can sometimes save time in the future.

For example, if we had a continuous integration build, and we just wanted to update the version number across all these artifacts based on some number set in our CI system, we could just update the CommonAssemblyInfo file before we run the solution through the build.

Links




Thursday, July 7, 2016

Updating NuGet extension on Visual Studio 2015

NuGet is pretty awesome, though for some reason the NuGet Package Manager for Visual Studio 2015 extension was re-done and every time I try to update it I get an error, and then the extension seems to not work when restarting Visual Studio.
Took quite a while to figure out why, but it seems that it's to do with not being able to uninstall the old version of the extension. Extensions are installed into the folder C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions (or similar depending on the VS version). Certain folders in this path relate to an extension per folder, those that look like randomly generated names specifically, as below:
404 - Image Not Found
Each time the NuGet extension tried to update, it would fail to remove the old installation, and then create a new folder for the 'upgrade'. If you tried to upgrade it a few times, you may have quite a few folders that contain the NuGet extension. When you restart VS it seems to have issues loading the extension because of this. Go through each of these to find those folders, e.g.:
404 - Image Not Found
Close Visual Studio, and then delete each of those folders specific to the NuGet extension. Then manually download the VSIX extension installer from the website, and install it outside of Visual Studio. Once complete, start up Visual Studio and the extension should be installed properly. Whether it works or not is another thing, it seems the GUI is quite buggy with this extension, so I prefer using the Package Manager Console.
Hope this helps someone, I've had to come back to doing this a few times, so it'll definitely help me in future I'm sure :D

Tuesday, July 21, 2015

Visual Studio tip: Hide selected text

Today I noticed the menu item Edit > Outlining > Stop Hiding Current (CTRL+M CTRL+U), and wondered to myself what it does. Well, turns out you can hide selected areas of text and then later unhide them. But how do you hide them in the first place??
"The option to hide a selection only shows in the Outlining menu once you've selected text" - Captain Obvious
Thank you Captain Obvious!
So as an example, lets say we have one of the controllers for a sample ASP.NET MVC project, and just want to inspect it to see what it does, we see the below action with a long bunch of lines:
404 - Image Not Found
We go through the first few lines and realise that's all just code related to validation, followed by loading up an item and some more validation. We're not really interested in the validation just yet so we can just collapse it by selecting those lines, and going Edit > Outlining > Hide Selection (or if like me you like keyboard shortcuts, CTRL+M followed by CTRL+H). We now can see what the method's actually trying to do, and can easily ignore the validation code for now:
404 - Image Not Found
404 - Image Not Found
If later you do want to see the folded stuff, it's as easy as expanding the code again. It seems that VS remembers this custom code folding section for later on when I close and open the file, and even after closing VS itself (though I wouldn't rely on it to be there forever... I'm guessing it's stored in some local user settings file).
I've seen people use the #region directive a lot for this kind of thing - making the code easier to read - but then someone else who doesn't like #regions will delete it later, or someone will restructure it. The advantage of doing this is that it doesn't affect anyone else on your team.
To remove the custom code folded section, just do the opposite, Edit > Outlining > Stop Hiding Current (CTRL+M CTRL+U) ... H for hide, U for unhide.
Another use case for this would be doing code reviews, or maybe if you're presenting pieces of code that you don't want the audience to read and get too far ahead of the presentation.

Friday, December 12, 2014

RequireJS and TypeScript

Here is an example of a RequireJS module:
1
define([], function() {
2
    function Calculator() {
3
    }
4
 
5
    Calculator.prototype.calculate = function (x, y) {
6
        return x + y;
7
    };
8
 
9
    return Calculator;
10
});
To make sure this is compiling through the TypeScript compiler, the first step is to convert it to use modules from TypeScript. The simplest way is:
1
function Calculator() {
2
}
3
 
4
Calculator.prototype.calculate = function (x, y) {
5
  return x + y;
6
};
7
 
8
export = Calculator;
To compile this, you then call the compiler with the right module flag, which is AMD for RequireJS:
1
tsc Calculator.ts --module amd
This will then generate the equivalent JavaScript. You'll notice it adds some extra code that's probably unneccesary, but hey, I'll take that for compile time safety:
1
define(["require", "exports"], function(require, exports) {
2
    function Calculator() {
3
    }
4
 
5
    Calculator.prototype.calculate = function (x, y) {
6
        return x + y;
7
    };
8
 
9
    return Calculator;
10
});
If you're working in Visual Studio, you might get an error about it not using the module flag. If you're working with an existing codebase, you might find you'll need to add TypeScript support first - adding a new TypeScript file in VS2013 does this by adding some stuff to the CSPROJ file, namely the XML element TypeScriptToolsVersion, and will need to reload the project. Afterwards, there should be a new tab for TypeScript Build options in the project properties dialog where you can choose the module style:
404 - Image Not Found

Tuesday, February 19, 2013

LESS is more

If you're a Visual Studio web developer and you haven't tried out the Web Essentials extension by Mads Kristensen, then GO NOW, GET IT!

And for those who do know about it, and you were using it for it's LESS support, then you may have noticed in the last update how it was removed :( The only alternative then would appear to have been T4CSS.

But the same day I updated and saw the lack of LESS support, was the same day that I realised it's now baked into Visual Studio in the most recent ASP.NET and Web Tools 2012.2 Update.

It supports adding LESS files from the New Item dialog, and upon build will generate the CSS and minified CSS for it too. Which is obviously the reason it was removed from Web Essentials. Awesome :)

Wednesday, February 22, 2012

A look at ASP.NET MVC 4

I just watched the talk by Scott Guthrie at Techdays 2012 in the Netherlands entitled "A look at ASP.NET MVC 4", see the video at the bottom of this post if you want.

In the talk, Scott talks about some of the new features in ASP.NET 4, as well as touching on some new features of Entity Framework Code First. The highlights of the features are:

  • Database Migrations
  • Bundling/Minification Support
  • Web APIs
  • Mobile Web
  • Real Time Communication (SignalR)
  • Asynchronous Support using language features (async and await)

An extremely useful addition to EF Code First is that of database migrations, allowing you to progressively develop your code and database. Migrations allow you to deploy/rollback different versions of your database. Each migration can add/remove its parts to the database (e.g. adding a column when migrating up, or removing the column when migrating down, even perhaps extracting data to a temp table and processing it during a possibly destructive migration). The one project I was on had a whole custom written database patching/versioning framework which enabled true integration testing, as well as generation of deployment scripts, which EF Code First with migrations can probably provide out of the box now. Very nice.

The bundling and minification support is a welcome addition too. By convention, instead of referencing specific scripts or CSS, if you reference a folder, all the relevant resources in that folder will be bundled and processed together. An HTML helper is also available which also provides versioning of the bundles by appending a hash of the resources to the query string. Custom bundles can be defined and custom processors can be implemented as well, for example, in the talk Scott illustrates that you could use CoffeeScript and LESS processors in a bundle, greatly improving a web developers life.

The WCF Web API, is now part of ASP.NET and is now known as the ASP.NET Web API. It provides the power of WCF with the ease of ASP.NET MVC, while respecting the HTTP protocol a lot more. It provides built in support for writing code once and supporting multiple response types (JSON/XML), OData for querying, filtering and sorting data by just adding to the query string (no code change... as long as your code supports returning an IQueryable), and also provides a nicer programming model for HTTP responses.

The default MVC project templates come with CSS that uses media queries for a more adaptive feel to the application. And for scenarios where media queries aren't enough, there's also support for detecting a mobile client, and returning a completely different template or view for a request. This allows for creating a single web application, but catering for a wider range of clients.

The Real Time Communication with SignalR allows for the server to push through to the clients. SignalR can detect if the client supports WebSockets, or if not falls back to various methods that enable the expected behaviour, e.g. long polling.

The Async support just leverages existing asynchronous controller functionality, but allows you to do so using the async and await keywords that are part of the next version of .NET.

I highly suggest that you watch the whole video, as always, the Gu's talk is full of useful information:

Tuesday, May 10, 2011

Visual Studio Clipboard Ring

Today I found out about the Visual Studio Clipboard Ring, which keeps the last 15 entries that you have entered into the clipboard using copy and cut (CTRL+C, CTRL+V, SHIFT+DEL), and lets you paste into the Visual Studio Editor an item from that list.

You just press CTRL+SHIFT+V when pasting, and each successive press will cycle through the entries. Very useful if you kept something on the clipboard, but then pressed SHIFT+DEL to delete a line, forgetting about what you wanted to paste! Which I tend to do a lot!