How to use dryioc container from main app in class library

Del

I am new to dryioc so excuse the ignorance:)

I would like to understand what the correct approach would be for the following,

Creating a console app that creates instance of dryioc container. Register a logger in the container (as singleton). Then instantiate a class from a class library (separate project) to be used in the console app and be able to reference the container (main app) from the class library to get an instance of logger. So the class library will utilise any logger registered in console app.

I'd prefer not to pass container as part of constructors in class library.

Thanks for any assistance.

dadhi

Something like this. Probably true for any other container (or even without container):

namespace Core
{
    public interface ILogger 
    {
        void Log(string message);
    }
}

namespace Lib
{
    using Core;

    public class Foo
    {
        readonly ILogger _logger;
        public Foo(ILogger logger) 
        {
             _logger = logger;
        }

        public void DoSomething()
        {
             _logger.Log("About to do something ...");
        }
    }
}

namespace ConsoleApp
{   
    using System;

    using Core;
    using Lib;

    using DryIoc;

    public class ConsoleLogger : ILogger 
    {
        public void Log(string message)
        {
            Console.WriteLine(message);
        }
    }

    public class Program
    {
        public static void Main()
        {
            var c = new Container();

            c.Register<ILogger, ConsoleLogger>(Reuse.Singleton);
            c.Register<Foo>();

            var foo = c.Resolve<Foo>();
            foo.DoSomething();
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use dryioc container from main app in class library

From Dev

Use domain class from main app in plugin

From Dev

How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

From Dev

How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

From Dev

How to extend a class from main application in an android library activity?

From Dev

Use reflection to get app.dispatcher from class library

From Dev

Include a container from main() in a class method

From Dev

Java - How to use a variable from a method when imported into the main class

From Dev

How to use Main from another Class in as3

From Dev

EF Core in class library and use from API project - How to pass config information to class library?

From Dev

give inputs to android library app from main app

From Dev

How to use DI container in reusable library?

From Dev

How to use Container in find operation in Jemmy library?

From Dev

Can a class library use function stored in main project?

From Dev

Access Class Library from Universal Shared App

From Dev

Scala: use case class from Main

From Dev

how to use method of main class from the extended class and avoiding constructor loop

From Dev

Launch an activity in main app from an Android Library Module

From Dev

Argument passed from Main App to Library project with Gradle

From Dev

Icons from Resource Dictionary in class library are not shown in the main application window

From Dev

Use dependency from library module in app module

From Dev

Yii - How to use a class inside config/main?

From Dev

How is a DryIoc container setup if you need different instances of same interface?

From Dev

DryIOC Container Contents

From Dev

How to use NSUserDefaults to store a container class

From Dev

Dynamically load a class from a library and use it after closing the library

From Dev

How can get access to Unity container in a class library ActionFilterAttribute?

From Dev

How to use a library that imports memcache in App Engine

From Dev

How to use c static library in iOS app?

Related Related

  1. 1

    How to use dryioc container from main app in class library

  2. 2

    Use domain class from main app in plugin

  3. 3

    How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

  4. 4

    How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

  5. 5

    How to extend a class from main application in an android library activity?

  6. 6

    Use reflection to get app.dispatcher from class library

  7. 7

    Include a container from main() in a class method

  8. 8

    Java - How to use a variable from a method when imported into the main class

  9. 9

    How to use Main from another Class in as3

  10. 10

    EF Core in class library and use from API project - How to pass config information to class library?

  11. 11

    give inputs to android library app from main app

  12. 12

    How to use DI container in reusable library?

  13. 13

    How to use Container in find operation in Jemmy library?

  14. 14

    Can a class library use function stored in main project?

  15. 15

    Access Class Library from Universal Shared App

  16. 16

    Scala: use case class from Main

  17. 17

    how to use method of main class from the extended class and avoiding constructor loop

  18. 18

    Launch an activity in main app from an Android Library Module

  19. 19

    Argument passed from Main App to Library project with Gradle

  20. 20

    Icons from Resource Dictionary in class library are not shown in the main application window

  21. 21

    Use dependency from library module in app module

  22. 22

    Yii - How to use a class inside config/main?

  23. 23

    How is a DryIoc container setup if you need different instances of same interface?

  24. 24

    DryIOC Container Contents

  25. 25

    How to use NSUserDefaults to store a container class

  26. 26

    Dynamically load a class from a library and use it after closing the library

  27. 27

    How can get access to Unity container in a class library ActionFilterAttribute?

  28. 28

    How to use a library that imports memcache in App Engine

  29. 29

    How to use c static library in iOS app?

HotTag

Archive