.NET Developer Days 2017

On 18-20.10.2017 I had a pleasure to attend .NET Developer Days 2017 conference in Warsaw. The first day we took part in a full-day workshop on containers with Docker and the next two days we attended the conference itself. In this post I’d like to share my thoughts and insights on the conference, its organizational aspects as well as my subjective opinions on the sessions I attended. Let me start by describing the workshops and all sessions I was present at. You can find the list of all sessions that were held during the conference

on its official website

Date selection using DatePickerDialog in Xamarin.Android

In this post, we’re going to see how to provide a nice Android UI control for selecting the date using DatePickerDialog. The dialog looks as follows:
It may be opened e.g. when clicking on a button in the app, as I used it in MoneyBack.

Creating DatePickerFragment

First of all, we will implement the dialog to be displayed within DialogFragment. Doing that our dialog will be able to be displayed as independent piece of UI on the top of any Activity. The second requirement is to implement IOnDateSetListener interface (coming from Java/Android), which provides a callback on date selection action done by the user. The DatePickerFragment meeting those two requirements may look as follows: .gist table { margin-bottom: 0; } There are few crucial parts in above-listed class: Line 1: as said before, the class derives from DialogFragment and implements IOnDateSetListener interface Line 3: here we have public static readonly string TAG variable defined – it is used as a unique identifier of the Fragment; in some cases, Fragments can be displayed without any UI – in such case the only possibility to identify and get the Fragment is by using

findFragmentByTag()

SQLite-Net Extensions – one-to-many relationships

In the 3rd post from SQLite-Net Extensions series we are covering the last type of relationship – one-to-many (and the opposite – many-to-one).

One-to-many, many-to-one

One-to-many relationships are used in general for parent-children or whole-elements relations. Classic examples are: bus and passengers, document and elements etc. One-to-many relationship means that the one-end entity knows about its children and many-end entity has a reference (foreign key) to its parent (but not necessarily knows about it). On the other hand, the opposite relationship to one-to-many is many-to-one. In that case, many-end entity has a reference to its parent and knows about it, but the one-end entity doesn’t necessarily know about its children (at least not directly). I used a verb to know several times – so it’s time to explain it 🙂 By “knowing” about the other end of a relationship I understand having a reference to it. It means that, for instance, in many-to-one relationship, the one-end of it doesn’t have a reference to its children. However in most cases what we’d like to have is a hybrid of one-to-many and many-to-one relationships. I will call it one-to-many with inversion. We want both parent to know about its children, and each children to know about its parent. In this post we’ll cover one-to-many with no reversion and one-to-many with reversion, as it also includes many-to-one relationship so you can have a comprehensive overview. We are going to see it on the example of Employee and Duty* entities. Each employee has a list of his duties, whilst each single duty is assigned to only one employee. * “Task” would probably be a better name, but I didn’t want to make it confused with .NET Tasks 🙂

One-to-many with no inversion

Firstly, let’s model this kind of relationship:
One-to-many with no inversion – class diagram
Now we can transform it into C# classes: .gist table { margin-bottom: 0; } In the Employee class (parent, one-end of the relationship) we define the collection of children, decorated with OneToManyAttribute. Collections types supported as for the time of writing this post by SQLite-Net Extensions are List and Array and can be used as you prefer. Let’s now see how the child entity (Duty) looks like: .gist table { margin-bottom: 0; } In the Duty class (child, many-end of the relationship) we need to have a foreign key to parent entity defined. For that purpose, we create a property representing it (), decorating it with , additionally specifying the type of parent referenced (Employee). That’s it. We can already use it in our code: .gist table { margin-bottom: 0; } No rocket-science here. What’s interesting for us it how looks in the end:
One-to-many with no inversion
As you can see, method returned object of type Employee with its collection properly retrieved (containing two duties assigned to the employee before). Moreover, each children has its foreign key () automatically retrieved from the DB – there is no overhead here, this is simply foreign key field stored in the same SQLite database table (Duties). As previously, let’s first see how the class diagram changes after adding inversion:
One-to-many with inversion – class diagram
What changed is that now each Duty has a property of type Employee. To realize the above class diagram and make each children (each Duty in our case) knowing about its parent (having a reference to responsible Employee), the only thing we need to do is to add the following property to Duty model class:

[ManyToOne] public Employee Employee { get; set; }

SQLite-Net Extensions – one-to-one relationships

In this second short post from SQLite-Net Extensions series, we’re going to see how to create one-to-one relationships using this tiny ORM.


This is the simplest type of database relationship. An example could be vehicle and registration certificate – each vehicle has one and only one registration certificate, and one registration certificate is associated with one and only one vehicle (excluding some extraordinary law rules in other countries that I don’t know 🙂 ). We can model it in two ways:
  • as a one-way relationship – in this case only one of the relationship’s ends knows about the other one
  • as a both-ways (with inversion) relationship – both relationship’s ends know about each other.

One-To-One with no inversion (one-way)

This kind of relationship looks as follows:
One-To-One – one-way – class diagram
We use it when we assume it’s enough that Vehicle knows about RegistrationCertificate, but the document doesn’t necessarily need to know which car/motor it’s associated with (at least directly). In the code, we then create two model classes using SQLite-Net Extensions: .gist table { margin-bottom: 0; } .gist table { margin-bottom: 0; } What’s interesting here is the ForeignKeyAttribute defined on RegistrationCertificateId property. It is – as its name says – the foreign key to the primary key of related entity (of type RegistrationCertificate). The related entity property itself is decorated with OneToOneAttribute. There’s nothing more we need to do to have this relationship modeled. We can already use it: .gist table { margin-bottom: 0; } Nothing special here, right? It looks very similar to what we’ve seen in the previous post about many-to-many relationships. What’s interesting for us it that in the end, when Vehicle object is retrieved from the database using GetWithChildren method, its RegistrationCertificate property is filled as well:
One-To-One – filled related entity

One-To-One with inversion (both-ways)

This kind of relationship models itself as below:
One-To-One – with inversion – class diagram
What actually changed on the diagram compared to the relationship with no inversion is that now RegistrationCertificate has a property of type Vehicle (in the code – a reference to Vehicle related object and a foreign key). In our case, this is even more “real” – it would be nice if a Vehicle knows about its RegistrationCertificate, but also when we look at the certificate we’d like to see to which car it belongs. We can now model it in the code. The only thing that changes in the model classes is addition of a reference and foreign key to Vehicle in RegistrationCertificate model, so I’m presenting only this class’s updated code: .gist table { margin-bottom: 0; } What’s cool here is that we don’t need to modify our code for saving entities with relationships into SQLite database. We can simply add the following line at the end:

var certificateStored = db.GetWithChildren<RegistrationCertificate>(certificate.Id);

SQLite-Net Extensions – many-to-many relationships

In today’s post we’re going to see what is SQLite-Net Extensions ORM and how to use it to create many-to-many relationship in SQLite database. The other types of relationships will be described in separate posts.

What is SQLite-Net Extensions ?

As you develop any mobile app, sooner than later you need to keep your app’s data in some persistent storage. In my MoneyBack project I’ve chosen SQLite database using SQLite.NET library for performing operations on it. This is actually very simple and easy-to-use database framework, but I recently realized I need to model some relations in my database.  SQLite.NET doesn’t offer any nice utilities to model such relations. However, if you need to model any relations in your SQLite database, there is a wrapper on SQLite.NET which allows to do that – it is SQLite-Net Extensions. It basically extends the core functionalities of SQLite.NET by adding elements that allow to easily handle relationships, including one-to-one, one-to-many, many-to-one and many-to-many. In this post we are going to see how to create many-to-many relationship using this library. I needed this kind of relationship to model the connection between Person and Event entities in my app.

Many-to-many relationship

Let’s see many-to-many relationship on an example of two entities: Person and Event. An event (entity of type Event) may contain zero or more participants (entities of type Person) whilst a person may be assigned to zero or more events. This is a typical many-to-many relationship which we are going to set up in our database now.

Install SQLite-Net Extensions

If you’ve previously been using SQLite.NET in your project – uninstall it first. I didn’t do it before starting to use SQLite-Net Extensions and I have many troubles with Visual Studio resolving my references incorrectly. SQLite-Net Extensions is a wrapper for SQLite.NET, so it already contains this library and additionally extends it by adding some extra functionalities for handling relationships. SQLite-Net Extensions can be installed as a Nuget package into your solution. According to the version you’d like to use, execute appropriate command in Package Manager Console in Visual Studio: synchronous:

Install-Package SQLiteNetExtensions -Version 1.3.0

MoneyBack – 3 weeks before the end of DSP

In today’s post I’d like to present what’s the current status of works on MoneyBack application, 3 weeks before the end of DSP competition. In general, the project hasn’t been going as well as I expected and is currently not as advanced as I’d expect it to be at this stage. There are many reasons behind that, including lack of time recently and many issues met during development, however, as I already wrote in my DSP introducing post, the most important is to survive with the project to the end of the competition and meet all requirements. The end of DSP doesn’t equal the end of MoneyBack 🙂 That’s why in this post I’d like to show you how the project looks currently and what’s still to be done. Moreover, as I promised before, I’m publishing my Trello board with tasks still to be done within the project.

UI changes

For the last days working on the project I’ve been trying to make the UI of MoneyBack some kind of “final” version, so I can easily extend it adding new functionalities and focus on development instead of spending time on interface design, which is not important for me at the moment. I met many issues with that, which I think came from using some deprecated UI elements I used (e.g. ListView and Adapters) that are not fully-supported in the newest versions of Android. Tutorials are also not really up-to-date for those components. Maybe that’s also because when I wanted to implement displaying a list of entities I looked in Google for something like “xamarin android list of elements” which lead me to this ListView and Adapters tutorial. Instead of firstly reading the whole User Interface tutorial, I wanted to implement displaying the list quickly, so I used those deprecated elements instead of implementing for instance newer and recommended RecyclerView from the beginning, which would be easier extensible now. That definitely teaches me that I should get to know a particular topic/area better and wider before deciding to use one of the possible solutions to solve a problem. Sometimes we want to implement something particular quickly, but later it produces more issues than benefits. Finally I added tabs (only one tab for People for now, but it will be easy to add new ones now) and ActionBar menu on the main Activity of the app:
MoneyBack – tabs
On the screenshot above, in the right-upper corner there is “Add” button visible when clicking on ActionBar menu. It opens a new person’s adding screen, on which I made name and email fields mandatory:
MoneyBack – adding Person
People added are then displayed on the main screen of the app in “People” tab:
MoneyBack – list of people

Code – interesting parts

Using tabs layout in an Activity
It turned out that using tabs-like layout in the Activity is pretty easy. In the AXML layout file for MainActivity I just added a FrameLayout container, within which the tabs and their contents are to be displayed: .gist table { margin-bottom: 0; } I called it tabFragmentsContainer, because within each tab we will display a Fragment. Fragment represents a part of UI and/or behavior, which is displayed within an Activity. You can think of Fragments as smaller pieces, that can be placed onto (inside) Activities. Basically, I changed PeopleListActivity to PeopleListFragment, which in fact only required changing inheritance from being a child of Activity to inheriting from ListFragment (which is a special kind of Fragment used for displaying lists of elements). The complete code of PeopleListFragment can be found in this public Gist. In the end, making MainActivity displaying tabs requires setting its ActionBar’s navigation mode to :

this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

Solving performance issues with dotTrace

If you’ve ever had any performance issues with your .NET applications, you may find this writing interesting. In this post I’d like to present to you a tool produced by JetBrains – dotTrace, showing how using only one of its functionalities can help in finding causes of performance issues in .NET applications.

Performance issue on production

Months ago some of your colleagues implemented an application and delivered it to the Customer. The app is “Countries and Cities Manager”, which is used by some administrator working in local government unit. That’s how it looks:
Countries and Cities Manager
That’s cool, isn’t it ? This “administration panel” allows to add a new city to the country. User fills name of the city and provides ISO code of the country considered. You may think “who would ever use it and for what?”. True. But first of all, this is just an example 🙂 , and secondly, imagine that Russia annexes some big European country. What happens then ? User administrating local government system must use your manager application to insert hundreds or thousands of new cities in Russia. Gotcha! The next day you receive a ticket in your company’s issues tracking system. Taking into consideration that an average number of cities in east-European countries is ~600, the ticket’s priority is set to “Blocking”. The description of the issue is:
  1. User fills the “City” and “Country ISO Code”, clicks “Add city”
  2. Waits for 10 seconds…
  3. Waits for 20 seconds…
  4. Waits for 30 seconds…
  5. Error message received:
    ISO not found
  6. User corrects “RU” to “RUS”
  7. Waits for 10 seconds…
  8. Waits for 20 seconds…
  9. Waits for 30 seconds…
  10.  City added!
  11. Scenario repeats for each new city to be added. Performance is unacceptable.
Initially you have no idea what could be the reason. Such simple operation and such terrible performance? Let’s see…

Looking for issue with dotTrace profiling

In such cases, especially if I don’t know the code of the application considered, the very first thing I do it to use dotTrace in order to profile the application when executing bad-performance actions. It provides many features, including: detecting performance bottlenecks in .NET applications getting information about calls execution times analyzing calls order on a timeline profilling SQL queries and HTTP requests profiling unit tests … and

many more

Building Windows Services using Topshelf

Hello again 🙂 Visibly this is going to be my first technical post here. I hope you’ll find it useful.

Windows Services

Recently I came up with a need to create an application, which executes some tasks in the background based on file system’s events. “That’s easy”, one would say. “Build Windows Service app”, he would add. Sure, that was also my first idea.

I think that’s very common that we want to create an application without any GUI, which purpose is just to do something in the background. Moreover we’d like it to be independent from the user running it – we want it to be running all the time, without having to manually launch it – especially when the application is running on customer’s environment, where we can’t be logged in all the time running the application.

Here comes the service – long-running executable with no user interface, which runs in its own Windows session. Such application can be:

  • executed by any user created in the system (e.g. with specific security policies assigned),
  • started automatically when the system launches,
  • automatically restarted in case of errors,
  • at any time started, paused, stopped or restarted.

Even if you’ve never developed any service applications, you are using many of them – check your services.msc to see the list of services currently running in your system.


Visual Studio 2015 provides a template to create Windows Service application and probably I could finish my post here. However, I would like to share with you an even easier way of building services: to do it with Topshelf – a very lightweight open-source library, which purpose it to make services development, debugging and deployment painless. Topshelf allows the developer to focus on service’s functionality, instead of wasting time interacting with .NET Framework’s build-in services support. There is also no more need to install your service using InstallUtil or learn how to attach your Visual Studio debugger to running service to detect issues.

Using Topshelf it’s enough to create console application, add one service class with public Start and Stop methods, configure service’s startup parameters (its name, display name, description etc.) and you’re ready to go. Below we’ll see how to do it.

Creating Windows Service application

After creating console application in Visual Studio, add Topshelf to the project by installing it from Nuget:

Topshelf - Nuget

Adding service class

Then, create service class, which is responsible for executing your service’s logic:

class FileWatcherService { public bool Start() { // Initialize your service application here return true; } public bool Stop() { // Executed when the service is stopped // Dispose your objects here etc. return true; } } read more...