dI.Hook - Creating a dependency injection container
If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.
Some posts you might want to read would be:
- How to create a HookRepository?
- How to add hooks to HookRepository?
- How to add hooks via configuration file to a HookRepository?
- How to invoke all hooks using dI.Hook?
- How to invoke hooks that satisfy a condition?
- How to retrieve an object of hook?
- How to remove hooks from HookRepository?
- How to ignore hook on specific method?
So this is the ninth article in the series which will deal with creating a dependency injection container and using them in your applications
Creating a dependency container is very simple. You can either choose a default implementation of Container, or you can implement the interface IContainer and write your own definition of container. The implementation you would require to implement would be
- public interface IContainer
- {
- ReadOnlyCollection<dIHook.Objects.ContainerItem> Collection { get; }
- ReadOnlyCollection<Guid> Keys { get; }
- void Register<TClass>() where TClass : class;
- void Register<TClass>(Guid key) where TClass : class;
- void Register<TInterface, TImplementation>() where TImplementation : class, TInterface;
- void Register<TInterface, TImplementation>(Guid guid) where TImplementation : class, TInterface;
- void Register<TInterface>(Guid key, TInterface objectValue);
- void Register<TInterface>(TInterface objectValue);
- TInterface Resolve<TInterface>() where TInterface : class;
- TInterface Resolve<TInterface>(Guid key) where TInterface : class;
- ReadOnlyCollection<Type> Types { get; }
- }
Assuming that you are not implementing this interface and are using the default implementation provided with dI.Hook, you would involve having
- Some business interfaces and entities
- Container having a definition of interface and implemented entities
- Container resolving these entity objects
For the sake of understanding, let’s have a eCommerce case study where we have 3 interfaces –
- ICustomer – Represents customer and his order
- INotifier – Notification service for the customer
- IBillingProcessor – Processor that takes the payment type and processes the billing
So let’s quickly set the stage with these interface and their implementation
- public interface IBillingProcessor
- {
- void ProcessPayment(OnlineOrder order);
- }
- public class BillingProcessor : IBillingProcessor
- {
- private PaymentType _paymentType;
- public BillingProcessor(PaymentType paymentType) {}
- public void ProcessPayment(OnlineOrder order) { }
- }
- public enum PaymentType { Cash,CreditCard}
- public interface ICustomer
- {
- void UpdateCustomerOrder(int customerId, string product);
- }
- public class InternetCustomer : ICustomer
- {
- public InternetCustomer() { }
- public void UpdateCustomerOrder(int customerId, string product) { }
- }
- public interface INotifier
- {
- void SendReceipt(OnlineOrder order);
- }
- public class EmailNotifer : INotifier
- {
- public EmailNotifer() { }
- public void SendReceipt(OnlineOrder order) { }
- }
Now, let’s use the container with these business entities
- Container container = new Container();
- container.Register<IBillingProcessor, BillingProcessor>();
- container.Register<ICustomer, InternetCustomer>();
- container.Register<INotifier, EmailNotifer>();
The above code creates a default objects of the BillingProcessor, InternetCustomer and EmailNotifier. If you want to pass your own object you could write as below (see BillingProcessor object)
- Container container = new Container();
- BillingProcessor billingProcessor = new BillingProcessor(PaymentType.CreditCard);
- container.Register<IBillingProcessor>(billingProcessor);
- container.Register<ICustomer, InternetCustomer>();
- container.Register<INotifier, EmailNotifer>();
Now let’s place an order on an eCommerce website using this container. An eCommerce class would be of no use without a reference to ICustomer, IBillingProcessor and INotifier. So eCommerce class for this example would be
- public class ECommerce
- {
- public void Process(OnlineOrder order)
- {
- _BillingProcessor.ProcessPayment(order);
- _Customer.UpdateCustomerOrder(order.CustomerId,
- order.Product);
- _Notifier.SendReceipt(order);
- Debug.WriteLine("Process called");
- }
- public ECommerce(IBillingProcessor billingProcessor,
- ICustomer customer,
- INotifier notifier)
- {
- _BillingProcessor = billingProcessor;
- _Customer = customer;
- _Notifier = notifier;
- }
- IBillingProcessor _BillingProcessor;
- ICustomer _Customer;
- INotifier _Notifier;
- }
So your eCommerce takes objects of ICustomer, IBillingProcessor and INotifier in the constructor and in the Process method, it
- Calls BillingProcessor to ProcessPayment for the order
- Calls Customer to UpdateCustomerOrder
- Calls Notifier to send notification
Now let’s see how this can be invoked using dI.Hook container that we created
- OnlineOrder onlineOrder = new OnlineOrder()
- {
- CustomerId = 12212,
- EmailAddress = "dihook@Ghanshani.com",
- Price = 400,
- Product = "NewProduct"
- };
- ECommerce commerce = container.Resolve<ECommerce>();
- commerce.Process(onlineOrder);
The above code creates an object of ECommerce class. Now what is important here is to see what internally happens. When you call Resolve method, it tries creating an object of ECommerce class. It finds that its constructor requires objects of IBillingProcessor, ICustomer and INotifier. Now since you have already registered objects of these interfaces the dI.Hook Container retrieves the object from the collection and then will try to create an object of ECommerce. If, however, ECommerce constructor required an un-registered object it will try to create a default object and add it in the container.
So if you explicitly created an object of BillingProcessor with payment type as CreditCard and added them in Container, it will use that object; otherwise, it will use default object of BillingProcessor with payment type as Cash
dI.Hook Container ensures that it resolves the hierarchical dependencies automatically. However if it is unable to resolve for any reason, it will throw an exception.