Getting started

Date 2022-01-08
Author Frederik Engelhardt

Introduction

To not overwhelm you with all the functionality of the framework I would like to start with the IoC registry. One of the principals in the framework is to avoid using the new keyword as much as possible. Everything should be abstracted and created by the IoC factory. I say should since this pattern is not always applicable. Don’t worry, the framework will help you in such situations too but there will be another guide for that later.

I will show you how to register your own components and request them from the factory.

Let’s start!

Create a new project

For this demo we can use a Console App.

Choose a name for the project.

Select a supported .net version. For this guide it will be .NET 6.0

Add NuGet package references

Open the NuGet package manager for your solution and search for equadrat.

Install the reference for equadrat.Framework.Core.Interfaces.

Install the reference for equadrat.Framework.Core.

You should have referenced the two base packages.

Create a demo component with abstraction

Add an interface with the name IMyComponent.

namespace GettingStarted
{
    internal interface IMyComponent
    {
        string Concatenate(string value1, string value2);
    }
}

Add a method to the interface.

Add a class with the name MyComponent.

namespace GettingStarted
{
    internal class MyComponent : IMyComponent
    {
        public string Concatenate(string value1, string value2)
        {
            return string.Concat(value1, value2);
        }
    }
}

Implement the interface in the class.

Create an instance of the IoC registry

When you created the solution, Visual Studio created a Programm.cs for you. We will now re-write that from scratch by starting with creating an instance of the IoC registry.

using e2.Framework.Components;
using GettingStarted;
 
var registry = new CoreIOCRegistry();

Register your own component

registry.Register<IMyComponent>().AsSingletonOf<MyComponent>();

Request an instance of your own component

var factory = registry.Factory;
 
var myComponent = factory.GetInstanceOf<IMyComponent>();

Add some output

Console.WriteLine(
    $"Implemented type: {myComponent.GetType()}\n" +
    $"Concatenated result is \"{myComponent.Concatenate("Hello ""World!")}\".");
 
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadKey(true);

The full program

using e2.Framework.Components;
using GettingStarted;
 
var registry = new CoreIOCRegistry();
registry.Register<IMyComponent>().AsSingletonOf<MyComponent>();
 
var factory = registry.Factory;
 
var myComponent = factory.GetInstanceOf<IMyComponent>();
 
Console.WriteLine(
    $"Implemented type: {myComponent.GetType()}\n" +
    $"Concatenated result is \"{myComponent.Concatenate("Hello ""World!")}\".");
 
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadKey(true);

Run the program

References

Source Code on GitHub https://github.com/equadrat/guide-getting-started