How to emit compiler warnings and errors in C# .NET

In this post we saw how to use the “if” preprocessor in Visual Studio to “communicate” with the compiler. Here’s a reminder of the example code which we’ll re-use here:

private static void TryPreprocessors()
# if DEBUG
	Console.WriteLine("You are running the Debug build");
# elif RELEASE
	Console.WriteLine("You are running the Release build");
	Console.WriteLine("This is some other build.");
# endif

In this post we’ll look at two more preprocessor types: warning and error. If you compile a project you can get one or more errors or warnings:

Read more of this post

How to declare natural ordering by implementing the generic IComparer interface in C# .NET

In this post we showed how to declare natural ordering for a custom type by implementing the generic IComparable interface. We saw that it required us to implement the CompareTo method. The example we looked at was a simple Triangle class where we said that triangles can be ordered based on their areas. That’s probably a reasonable comparison for triangle.

However, what about the following object?

public class Building
	public double Area { get; set; }
	public int NumberOfRooms { get; set; }
	public string Address { get; set; }
	public bool ForSale { get; set; }
	public DateTime DateBuilt { get; set; }

Read more of this post

Introduction to ASP.NET Core part 25: user management cont’d


In the previous post we started looking into user management in .NET Core MVC. .NET Core provides a number of built-in objects for this purpose: so far we’ve seen IdentityUser to represent the properties of a typical application user, IdentityRole to represent roles and the generic IdentityDbContext of T class to easily connect the application with an SQL Server based identity provider. The built in objects can be customised by deriving a class from them like we did with our UserManagementDbContext and User classes. We also saw how to install and register the EF based identity provider in Startup.cs via the Configure and ConfigureServices methods. We keep the user management related elements separate from the bookstore domain so that users, claims, roles etc. do not intermingle with our core domain.

Currently our demo application doesn’t do anything with users. Any visitor can do anything on the books related pages. We’ll start building out our application around that in this post.

Read more of this post

Declaring generic types in F#

Generic types have type parameters in F#. This is no surprise to those coming from other languages like Java and C#. Generics increases the flexibility of objects by letting one or more of its properties take on multiple types.

In F# type parameters are declared using the single quote followed by the type name like here:

type Container<'a> = {description: string; containedValue: 'a}

Read more of this post

Equality checking in F#

F# uses the equality sign ‘=’ to check for equality like here:

let areTheyEqual = 5 = 5

areTheyEqual will evaluate to true. At first it can be confusing to see the two single equality signs like that. The first one is the assignment operator and the second one checks for equality. It might be easier to rewrite the above as follows:

let areTheyEqual = (5 = 5)

The equality operator in F# checks for value equality for value types. It’s easy to check for equality on lists, arrays, tuples etc. Here are a couple of examples:

Read more of this post

How to partially read a file with C# .NET

Say you have a large file with a lot of text in it and you need to find a particular bit. One way could be to read the entire text into memory and search through it. Another, more memory-friendly solution is to keep reading the file line by line until the search term has been found.

Suppose you have a text file with the following random English content:

Read more of this post

Introduction to ASP.NET Core part 24: handling users


In the previous post we added the EntityFramework repository elements to our demo project. The book entities are now saved in and extracted from a database. We successfully replaced the in-memory placeholder repository with one backed up by SQL Server. We implemented the EF repository so that it has a separate commit function that needs to be called to persist the changes. The service class calling on the repository has an extra step to make but this way it’s possible to call AddNew several times and then CommitChanges only once. We’ll therefore not send a query to the database after each and every call to AddNew but send a single set of instructions instead.

In this post we’ll start looking into how to handle users in .NET Core MVC. We’ll be using the user management features built into .NET Core and EF Core. User management is most often not part of the core business domain and using the well tested built-in security features can save us a lot of time. That is time that we can spend on the real business domain of the application. In addition they can help us with the login and logout process.

We’ll also try and separate the user related elements from the “real” business entities of our application. That is we’ll put the users in a separate database with its own data context and connection string.

Read more of this post


A great site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: