C# Traps and Traps – part 2

Theory vs practice.

One of the first conclusions from learning about memory, data structures and collections was that we should always choose the collection according to our usage. The next step was to learn about all the collections so we could make an informed decision while writing our code.

An array is a collection that takes uninterrupted space in memory – and thanks to that, if we know where the first element of an array is in the memory, we can compute where the n-th element is – we just have to add n multiplied by the size of the element to the first element address and as a result, we know the address of the n-th element. The problem with an array is that when we need to expand the array (add new elements) we have to find a new place in the memory that will fit the array (including new elements), copy the memory to the new address, change the pointer to the memory and then free the old memory. The details differ between implementations but it is generally a lot of work.

Linked list, on the other hand, is great if we don’t need to have fast access to the n-th element, but we would like to add a lot of elements to the collection. A linked list does not take uninterrupted memory block, but instead, every element of the collection has knowledge of where the next (and sometimes previous) element is. This way, knowing the first element, you can get the address to next one, from there the next one and so on. When you need to access an n-th element of the Linked List it is difficult, because we need to visit n elements to finally find n-th. But it is super easy to add new elements to this collection, the only thing we need to do is to modify the previous element of the collection so that it points to the new element and makes the new element point to next element of the array. No need to copy memory, we just change pointers. Neat.

Continue reading “C# Traps and Traps – part 2”

C# Traps and Traps

Recently, on behalf of my employer, I spoke at the IT Academics Day, a small .NET conference organized by the West Pomeranian University of Technology. Together with Mateusz, we have presented some of the traps that a C# developer might encounter and how to recover from those. I would like to share three of ten such traps in today’s blog post, the rest will follow.

When inheritance is dangerous

The first one is a dangerous design trap, a bomb with a long fuse. Let’s take a look at the following code:

internal class BaseData
{
   protected string data = null;

   public BaseData(string data)
   {
       this.data = data;
       this.InitializeState();

       Console.WriteLine("BaseData created");
   }

   public virtual void InitializeState()
   {
       // Some basic initialization logic.
   }
}

Do you see anything dangerous yet?

Continue reading “C# Traps and Traps”

Scope – the inconspicuous project killer

There are many things that can go wrong while working on a project. And I mean not only technical difficulties, interpersonal relations, lack of time and experience, et cetera. They all can get in the way, but in my opinion, nothing breaks the spirit more effectively than a badly estimated scope.
Continue reading “Scope – the inconspicuous project killer”

Using validation attributes in .NET Core

There are a number of ways you can implement validation of your forms, models or uploads. One of the most common methods of doing that is using validation attributes. Not only are they very easy to use, they can also be customized freely according to your business’ requirements. Today we’ll take a closer look at how to use them based on a real-life example from our project.

Continue reading “Using validation attributes in .NET Core”

Creating custom Error pages and catching Exceptions in ASP.NET Core 2.0

One of my first tasks on the project was to create a custom error 404 page that the users of our application will be redirected to in case of such an error. By default, an ASP.NET Core application will redirect you to a browser-specific error page. That’s not really helpful to the user, since on Mozilla Firefox the page will be simply blank. Other browsers’ versions aren’t pretty either and in general you as the developer want to provide the user with some information on why the error occurred and maybe show him/her a link to the Home page and such. Shortly after setting the custom 404 page I’ve realized that it would be good to catch other errors and exceptions too – and thankfully in ASP.net Core all of it is really easy.

Continue reading “Creating custom Error pages and catching Exceptions in ASP.NET Core 2.0”

Creating a simple Content Management System

I have been working on creating a content management system (CMS for short) for troika-game.net. One of my tasks was to implement a way for the admin (who may not be a software engineer himself) to add, edit, and delete news from the home page. It is a very much needed (while still being rather basic) feature of our app. It requires the understanding of several aspects of ASP.NET Core MVC, so it’s a great way to learn the technology and besides – you got to start somewhere!

Continue reading “Creating a simple Content Management System”

Things I understood after starting to learn ASP.NET Core 2.0

When I joined the LearnByDoing.it project, I didn’t really know what I am in for. It’s just that I like to learn new stuff and the idea of developing a game just to get some new skills was already pretty interesting to me. Since joining, I have realised a couple of things about the ASP.​NET Core 2.0 framework that we use and thought about sharing them.

Continue reading “Things I understood after starting to learn ASP.NET Core 2.0”

Learning difficult things, part 1

Through my university experience I came to understand that the ability to learn is a skill by itself. You can be either good or bad at understanding new things. For a physics undergraduate such as myself (and now a physicist) it’s a skill that’s pretty fundamental and I dare say it’s also no less important to you if you want to be (or you already are) a software developer.

And that’s why we’re going to spend a couple of minutes talking about it.

Continue reading “Learning difficult things, part 1”

A taste of SignalR Core

What is SignalR?

The beginnings

ASP.Net (both “bare” Web Forms and ASP.Net MVC introduced later) is developed around a traditional HTTP request and response mechanism –
the server provides content in form of HTML pages or files only when they are being requested by client. In contrast, SignalR provides two-way communication between server and client – both can trigger actions in each other and the result can be shown immediately in a browser without the need for refreshing.
The last part assumes that there is a browser involved – which doesn’t necessarily has to be true, as apart from javascript implementation of SignalR, a C# one (with more already announced) exists – SignalR isn’t constrained to just web pages.

One of the main selling points of SignalR was its simplicity of use. With just a few lines of code, it was easy to set up a simple chat. (And not only that – one of the examples provided by Microsoft was a browser multiplayer shooter game).

Continue reading “A taste of SignalR Core”

Introduction to Entity Framework Core

What is Entity Framework?

For those who never stumbled upon Entity Framework, it is Object-Relational Mapping (ORM) framework. Its main purpose is to provide an abstraction of a database, so you can use the database as if using normal in-memory data collections.

In practice, you still need to be mindful of what you do write your LINQ upon, especially time-wise. However, the simplicity of building a database-powered persistence is stunning and, in my opinion, worth investing your time required for learning this framework.

Continue reading “Introduction to Entity Framework Core”