Working with TinyIOC in .NET Framework

.NET

What I like about TinyIOC is that you can easily embed it into your code and the setup is very quick.

  • Download the file and add it to the project
  • Create an interface and an implementation, e.g. IMyService, MyService
  • Create a class that initializes the container and provides a reference to the container instance
        	
        internal class DIContainer
        {
    
            public static Lazy<TinyIoCContainer> Instance = new(() =>
            {
                TinyIoCContainer container = TinyIoCContainer.Current;
    
                container.Register<IMyService, MyService>().AsSingleton();
    
                return container;
            });
    
        }
            
        
  • Get an instance in code by calling:
        	
       IMyService myService = DIContainer.Instance.Value.Resolve<IMyService>();
            
        

Post a Comment

Previous Post Next Post