Skip to content

Tag: .Net Core

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…

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 these questions.   Implicit Dependencies The dependencies are implicit for a class, if they exist only in the code within…

Analyzing a Tightly Coupled Application

Dependency injection is a vast topic in itself. However, it is difficult to find everything we should know about it in one place. And so, I thought of creating my own series of learning about topic. In this part of the series we will look into a tightly coupled application and the problems related to it.   Introduction An application is said to be tightly…

Null Object Pattern

Almost every method that we write, irrespective of the programming language, we always have checks like the following one: if (objectVariable == null) return; // do some work with objectVariable We end up adding so many null checks in our methods, that it gets almost hard to figure out what we are supposed to do in our method. This makes our code look ugly and hard…

Anonymous Types in C#

Generally, for an object to encapsulate some properties, the type of the object has to be defined first. On the contrary, anonymous types provide a convenient way for an object to encapsulate readonly properties without explicitly defining the type first. The type of each property is deduced by the compiler.   Creating an Anonymous Type We can create an anonymous type using a new keyword along…

Understanding Relationship Between Objects

Introduction The relationship between objects defines how these objects will interact or collaborate to perform an operation in an application. In any application, objects of user interface classes interact with the business layer objects in order to perform an operation. And in the background, the business layer objects may interact with objects of some repository which in turn talks to some data source object or…