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 […]
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 […]
Angular RouteReuseStrategy
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
param ( [Parameter(Mandatory=$true)][string]$subject = "My SelfSigned Certificate", [string]$outputPath = (Get-Item -Path ".\" -Verbose).FullName, [Parameter(Mandatory=$true)][string]$password #[string]$password = $( Read-Host "Input password, please" ) ) $securePassword = ConvertTo-SecureString -String $password -Force -AsPlainText $filename = ($subject.Replace(" ", "_") + ".pfx").ToLower(); New-SelfSignedCertificate -NotAfter 2030-12-31 -certstorelocation cert:\localmachine\my -dnsname $subject | out-null $loc = Get-Location Set-Location Cert:\LocalMachine\my $createdCert = Get-ChildItem | where { $_.Subject -eq "CN=$subject" } Set-Location $loc; $thumbprint = $createdCert.Thumbprint write-host "Creating cert '$subject'. Exporting to '$outputPath\$filename'."; Export-PfxCertificate -cert cert:\localMachine\my\$thumbprint -FilePath $outputPath\$filename -Password $securePassword | Out-Null write-host "Done!" |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Write-Host "Deleting the migrations folder..." Remove-Item "Migrations" -Force -Recurse Write-Host "Dropping database ASPD.." sqlcmd -E -S "np:\\.\pipe\LOCALDB#3786839C\tsql\query" -Q "use master; ALTER DATABASE [ASPD] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;DROP DATABASE [ASPD];" Write-Host "Adding initial create for contexts.." add-migration InitialCreate -Context "CustomerContext" -OutputDir "Migrations/Customers" add-migration InitialCreate -Context "ProductContext" -OutputDir "Migrations/Products" add-migration InitialCreate -Context "PartContext" -OutputDir "Migrations/Parts" Write-Host "Updating database for each context.." update-database -Context "CustomerContext" update-database -Context "ProductContext" update-database -Context "PartContext" |
To find the instance pipe name of the LocalDB i used the sqllocaldb […]
State pattern in C#
The state design patterns is a behavioral pattern that implements a state machine in an object-oriented way. It allows an object/class to alter its behavior when its internal state changes. Lets say that we have a competition application that allows clients to register score, but only when the competition is open. A non state pattern design […]
Kill Linux processes
We have an old messy thirdparty application at work that uses Oracle. Sometimes the application tends to lock the Oracle system and use a LOT of threads, leaving the system pretty much unresponsive. To be able to kill all 1000+ Oracle-plus processes i created a small script that kills all processes.
Shrink transactionlogs
To shrink the MS-SQL transactionlogs for emergency storage needs we can use the following syntax.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
USE MYDB; GO -- Truncate the log by changing the database recovery model to SIMPLE. ALTER DATABASE MYDB SET RECOVERY SIMPLE; GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (MYDB_Log, 1); GO -- Reset the database recovery model. ALTER DATABASE MYDB SET RECOVERY FULL; GO |
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.
1 2 3 4 5 6 7 8 9 10 11 |
UPDATE DST SET DST.Title = SRC.Title, DST.ContractNumber = SRC.ContractNumber, DST.ContractDescription = SRC.ContractDescription FROM Contracts AS DST INNER JOIN [SERVERNAME].[DBNAME].DBO.Contracts AS SRC ON DST.Id = SRC.Id where SRC.Id > 500 |
Use at your own risk.