Posts

Showing posts from January, 2020

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

The danger of duplicated mapping of many-to-one in NHibernate

If you use NHibernate, you will often have to deal with lots of many-to-one mapping. However, if you are not careful, you might accidentally create duplicated mapping due to the use of foreign key. Let's say you have a class,  A ,   which has a column, A_ID as the primary key. Class  B is related to A via a column, A_ID (in B), which is the foreign key referencing A 's primary key. Hence, we would define our B object like the following, public class B { public virtual A ClassA { get ; set ; } public virtual long A_ID { get ; set ; } } We have both the properties to represent the object A (due to the foreign key mapping), as well as the column which acts as the foreign key. The following is the XML mapping file. <property name="A_ID">       <column name="A_ID" not-null="true" /> </property> <many-to-one name="ClassA" column="A_ID" cascade="save-update"/> We want

Implementation of distributed cache

One of the things that we can't escape while developing a system, is the use of database to store data. However, we ought to know that the frequency of database access will directly affect our system's performance. This is where caching comes in. Caching not only reduces the number of calls required to access the database, but also to increase the speed we could retrieve the data that we need. Cache is a storage pattern which allows for fast retrieval of data which we need frequently, and it should not be a set of data which changes constantly. In case of ASP.NET Core, there are 2 types of caching that we can use; in-memory cache & distributed cache . In-memory cache means that the cache is available for access within the application itself only. If we are building a system that contains multiple apps. which spread across multiple servers, each app. will have its own cache, which might be redundant, since all of them utilize the same data. This is where distributed cac