Using the KeyedCollection object in C# .NET

The abstract generic KeyedCollection object can be used to declare which field of your custom object to use as a key in a Dictionary. It provides sort of a short-cut where you’d want to organise your objects in a Dictionary by an attribute of that object.

Let’s take the following object as an example:

public class CloudServer
{
	public string CloudProvider { get; set; }
	public string ImageId { get; set; }
	public string Size { get; set; }
}

The Image IDs are always unique so the ImageId property seems to be a good candidate for a dictionary key.

Here’s an example:

Read more of this post

Introduction to ASP.NET Core part 26: authorisation basics and logging in

Introduction

In the previous post we continued looking into the basics of user management in .NET Core. We first created a separate database for our users and related objects such as claims and tokens. We then went on and created the components necessary for user creation: the view, the account controller and the user registration view model. We used the built-in generic UserManager of T for the database section of user management. The UserManager object provides a wide range of functions to handle users: insert a new user, retrieve a user by ID, confirm an email and much more. Therefore UserManager is an out-of-the-box service class to connect the application user and the database.

In this post we’ll investigate the basics of what to do with our users: authorisation and logging in.

Read more of this post

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");
#else
	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

Introduction

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

ultimatemindsettoday

A great WordPress.com 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: