Posts

Incrementing alphabets as a form of ID

Occasionally, whether you are designing or integrating APIs, you would come across the need to have unique identifier that includes alphabet(s). For example, how can we implement the following unique identifier, AANNNNNN, where NNNNN is numeric serial, and AA is the alphabetical serial.  So, each time we need a new ID, it would be   AA000000,...,AA999999,AB000000,...   I will not go into what you should do after ZZ999999, but rather how to maintain the alphabetical side of this ID. The first thing that came into my mind would be splitting the alphabets from the numerical serial, but how can I store the alphabets as a sequence in the database?  The solution would be to store a numeric representation of the alphabets in the database, and convert them accordingly when you read the sequence. Using Excel as an example, we would have the following,  1: A  ...  26: Z  27: AA  28: AB  ... and so on  Since I need a 2-character alphabets, my sequence should start at 27, 'AA', and ends at

An extra .0 when returning JSON response that contains decimal type

Sometimes when we are designing API responses, we might want to return a JSON string which contains decimal-type field. We could easily represent the decimal-type field with string, so the response is in the following format: {     "amount": "3.00" } However, for whatever reason, if you want to return the amount field   in decimal, and let's say it is very important the number of decimal place is shown correctly, there is some configuration required to get that right, as without any intervention, the client will end up seeing something as follow. *I am writing this based on the API designed using .NET's tech. stack. {     "amount": 3.0 } Assuming we want the amount to be returned in 2 decimal places, 3.00, we could achieve that with the use of attribute. Let's start by creating a class with a property called amount . public class Receipt {     public decimal amount { get; set; } } At some point, we want to serialize the above

Add current Git commit to web.config on each build [.NET Core/ASP.NET Core]

*Docker service might eliminate the following technique's usability. In this post, I am going to demonstrate a way to actually write the current commit of your version control module (I am using Git) into a location, let's say in your web.config . One of the issue I have faced when managing multiple console apps., or even web projects is that it is not easy to keep track of the com mit which the applications belong to. However, there is a simple technique to actually write the current commit of a specific branch into a location inside your project. The technique is the same regardless of the project type. * I am talking about .NET based projects. Although I only tested this in .NET Core based projects, I am pretty sure it works almost the same in .NET Framework/ASP.NET MVC based projects. The main idea is the utilize the build engine, MSBuild, to do something after building your application. The command execute a task could be derived in your .csproj file. You can defin

Ways to perform eager loading via NHibernate

By default we know that lazy loading is enabled in NHibernate. So, let's say if we decide to keep the default, how do we enable eager loading with NHibernate? There are 2 ways to do that (there are more, but I will mention 2 that you will commonly see): 1.  Query<Entity>().Fetch(x => x.ClassA).Fetch(x => x.ClassB); 2. var tempQuery =  Query<Entity>().Fetch(x => x.ClassA).ToFuture();      Query<Entity>(). Fetch(x => x.ClassB).ToFuture();     var entityList =  tempQuery   .ToList(); Do note that Fetch is used to fetch reference, while FetchMany is used to fetch collection. If you are wondering what is the difference between 1 and 2, it is the use of ToFuture to split the SQL statement to multiple queries. The problem with multiple Fetch in a single LINQ query is that it would result in Cartesian product of the main object, in this case, Entity  . Hence, it is not recommended to use multiple Fetch in a single query, but rather use method 2.

Generic query with NHibernate with eager loading

In my previous post, I showed how we could create generic methods to perform query,  https://csharptipstricks.blogspot.com/2019/12/usage-of-expression-to-create-generic.html . However, the methods present there does not allow eager loading. In case you are wondering how eager loading is performed, see below. Do know that there are ways to assign eager loading during mapping, but I will not show it in this post. Method I : SessionFactory.OpenSession().Query<MyEntity>().Fetch(x => x.Property1).Fetch(x => x.Property2).ToList(); Method II : var tempMyEntityQuery = GetSession().Query<MyEntity>().Fetch(x => x.Property1).ToFuture(); GetSession().Query<MyEntity>().Fetch(x => x.Property2).ToFuture(); List<MyEntity> myEntityList = tempMyEntityQuery .ToList(); Both the methods above will eager load Property1 & Property2 in a single trip to the database. However, I would choose method I because a single statement (with left outer join) is gener

The use of trigger to simplify the population of timestamp during update (PostgreSQL)

Often in development, we will want to include Created and Updated columns in each of our table, for tracking and recording purpose. However, if you are like me, we cannot avoid interacting with the database outside of our code, be it we want to insert or update a record manually. In case of PostgreSQL database, we can use function and trigger to track a particular column in a table. The function is for us to ensure that there is a new value for the Updated column, but we would not want to override a value is one is provided, we just want to ensure the Updated  column is up-to-date, just in case none is provided. Firstly, we need to create a function to provide a new value for the Updated column, if one is not given. CREATE OR REPLACE FUNCTION UPDATED_CHANGED ( ) RETURNS TRIGGER AS $$ begin IF NEW . UPDATED < = OLD . UPDATED THEN NEW . UPDATED : = NOW ( ) AT TIME ZONE 'UTC' ; END IF ; RETURN NEW ; END ; $$ LANGUAGE plpgsql ; Here, we are

ASP.NET Core service locator pattern

There are many posts out there which specifically say that we should not use service locator pattern (unless necessary), which we would not be able to determine the dependencies during compile time because the dependencies are hidden. However, I am going to point out an issue in case if you need to use it, in the context of ASP.NET Core projects. I have a class Foo , which will be bind to IFoo , and I will need to configure a named HttpClient for that class via the following extension,   services.AddHttpClient<IFoo, Foo>(); If we do the above, Foo will be treated as an object which is Singleton injected. In case you are not aware, we cannot inject services which has a smaller lifespan than the service itself. Hence, we cannot inject services which are transient or scoped into Foo . Here comes the problem, what should we do? That's where service locator pattern comes to the rescue! There might be other way, but let's say we decide to use the service locator provide