Automatically converting PCL to .NET Standard 2.0 project

Mar-23-2018 01-41-34

Every time you create a new Xamarin.Forms project  in Visual Studio for Mac you have to manually convert it to .NET Standard. The conversion is very straightforward and can be done with just a few steps:

Hopefully one day VS team will take care of it, till then, I decided to automate this process and created an add-in/extension for VS for Mac – Mutatio.

Mutatio – in Latin means change, transformation or exchange.

Mutatio can convert newly created or existing projects. Please keep in mind that there might be NuGet packages that does not support .NET Standard 2.0, in this case you may see related exceptions.

In case you change your mind and you want to rollback, Mutatio is making a backup of all the files it modifying and deleting under the project’s root directory within mutatio_backup folder. So all you have to do is to copy the files back to your project and reload the solution.

One of the biggest challenges I met while development was related to reloading the project after conversion. Within VS for Mac after manually modifying the *.csproj under the right click menu of the project there will appear a Reload option, however I didn’t find a way to call this method programatically. Currently, the whole solution will be reloaded as a workaround. If you know how to solve the problem programatically I would really appreciate if you will share your knowledge by contributing or leaving a comment.

More details can be found on GitHub.


Reducing the amount of code by switching to .NET Standard

What can feel better than writing a code? Of course deleting it!

There are many good technical articles that explain why .NET Standard is great and why should we use it. My goal in this article is to demonstrate some benefits in practice.

If you didn’t switch yet from PCL and Shared Projects to .NET Standard, I would highly recommend to do it. Most of the third party libraries are compatible and the switch is usually painless. After switching to .NET Standard you could benefit from reducing the code size and the amount of “hacks”. Let’s take a look on few practical examples. Continue reading “Reducing the amount of code by switching to .NET Standard”

Quickly delete “bin”, “obj” and “packages” directories under a .NET solution

Recently had to update some old Xamarin.Forms project to the latest and greatest XF and very quickly I realised that it is not going to be an easy task, since I had to manually manipulate the csproj files to remove the old nuget dependencies. I found myself going thru multiple projects multiple times in order to delete the “bin”, “obj” & “packages” directories to fix the miscellaneous build errors and I came up with a very simple script to recursively delete delete the “bin”, “obj” & “packages” directories:

Backup your code before using this script, I am not responsible for any data loss. Please use it wisely.

To use this script: open a terminal in your solution’s root directory and copy paste the script above. Keep in mind that “bin” & “obj” directories will be regenerated after the next build, however “packages” directory will appear again only after restoring nuget packages for the solution.

Android emulators stopped working on macOS 10.13 (High Sierra)

Recently I updated my macOS to High Sierra however this broke my android emulators. Non of them started and logs showed something like:

Failed to open vm 3
Failed to create HAX VM
No accelerator found.
failed to initialize HAX: Invalid argument

As the logs states there is a problem with HAXM, and to be more specific a time for update has come. To solve the issue simply download the latest and greatest version of HAXM and restart your mac.

Entity Framework DB First and Computed Columns

Imagine you have a lovely DB first approach in place. There is a table “Fruits” with a column “WikiLink”. This column is getting populated on insert, using a trigger on the DB level. Can you spot “the” problem here?

Let’s try to understand how EF returns the DB generated entity Id from the DB right after the insert. The code below demonstrates a standard way to insert a new entity with EF:

var fruit = new Fruit { Family = “Rosaceae”, Name = “Apple” };
await dbContext.SaveChangesAsync();
// fruit.Id is auto-generated on the DB level and accessible after insert
// fruid.WikiLink is null

The Id column marked as identity, therefor it is recognised as a value generated on DB level. If you will use a DB profiler you will actually see:

— T-SQL Pseudocode for -> dbContext.Fruits.Add(fruit);
INSERT INTO [dbo].[Fruits] (Family, Name)
VALUES (“Rosaceae”, “Apple”)

It is insert and select in one query. Now back to our problem with ‘WikiLink’. What we would like to achieve is:

INSERT INTO [dbo].[Fruits] (Family, Name)
VALUES (“Rosaceae”, “Apple”)

When you use a code first approach, you have an option of explicitly marking an entity’s property as db generated with an attribute:

public string WikiLink { get; set; }

However, with DB first approach you don’t have that option, and since there is a trigger behind this column, there is no way for EF to understand that it is generated on the DB level.

Unfortunately, it is not easily solvable with DB first approach. There are a couple of workarounds we could think about:

  1. We can try to manipulate “manually” the auto-generated edmx file and set the “WikiLink” to db computed. Which is a bad idea, because this change will get lost with the next model update as it is being regenerated on each change.
  2. Another option will be to query the DB right after the insert, to get the full row data:

var fruit = new Fruit { Family = “Rosaceae”, Name = “Apple” };
await dbContext.SaveChangesAsync();
await dbContext.Entry(fruit).ReloadAsync()
// fruid.WikiLink is not null anymore

The disadvantage is obvious – additional db query:

— T-SQL Pseudocode for -> dbContext.Fruits.Add(fruit);
INSERT INTO [dbo].[Fruits] (Family, Name)
VALUES (“Rosaceae”, “Apple”)

— T-SQL Pseudocode for -> await dbContext.Entry(fruit).ReloadAsync()
SELECT FROM * [dbo].[Fruits]
WHERE Id = 99;

However, it could be good enough for some projects.

If you can modify the DB scheme, the solution is quite simple – replace the trigger by a computed column:

— T-SQL Pseudocode for
alter table [dbo].[Fruits]
add [WikiLink] as concat(N’_ttp://’, Id )

This way you may gain more performance. Since you may have a ‘RowVersion’ column which will be calculated twice because of the trigger. Not to mention that the DB structure is much more readable, compare to unnecessary trigger.

There might be another workarounds for this issue if you don’t have access or you can’t modify the DB. I would like to hear about your experience, so please share via comments.

Web.API 2 centralised model state validation

Model state validation is easy with DataAnnotations. However, you will find repeating the next line of code very soon:

if(!ModelState.IsValid) return BadRequest(..);

Luckily, there is a pretty simple solution – to use ActionFilter:

All you have to do is to add the ValidateModel attribute to your controller methods that require model state validation.