Resource Release in .NET Applications

In C#, there is one huge benefit, which is a drawback at the same time – an automatic garbage collection. With traditional desktop applications, this is great when a platform cleans up the whole garbage and releases memory. However, it is not always good for Web.

When we develop a Web application, Web-requests are mainly short; our code must perform small tasks and work very quickly. If we do not help a garbage collector, the server resources can exhaust in no time.

(more…)

Tricky Questions about C#

Some questions may seem too basic, but they still contain tiny tricks. Sometimes even a simple question may nail to the wall. These questions will be useful to all who study the language.

So, let’s start!

1. What will be the result of execution of the following code?

(more…)

Another Way to Localize Application

I would like to introduce a simple way to localize applications. I do not like the standard mechanism with resource assemblies for the following reasons:

  1. When receiving a value of a localized string in the code, I would like to rely on OOP and compiler prompts. It is very unpleasant to generate a project on Friday evening, and on Saturday morning, to get a call from QAs working overtime that someone inattentive wrote GetResource (“asdf”) instead of GetResource (“assf”). Thus, now something crashes or is displayed incorrectly. In addition, the project is to be “put to print” on Monday… (more…)

Preparing for Exam 70-483: Programming in C#

During preparation for exam 70-483, I found lots of websites containing links to various manuals that helped me a lot. But what really helped me was the memo notes I composed for myself and which I’d like to share in this article. I do not target to provide a detailed description of C#, I just want to refresh your memory and to highlight certain aspects of C#. If you are not familiar with some aspects, it means you have gaps in your knowledge that should be eliminated. (more…)

How Generics save from Boxing

At the method input, we often perform a null test. Someone makes the test as a separate method, so that the code looks cleaner, and gets something like this:

The interesting thing about this test is that I see a frequent use of the object attribute, though you can use generic. Let’s try to replace our method with generic and compare performance.

(more…)

Signals in C#

 

Currently, the thread synchronization in С# causes some difficulties, in particular, when passing synchronization primitives between the objects of your application and supporting them in the future.

The current model with Task and IAsyncResult, as well as with TPL, solve all issues through a proper design. However, I would like to create a simple class that allows sending and receiving signals with a thread lock. (more…)

Entity Framework: (anti)pattern Repository

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction.

Entity Framework provides us with the actual implementation of the Repository patterns: DbSet<T> and UnitOfWork: DbContext. I often see colleagues using in projects their own implementation of repositories on top of the ones existing in Entity Framework.

(more…)

Modular WPF application using Caliburn.Micro and Castle.Windsor

To start with, I want to specify what is meant by a modular application in this article. So, a modular application will be considered an application, which consists of the so-called shell and a set of plug-in modules. There is no direct dependence between them, only via contracts. This allows independently modify each of the components, change their contents, etc. I think everyone knows the advantages of modular architecture.

(more…)

Steps to Reduce Code Size after Use of Strategy Pattern

This article features a few tricks to reduce the size of the code resulting from the use of the ‘strategy’ pattern. As you can deduce from the title, all these tricks will be related to the usage of generic types.

1. Hierarchy of classes involved in magic rituals

Suppose we have an abstract ‘vehicle’ class that can move (the Move method). This class has 3 descendants: a car, a plane, and a rickshaw. Each of them implements the method in its own way: (more…)