Here you can find my private notes about programming that I wanted to share with you. Feel free to filter based on the topic you want or search for something specific.
How to solve the "project.assets.json not found. Run a NuGet package restore to generate this file" problem in .NET Core applications when using MSBuild
While I was developing a .NET Core web application and wanted to automate the build process in VSTS, I got the following error, when I was triggering the build:
Error: Assets file ...\project.assets.json not found. Run a NuGet package restore to generate this file. Process 'msbuild.exe' exited with code 1.
The issue here is that needed .NET Core files are missing, when we start build our application. I solved this problem by defined an extra initial step in my build process.
Write a custom .NET attribute to mark searchable columns of a HTML table and match them with their equivalent DB columns
Consider you have to implement the following requirement:
We have a MVC .NET web application and a view with a HTML table and multiple columns. We want to mark some of these columns as searchable, so that we can search their values when we use the search field.
We host our data in a relational database, we get the structure of the DB table as it is. We use Entity Framework and we need to “map” the DB columns to their equivalent UI columns, so that the search works correctly.
The It.Is and It.IsAny methods of the Moq unit-testing framework and why your "partial mocking" might do not work
Unit-testing of existing code can sometimes be a challenge, since you have to deal with classes that contain too much of functionality. In that case the “Single Responsibility” principle was not in focus, however, refactoring the code can be an issue, so you will have to unit-test the class as it is. Apart from that we do not want to “mock” the Interface of a class, which would mock all its method, but the class it self.
Such classes are calling methods of the same class, so the task is to mock only few of the methods, but not all of them, since we also want to unit-test some of them. The feature of partially mocking the methods of a class is called partial mocking. The methods that we want to mock, have to be defined as virtual, so that Moq can ovveride them. This feature can be a sign of code smell, when overused.
There are some great examples in StackOverflow on how to achieve that, however, in this article I would like to focus on an issue that caused my partial mocks not to work and show you how I fixed it.
Define a custom HTML prefix for your .NET MVC models
There might be the case when you develop with .NET MVC, that you have to render a complex view which contains a hierarchy of partial views.
In such times you might want to render more than one instances of a ViewModel inside the view or you just want to use different ViewModels, like one from your framework and one from your partial view, that have the same name.
In order to avoid collisions of names of the properties between the models, you can use the HTMLFieldPrefix property which adds a prefix to the CSS id and name attributes.
Let us consider the ViewModel class in the following example:
A very possible reason as to why your ASP.NET MVC ViewModel's IEnumerable Validate method is not firing
If you are dealing with an ASP.NET MVC project chances are that your ViewModels or POCO classes are implementing the Validate method of the IValidatableObject interface to validate on the server side the incoming data from the UI of your application. Chances are also that you are decorating the properties of these classes with validation attributes like [Required].
As stated here when you use both validation attributes and the Validate method, the property attributes are going to be applied first, if they pass the object attributes are checked and if they also pass the Validate method of the current class will run and validate the input.
However, what happens when you use no attributes in the property or object level and you only implement the Validate method? Why is the method not firing?