Showing posts with label TDD. Show all posts
Showing posts with label TDD. Show all posts

Thursday, 16 April 2020

Dependency Injection in ASP.Net classes for TDD


Dependency Injection in ASP.Net classes for TDD

In Asp.Net classes there is not built in library like Unity or Autofac.

What’s the problem with plain c# classes?

The reason for that is, in MVC or Web API Controller objects are created via IHttpControllerActivator interface which is inherited by a class. In Unity library IDependencyResolver is user which is kind of a ServiceLocator anti-pattern hence objects of controllers are created without manual intervention
This IHttpControllerActivator lies in API pipeline to instantiate controllers
Now when we develop c# classes and code becomes too big and complicated and automatically becomes tightly coupled which makes TDD harder and next to impossible as once because there is no way to inject mock/test classes instead of actual DB or dll library classes

How to solve it?

We can create a ServiceLocator anti-pattern in plain old .net classes for DI purpose. Instead of directly creating object of third party dll library we can simply register those in ServiceLocator anti-pattern and then resolve the dependency within the scope of the anti-pattern

</>
       public class ServiceLocator
       {
              // Key = interface type
              // Value = concrete type that implements the Key interface
              private static readonly Dictionary<Type, ServiceTypeContainer> types;

              // used for locking, especially when creating objects using reflection
              private static object syncObject = new object();

                           static ServiceLocator()
              {
                     types = new Dictionary<Type, ServiceTypeContainer>();
                     Register<IDependency>(typeof(Dependency));
              }

              public static T Resolve<T>()
              {

                     ServiceTypeContainer typeContainer = types[typeof(T)];
                     T returnObj;

                     if (typeContainer.Initializer == null)
                     {
                           // CreateInstance uses a static cache that is not thread-safe
                           // We could change the lock to on typeContainer if this becomes a bottleneck
                           lock (syncObject)
                           {
                                  returnObj = (T)Activator.CreateInstance(typeContainer.ServiceType);
                           }
                     }
                     else
                     {
                           returnObj = (T)typeContainer.Initializer();
                     }

                     return returnObj;
              }

              public static void Register<T>(Type objT)
              {
                     Register<T>(objT, null);
              }

              public static void Register<T>(Type objT, ServiceInitializer initializer)
              {
                     types.Add(typeof(T), new ServiceTypeContainer(objT, initializer));
              }
       }

       public class ServiceTypeContainer
       {
              public Type ServiceType
              {
                     get;
                     private set;
              }

              public ServiceInitializer Initializer
              {
                     get;
                     private set;
              }

              public ServiceTypeContainer(Type serviceType, ServiceInitializer initializer)
              {
                     ServiceType = serviceType;
                     Initializer = initializer;
              }

       }

       public delegate object ServiceInitializer();

There are two classes above one is used to register classes and another type container
Now instead of tightly coupled code you can register all the third party dll classes in anti-pattern and then use it like with the interface only
As you can see I have registered the Dependency class using IDependency type and later I will resolve it using IDependency only

Practicle

Below is a class ProductHistoryClass which has parameterized constructor that takes a custom type as an argument which is in fact a reference to a service or a dll class calling another method

public class ProductHistoryClass
    {
        private IDependency dependency;

        public ProductHistoryClass(IDependency _dependency)
        {
            dependency = _dependency;
        }

        public int FindProductHistory()
        {
            return dependency.FindProductHistory();
        }
    }

Below are our dependency Interface and class
public interface IDependency
    {
        int FindProductHistory();
    }

public class Dependency : IDependency
    {
        public Dependency()
        {

        }

        public int FindProductHistory()
        {
            return 1;
        }
    }
I have kept declaration and definition simple to understand

Now in old ways we would be creating object of Dependency class and then pass the object into constructor calling of ProductHistoryClass which makes our classes tightly coupled and difficult to modify in future
So how to make use of anti-pattern here.

Don’t worry I got it covered J

In you calling class you can do like

public static void WithParam()
        {
            ProductHistoryClass prdCls = (ProductHistoryClass)Activator.CreateInstance(classType,ServiceLocator.Resolve<IDependency>());
            int res = prdCls.FindProductHistory();
        }

Here I have used reflection to create object or ProductHistoryClass and ServiceLocator to resolve our dependency

And just by these simple lines of code we are able to inject dependency in plain old c# classes
Now I would like you guys to experiment with this.
This is it from this blog hold tight till I write next one
J
If you like the blog then please like it and leave a comment
Do not forget to subscribe.
Thank you.

Thursday, 10 October 2019

Setting and getting private variable of a class without properties



Setting or getting private variable is quite a corner scenario while developing a new application and mainly developer will always find a way to access the private variable. But there is a direct way where we can access the private variable using reflection without much of hassle

What is the real-world scenario where I would need to access a private variable?
In real-world scenario I don’t you may ever need to access private variable in your own application. If you do then you’ll simply change it access specifier.

     UTC or TDD development you might find yourself in scenario where you need to access or change the value of private variable of a class. In UTCs we often do such things to by check some negative scenario or for some other reasons.

      Third party dll or library, we may need to access private variable of a third-party variable

These are the cases where I need to access private variable

How could we achieve this?
Quite simple, you just need to do some R&D with reflection classes and need to spend some time with Type class provided in .net framework.

Example-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossProjectDemo
{
    public class Car
    {
        private string name = string.Empty;
        private string prvtVariable = "PrivateVariable";

        public string Name {
            get { return name; }
            set { name = value; }
        }

        public Car()
        {
        
        }
    }
}

As you can see there two private variables
1. name
2.
prvtVariable

While name is being accessed in a public Property Name, prvtVariable will be accessed directly

Let’s see how
First in your application import Reflection namespace
using System.Reflection;

Then follow the code-
Car c = new Car();
Type typ = typeof(Car);
FieldInfo type = typ.GetField("prvtVariable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var value = type.GetValue(c);



In above image you can see that the type.IsPrivate is true which means variable is private



And we can access its value into the value variable.
That’s easy right.
Now, Let’s move towards how we can change its value. Lets’ make few changes to the existing code
Just add one-line right after Getvalue() method call

var value = type.GetValue(c);
type.SetValue(c, "Not so much");

and now you have changed the value of a private variable



Easy, isn’t it. So here we’re accessing private variables through reflection.
For more detail info like this visit this
Thanks

Send All Azure App Insights Or Logs Data To Tool Like Datadog

  Introduction Microsoft Azure provides a variety of insights and monitoring logs that you can monitor to check the state of a resource and ...