Thread-safe bags in .NET

Bags are very similar to Stacks and Queues. We saw that both stacks and queues order their elements in a well defined way: last-in-first-out and first-in-first-out respectively. Bags on the other hand are unordered collections. There’s no guarantee on how, i.e. in which order the elements will be retrieved.

Unlike stacks and queues, bags have no one-to-one single-threaded implementation in .NET. They are however implemented as thread-safe ConcurrentBag objects in the System.Collections.Concurrent namespace.

Read more of this post

Web API 2 security extensibility points part 1: starting point and HTTP request context

Introduction

Web API 2 comes with a number of new security features. In this new series we’ll concentrate on the HTTP request context and its Principal property. In particular we’ll see how to hook into the different extensibility points in the Web API. These extensibility points offer you to plug in your security-related checks at different points in the application lifetime when a request hits your API. You can carry out a number of checks and modify the request Principal according to your business rules and needs.

I’m building the demo in Visual Studio 2013. I’m not sure at this point how much the Web API template will change in Visual Studio 2015.

Read more of this post

Python language basics 31: mutable objects as default arguments

Introduction

In the previous post we discussed how to provide optional arguments to a function. We saw how easy it was to assign a default value to a function argument using the assignment operator ‘=’. The caller could optionally ignore those arguments so that the default ones would be used within the function body. Alternatively the caller could override the defaults and provide its own argument values.

In this post we’ll discuss an additional facet of optional function arguments. Optional arguments are evaluated only once along with the function. This can cause unexpected results when mutable objects are passed as default arguments.

Read more of this post

Python language basics 30: providing default values to a function

Introduction

In the previous post we looked at how objects are passed by reference to function in Python. We saw how it could be dangerous to let a function modify an incoming parameter if you’re not aware of the reference passing behaviour. We also briefly looked at the idea of a deep copy which is one way to keep your original objects intact.

In this post we’ll diverge from objects and instead investigate a feature of functions: default arguments.

Read more of this post

Structurally compare two arrays in .NET

In this post we saw how to determine if two arrays are structurally equal in .NET. Two arrays are said to be structurally equal if they contain the same elements in the same order.

Structural equality has a comparison counterpart: IStructuralComparable. It determines if an array comes before or after or is equal to another array based on the elements within it.

Read more of this post

Building a Web API 2 project from scratch using OWIN/Katana .NET Part 5: adding an IoC

Introduction

In the previous post we transformed the CustomersApi application a little bit. We added a customer repository and a customer service. We also transformed the Get action method of CustomersController into an asynchronous one.

We ended up having to construct the dependencies of CustomersController in the constructor like this:

public CustomersController()
{
	_customerService = new CustomerService(new CustomerRepository());
}

In this post we’ll get rid of this direct control over the controller dependencies. Instead, we’ll let an ICustomerService object be injected into the controller through its constructor.

Read more of this post

.NET source code reference

This is just a tip if you’d like to check how a certain built-in method in .NET is implemented. The .NET core library has a reference page available here:

http://referencesource.microsoft.com/

You can search for a method, click the correct method name and view the exact implementation details:

Search for add method on .NET reference page

Thread safe queues in .NET

We saw how standard Queues work in .NET in this post. Queues are first-in-first-out collections where the first element added to the collection will the first to be removed. Queues are ideal to model consumer-producer scenarios where a producer adds items to a queue which are then processed by a consumer in the order they came in.

If you’d like to have a Queue that is shared among multiple threads in a multi-threaded application then the “normal” Queue of T object won’t be enough. You can never be sure of the current state of the queue in the very moment when a certain thread tries to dequeue an item from it. The queue may have been modified by another thread just a millisecond before and then the Dequeue method will fail. In general you should be very careful with how you share the resources among different threads.

Read more of this post

Building a Web API 2 project from scratch using OWIN/Katana .NET Part 4: async controllers and mini-DDD

Introduction

In the previous post we briefly looked at a new hosting project by Microsoft called Helios. It is meant to be the future of web application deployment where Helios removes the necessity of having the entire System.Web dependency in your web project. We saw that Helios is only in a preview state so it shouldn’t be used for real-life projects yet.

In this post we’ll diverge from OWIN/Katana and instead see how we can add asynchronous controller methods to our current CustomersApi web project. We’ll also build a miniature version of a layered application. We’ll put the layers into separate folders for simplicity.

Read more of this post

Python language basics 29: passing the reference value into a function

Introduction

In the previous post we looked at the effects of modifying the value of an object. We saw the difference between mutable and immutable objects. Immutable objects cannot be changed directly in memory whereas mutable objects can. You need to keep in mind that if two object variables reference the same value then changing one variable value will also change the value of all other references.

In this post we’ll discuss what this behaviour means when objects are passed as arguments into functions. Functions will be able to modify the referenced argument which will also affect the object that was originally passed in. If you’re not aware of this behaviour it can cause irritation and buggy code.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

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