Domain Driven Design with Web API revisited Part 2: the problem domain and DDD basics refreshed

Introduction

In the previous post we went through DDD at a very high level. We discussed the cases when DDD can be a good solution and when it could rather be overkill. We saw that a complex domain model with lots of behaviour and logic could well justify the usage of DDD. On the other hand a project with more simple use cases such as CRUD could be made lighter with just plain classes filled with public getters and setters. It’s also an option to just pick certain ideas from DDD in your project. DDD is full of useful guidelines and patterns that are beneficial to software projects of all sizes.

In this post we’ll describe the problem domain of our demo application. We’ll also refresh our knowledge of some basic DDD concepts.

Read more of this post

Python language basics 37: splitting a delimited string

Introduction

In the previous post we saw how to efficiently concatenate strings using the join function. We showed a little trick to build one string from several others with a space in between.

In this post we’ll quickly look at the exact opposite, i.e. how to split a delimited string.

Read more of this post

Python language basics 36: string concatenation using the join method

Introduction

In the previous post we discussed how to use the ‘in’ operator to determine whether a certain element is found in a collection. You can apply this operator – and its negation ‘not in’ – on all collection types including strings.

In this post we’ll look at a useful String function called ‘join’ to concatenate bits of strings into a single one.

String concatenation

Basic string concatenation is performed using the addition operator ‘+’:

combined = "Welcome" + " to" + " my blog " + "dear " + "readers"
print(combined)

‘combined’ will become “Welcome to my blog dear readers” as expected. Do you recall that strings are immutable in Python – and in fact all major programming languages? The problem with the above statement is that it creates multiple strings in succession as the ‘+’ operator is applied:

Welcome
to
Welcome to
my blog
Welcome to my blog

etc.

‘combined’ will at the end point at the string “Welcome to my blog dear readers” in memory. The other values will be kept in memory until the garbage collector cleans them up as unused resources without references.

A more efficient solution is the join function although its usage is a bit strange at first sight. It operates directly on a string which will become a string delimiter. Here’s an example:

combined = '|'.join(("This", "is", "a", "delimited", "string"))
print(combined)

combined will become:

This|is|a|delimited|string

Can you guess why we needed 2 pairs of parenthesis? The inner pair is the way to declare the tuple collection which we discussed in this post. The join method takes only one argument and it must be a collection. Hence we need to wrap the strings to be concatenated in a collection. A string list works equally well:

combined = '|'.join(["This", "is", "a", "delimited", "string"])
print(combined)

‘combined’ will be the same as above.

How can we build the same “Welcome to my blog dear readers” like above? What is the delimiter here? It’s of course the space character ‘ ‘:

combined = ' '.join(["Welcome", "to", "my", "blog", "dear", "readers"])
print(combined)

‘combined’ will evaluate to “Welcome to my blog dear readers” as it was intended.

Read the next part here.

Read all Python-related posts on this blog here.

Explicit interface implementation in .NET Part 1

The generic and well-known Dictionary object and its generic and thread-safe counterpart, i.e. the ConcurrentDictionary object both implement the generic IDictionary interface. The IDictionary interface has an Add method where you can insert a new key-value pair into a dictionary:

Dictionary<string, string> singleThreadedDictionary = new Dictionary<string, string>();
singleThreadedDictionary.Add("Key", "Value");

I can even rewrite the above code as follows:

IDictionary<string, string> singleThreadedDictionary = new Dictionary<string, string>();
singleThreadedDictionary.Add("Key", "Value");

However, what if we try to do the same with the concurrent dictionary?

Read more of this post

Domain Driven Design with Web API revisited Part 1: introduction

Introduction

Domain driven design (DDD) has been around since 2003 when Eric Evans published his groundbreaking book on the subject. It is quite a large and complex topic with a steep learning curve. Chances are that it will take years of training and hands-on experience before you can get fluent with all its parts. Also, it takes truly large projects, like SAP with all its modules, to utilise all ideas presented by DDD. I have to admit that I’ve only come to work with a subset of DDD, such as aggregate roots, value objects, entities, repositories etc., and haven’t worked with e.g. an anti-corruption layer. That’s fine, applying DDD to a software project doesn’t necessitate using every single field from DDD. You could then easily bloat your project unnecessarily.

Read more of this post

Summary of thread-safe collections in .NET

The System.Collections.Concurrent namespace has 4 thread-safe collections that you can use in multi-threaded applications. The starting point is that you have a multi-threaded app where the same collection needs to be accessed by different threads. In that case the well-know collection types, like HashSet, List, Dictionary etc. simply won’t be enough.

If many different threads have access to the same resource then there’s no guarantee on the state of that resource in the moment a thread accesses it in some way: deletion, lookup, insertion or modification. Another thread may have accessed the same resource just milliseconds before that and the other thread will access the resource under the wrong assumptions. You’ll end up with buggy code with unpredictable results and ad-hoc fixes and patches that probably won’t solve the root of the problem.

Read more of this post

Using a thread-safe dictionary in .NET C# Part 4: thread-safe insertlookups

In the previous post we looked at how the AddOrUpdate method worked. We saw that it was a very neat and thread-safe way to either insert a new key-value pair or update an existing one.

We’ve already seen a thread-safe method that helps you retrieve values by their keys in this post: TryGet. It returns true if the item could be retrieved. TryAdd on the other hand is used to insert a new key-value pair. It also returns true if the item could be inserted successfully. However, what do you do with the returned boolean values? Do you keep trying in some loop until it succeeds?

Read more of this post

Web API 2 security extensibility points part 5: OWIN

Introduction

In the previous post we discussed how to add a custom authorisation filter to your .NET web application. We saw how you could derive from the AuthorizeAttribute class and implement your own logic for the IsAuthorized and HandleUnauthorizedRequest methods. The methods in the custom authorisation filter are called after any authentication filter is applied and before the appropriate controller action is executed.

In this post we’ll look at another entry point where you can apply your custom security logic in a .NET web application: OWIN

Read more of this post

Python language basics 35: determine if an element is included in a collection

Introduction

In the previous post we looked at how to return multiple values from a function in Python. We saw that this is fact was only possible through a trick where the trick was to use a collection of type Tuple. We went through an example with a simple function that returned 4 values in a tuple. We also discussed how to access individual elements in the tuple.

In this post we’ll see how to determine if a certain element is located in a collection.

Read more of this post

Python language basics 34: returning multiple values from a function

Introduction

In the previous post we looked at the idea of local and global variables. We saw how a variable with function-level focus could shadow another variable with the same name but global scope. Finally we discussed how to indicate our intention to use the global level variable using the “global” keyword.

In this post we’ll look at a technique to return multiple values from the same function.

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.