Visual Studio 2013 has an awesome feature for those who like debugging. One limitation before Visual Studio 2013 was that we couldn’t see the return value of a method, neither the return value of the method that call other method. Now, with Visual Studio 2013, it’s possible to see the return value with the Debugging windows “Auto”. It’s also possible to see returning value by using the $ReturnValue keyword in the immediate windows.
If we set a breakpoint to the Method1 and we hit next, we will be at the ending curly bracket. Normally, we won’t see the value of “Method1 call ” + Method2();
Now, we can see it.
As you can see in the image above, we see in the corner left bottom the Auto Windows Panel. The first line is the value inside Method2() and the second line is the return value of Method1().
With the new version of the Microsoft .Net, the 4.5.1, you will be able to use assembly that as been compiled with an other framework version dependency. For example, a project that use a library that itself use a library that we might use in the project but with an other version won’t work normally. But, with the new feature called binding redirect, an entry inside the web.config (or app.config) is added to tell the .Net virtual machine to use for specific library the version of the dependency.
Project A --> Library A --> Dependency 1 (version1)
|---------> Dependency 2 (version2)
The example above would have an entry in the web.config (or app.config) that tell the dependency to use version 1 if used by Library A and use the version 2 when directly used by the project.
It might not be an impressive feature because it’s not visual but it’s very useful. Often, library from the .Net framework could be problematic but this is a thing of the past.
Visual Studio 2013 let you have two type of scroll bar. The first one is the same at in Visual Studio 2012 which contain additional information like where errors are located with a red rectangle and allow you to mark every keyword searched with a yellow mark. It also contain a dark brown color for break point and a blue line for the current line. The last thing it can display is what has changed.
Now, you can switch to have a visual representation of the code. This new scroll bar visualization is called the map mode.
To activate the map mode, right click on any scroll bar and select the options entry in the context menu.
This will bring the options menu for the scroll bar. At the right of the dialog window, a behavior frame contains the two available options.
Not only you can see the code but you can also if you put you mouse over the scroll bar see what the code is.
The main goal of this new feature is to allow you to move less when developing. It allows developer to not move back and forth inside a page. With the combination of the peak definition feature, the focus remains more and more into the task and not to move around files and lines.
You can define association 1 to many and defining the relationship with both code snippet below. One is defined with a string, the other one with a property.
Both mappings will create exactly the same database schema with a non nullable foreign key.
The mapping with MapKey is used when you don’t want to have the foreign key as a property in your model context class. This is called Independent Association.
The mapping with HasForeignKey when the foreign key is a property in the model. This type is called Foreign Key Association.
You can use the one you want. Personally, I prefer to use the HasForeignKey because it’s strongly mapped and if the property name is refactored that I am sure that the property will follow.
Visual Studio 2013 brings many new improvements and one of this one is the peak definition feature. This feature is available by the default shortcut alt+F12 when the caret of your mouse is on a method.
The goal of the peak definition is to display the content of the method inside the file, where the method is called. A picture worth a thousand words, so here is an example. I have clicked the AddValidationErrors and I have pressed alt+F12. What happen, is that a block with the title “controllersextension.cs” appears. It displays the code that would been called. You can close this window any time by clicking the “X” or by pressing escape.
The goal of this feature is to limit the number of file open for just watching what’s happening. It’s very fast, and allow to have a peak of what’s happening. The only limit this new tool has is that it doesn’t work well when the method is from an interface. In that case, you see the interface definition.
Few times ago, I open up a solution that was perfectly working previously and realized that this one compile but when I launch the browser raise me an exception :
No parameterless constructor defined for this object
.
Usually, this exception occur when you are using Asp.Net MVC with Unity and this one cannot resolve the parameters of your constructor. This mean that you request a controller method but the parameters aren’t provided. To solve this problem, usually people that develop with Asp.Net MVC and Unity use the Unity.MVC3 code that let you set the Unity dependency resolver to the framework that will resolve controller’s parameters when Asp.Net do a http request. The second option is to have constructor without parameter, which is often not possible.
But, in my scenario, I was using Unity.MVC3 and it was working before. The problem reside in the Web.Config file. Visual Studio has inserted an attribute that cause all the chaos of this exception.
The web.config has an attribute called “xmlns” with the value “http://schemas.microsoft.com/.NetConfiguration/v2.0” for the element configuration.
By removing the xmlns attribute (and its value) everything worked as before. No more exception “No parameterless constructor defined for this object”, no more problem. So, if you have a parameterless constructor exception when using Asp.Net MVC and Unity, check out your configuration file to be sure that this attribute is not set.
I am using since few months in most of my project the SimpleMembershipProvider instead of the old SqlMembershipProvider. SqlMembershipProvider is there for over 10 years and since Asp.Net MVC4, a new mechanism is available. This new provider comes by default with new Asp.Net MVC project and use the reference : WebMatrix.WebData.
The main goal of this new library is to simplify the use of user-password application. It also gives the possibility to extend more how to store the information. Sql Server is still behind but you can now use easily Azure storage if desired. It also simplify by a lot the number of tables and views requires in the past by SqlMembershipProvider. Now, the tables are limited to only few and you can customize their schema, the name of the tables and also fields names.
As you can see in the image above, you can add a Language field. This require only to add a property and let Entity Framework handle the rest.
[Table("UserProfile")]
public class UserProfile : ICurrentUser
{
public UserProfile()
{
this.UserName = "Anonymous";
this.Language = "en-US";
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Language { get; set; }
}
If you are using Entity Framework with the migration tool, you just need to add in your seed method a one liner.
The first parameter is the Entity Framework connection string to your database, the second is the table name for the user then the column id for the user following by the name that is required when we logon.
If you need to have default role and user in your seed you can add them by using the Api.
protected override void Seed(DatabaseContext context)
{
base.Seed(context);
WebSecurity.InitializeDatabaseConnection(
"DefaultConnection",
"UserProfile",
"UserId",
"UserName", autoCreateTables: true);
if (!Roles.RoleExists(Model.Roles.ADMINISTRATOR))
Roles.CreateRole(Model.Roles.ADMINISTRATOR);
if (!Roles.RoleExists(Model.Roles.NORMAL))
Roles.CreateRole(Model.Roles.NORMAL);
if (!WebSecurity.UserExists("123123"))
WebSecurity.CreateUserAndAccount("123123", "123123", new { Email="123123@123.com", Language="fr-CA"});
if (!WebSecurity.UserExists("qweqwe"))
WebSecurity.CreateUserAndAccount("qweqwe", "qweqwe", new { Email = "qweqwe@qwe.com", Language = "en-US" });
if (!((IList<string>)Roles.GetRolesForUser("123123")).Contains(Model.Roles.ADMINISTRATOR))
Roles.AddUsersToRoles(new[] { "123123", "qweqwe" }, new[] { Model.Roles.ADMINISTRATOR });
if (!((IList<string>)Roles.GetRolesForUser("qweqwe")).Contains(Model.Roles.NORMAL))
Roles.AddUsersToRoles(new[] { "qweqwe" }, new[] { Model.Roles.NORMAL });
}
Microsoft has the ability to fake method but as the disadvantage that it requires to be from an interface or to be virtual. This is the case of most testing framework. Nevertheless, Microsoft has the principle of Shim which allow to replace the method behavior by yours for the time of a test. You can get additional information on MSDN. This blog article explains briefly how to use shim.
For the purpose of this article, let say that we have a library with a single class which contain a single method :
namespace ShimpProjectClassesToShim
{
public class MyClass
{
public string MyMethod(string variable)
{
return "From MyClass = " + variable;
}
}
public class MyClass2
{
public string ToTest()
{
var m = new MyClass();
return m.MyMethod("InsideMyClass2");
}
}
}
This method is not virtual, but could have been because shim can be bound to static method. As you can see, the purpose of the test would be to change the inside of the method for the time of a test method.
To continue with our example, we need a second project, the one of the test. This one will have a reference to the library previously created. Once done, right click the reference and click “Add Fake Assembly“.
Do no worry, you have already added the reference from the unit project to the library project. Maybe a better naming of the action should have been “Transform to Fake Assembly. When done, you will see a Fakes folder created with the fake reference.
If you are curious about what contain this file, here is the one of the example :
So the test will consist of shimming MyClass that is used by MyClass2. This is a common scenario that you want to test a method that use an other class that you do not want to test. In our case, we want to test MyClass2 and not MyClass, we shim MyClass to have a known return value.
The test my be inside the scope of a ShimContext to be able to set the shim inside the test.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
//Shim
using (ShimsContext.Create())
{
//Arrange
ShimMyClass.AllInstances.MyMethodString = (theClass, s) => "FAKE " + s;
var classToUnitTest = new MyClass2();
//Act
string result = classToUnitTest.ToTest();
//Assert
Assert.AreEqual("FAKE InsideMyClass2", result);
}
}
}
The arrange section set for all instances of the method the desired return value. As you can see we use “ShimMyClass” class to set all instances. In your case, if you class is called “XYZ”, you will have to use “ShimXYZ”. This is by design that a class has been created for you with the prefix Shim. From there, you will be able to set return value for all methods. The rest of the unit test is as usual.