Tell, don’t Ask

The most languages i develop in these days are OOP (Object Oriented Programming) languages and i try to use the SOLID-principles as much as possible.

The first principle in SOLID is the Single Responsibility Principle (SRP), which states that a class should have only one reason to change. In other words, it should only have one responsibility.

The “Tell, don´t ask” principle is a good rule of thumb when it comes to SRP. Rather than asking an object for data and acting on that data, we should instead tell an object what to do. This will move behavior into an object that is maintaining the data.

This makes it easier to maintain and to test.

Imagine the following code where we “ask” for data:

The MonitorThreshold class is just a dataholder with properties. To validate if a new CurrentThreshold is less or equal to MaxThreshold we need to handle that in the MonitorService.

If i need a MinThreshold property i have to modify both MonitorThreshold and the MonitorService classes, and that does not comply with the single responsibility principle. Also, as you can see, the testing of this will be very cumbersome.

Instead we “tell” the MonitorThreshold what to to. Like this.

This makes the MonitorThreshold class easy to test and it will also comply with the single responsibility principle. Adding a property of MinThreshold and use that in the validation will not affect any other class.

Custom MVC validation attribute

Finding my self in a new-old situation i needed to create a custom validation attribute.

To create an custom validation attribute we inherit from the abstract class “ValidationAttribute” in System.ComponentModel.DataAnnotations. This allows for server side validation by invoking the method “IsValid”.

I decided to implement an “MoreThan” attribute which checks that the one value in my model is more than another. For this we need the validator to compare the model property with another property.

This is how the validator class looks like.

To use it we simply add the “MoreThan” attribute to our model property with a comparison property of a string.

If we want to use the jQuery obtrusive validation we need to add an “IClientModelValidator” interface to our custom validation class. This makes the internal tag helper to know about the validation.

So how can we test if the custom validator work? Lets implement a simple xunit test where we test the validitiy and the error message we want it to show.

Now, go create your custom validators to satisfy your needs.

Create self signed certificate

Today i started a project what utilized IdentityServer and for that i needed a signed certificate. This post might not be the best explained, but more like a reminder for my self.

 

Map private fields in Entity framework core

In DDD you always want to control what is being put into your domain object. That can be done with public getters and private setters. But when it comes to collections (e.g. System.Collections.Generic.List<T>) it becomes more complicated, due to the fact the the domain object exposes an object of type List<T> which can be modified.

Continue Reading “Map private fields in Entity framework core”

Testing DBContext migrations

Today i was creating EntityFrameworkCore DBContexts and it’s a time consuming work to delete the databases, add migrations and then update the database to see what the migration scripts would generate.

I created a simple powershell-script that i ran when needed.

To find the instance pipe name of the LocalDB i used the sqllocaldb utility.

 

Shrink transactionlogs

To shrink the MS-SQL transactionlogs for emergency storage needs we can use the following syntax.

Use at your own risk.

Tablecopy with T-SQL

Today i found myself in a situation where i needed to copy some data from one table on a MS-SQL server to another.

I found the following T-SQL syntax to be very helpfull.

Use at your own risk.