Skip to content

Chirontex/garner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Garner v1.0.2

Garner is a simple DI container implementation for .NET.

Installation

Get it from NuGet.

How to use

Create your services:
using System;

namespace SampleApp;

public class ConsoleSayingService
{
	public void Say(string phrase)
	{
		Console.WriteLine(phrase);
	}
}

public class Tom
{
	protected ConsoleSayingService _consoleSayingService;
	
	public Tom(ConsoleSayingService consoleSayingService)
	{
		this._consoleSayingService = consoleSayingService;
	}

	public void SayName()
	{
		this._consoleSayingService.Say("My name is Tom.");
	}
}
Implement Garner.Interfaces.IServicesDictionaryFactory:
using Garner.Interfaces;
using SampleApp;
using System.Collections.Generic;

namespace SampleApp.Factory;

public class ServicesDictionaryFactory : IServicesDictionaryFactory
{
	public Dictionary<string, dynamic> createServicesDictionary()
	{
		var dictionary = new Dictionary<string, dynamic>()
		{
			["console_saying_service"] = new ConsoleSayingService(),
		};
		
		dictionary.Add("Tom", new Tom(dictionary["console_saying_service"]));
		
		return dictionary;
	}
}
Create container and use it:
use Garner;
use SampleApp.Factory;

namespace SampleApp;

class Program
{
	public static void Main(string[] args)
	{
		Container.ServicesDictionaryFactory = new ServicesDictionaryFactory();
		Container container = Container.GetInstance();
		
		Tom tom = container.Get("Tom");
		tom.SayName(); // output "My name is Tom." in the console
	}
}

About

I know about Autofac, but...

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages