Skip to content

Quick Dev Notes Posts

Being lazy with Lazy<T>

Being lazy doesn’t refer to being lazy in life, but being lazy about the creation or instantiation of objects in our code. Sometimes, creation of an object is very costly. Now, when we say “costly”, we are not talking about money. Rather, we are talking about the memory consumed in the process and by the object itself. Consider a component A that uses a service…

Why Service Locator is an Anti-Pattern?

Previously, we saw how we can use a Service Locator in order to prevent ‘newing‘ of objects in our code. However, everything comes with a price. Though we will talk about this in detail, but just to give you a gist of it, a service locator is an anti-pattern as it hides the class dependencies.   Commerce Application Example The example we are following is…

Using Service Locator Pattern in DI

To begin with, service locator pattern is one of the most commonly used pattern. In general, this pattern is used along with other patterns like Adapter Pattern, Observer Pattern, Dependency Injection Principles and many more. In this post, however, we will be talking about its use with dependency injection, which is one its many uses.   Introduction The service locator pattern works as a middle…

AsImplementedInterfaces – code with bad smell

While I was exploring Autofac, I found AsImplementedInterfaces quite interesting. Not only because it is useful, but also because if we are not careful with its use, we may end up with unexpected application behavior. However, it was quite difficult, to find an example which explains: what is AsImplementedInterfaces and how it helps? why code with AsImplementedInterfaces has a bad smell? And therefore, here we are, with this…

Dependency Injection using Autofac

In the previous post, we saw how we can inject dependencies without any DI container. However, in this post, we will see how DI containers help us by instantiating the dependencies and provide them whenever and wherever required.   What is a DI Container? In the example for our last post, we had to instantiate dependencies on our own, before injecting them via constructor. Consequently, as…

Dependency Injection – without DI Container

Now that we have the answer to our question, why loosely coupled code?, let’s try and solve the problems we found in a tightly coupled application. In this post, we will use constructor based dependency injection, however, we are not going to use any DI container for now.   Dependency Injection Dependency Injection (DI) is a technique that helps us achieve loose coupling between objects and…

Implicit vs Explicit Dependencies

If our class depends upon other classes to complete its operations, then these other classes are dependencies for our class. Our class can have these dependencies as implicit or explicit dependencies. What does this mean? What effect does it have on our application design? Let’s try to find answers to similar questions.   Implicit Dependencies The dependencies are implicit for a class, if they exist only in the code within…

5 Reasons to write Loosely Coupled code

In the last post, we analyzed a tightly coupled application and listed out the problems we found with the same. In this post, we will explore, how writing loosely coupled code helps us keeping our applications easy to write, test and maintain.   Introduction A loosely coupled application has good isolation among its classes, modules and assemblies. Therefore, if we make a change to one part…

Pre/Post Build Events in Visual Studio

Introduction Pre/Post Build Events are useful when we wish to perform some operation before/after a project is build. These operations are nothing but the shell commands being used from the command line. Consider a scenario where we build our library project and its .dll is saved into the Project/bin/Release directory. For some reason we want all our library files in one common directory that we would refer…