-3

I Know IoC is a design principle which recommends the inversion of different kinds of controls in object-oriented design to achieve loose coupling between application classes. But I have confilct with the following code:

  static void Main(string[] args)
        {
            ProductService ProductService = new ProductService(new LogInDB());
            ProductService.Log();

            Console.ReadKey();
        }

  public class ProductService
        {
            private readonly Ilog log;

            public ProductService(Ilog _log)
            {
                log = _log;
            }

            public void Log()
            {
                log.Log();
            }
        }
        public interface Ilog
        {
            void Log();
        }
        public class LogInFile : Ilog
        {
            public void Log()
            {
                Console.WriteLine("Log Into File");
            }
        }

        public class LogInDB : Ilog
        {
            public void Log()
            {
                Console.WriteLine("Log Into Data Base");
            }
        }

What is difference between previous and next code In the first code I used IOC (and added product service) but next I'm using just late binding but i see IOC not added any value

static void Main(string[] args)
        {
            Ilog logObj = new new LogInDB();
            logObj.Log();

 //I still able to using LogInDB
               //Ilog logObj = new new LogInDB();
               //logObj.Log();

            Console.ReadKey();
        }

       
        public interface Ilog
        {
            void Log();
        }
        public class LogInFile : Ilog
        {
            public void Log()
            {
                Console.WriteLine("Log Into File");
            }
        }

        public class LogInDB : Ilog
        {
            public void Log()
            {
                Console.WriteLine("Log Into Data Base");
            }
        }
Dev Net
  • 21
  • 4
  • If you aren't sure why you would use IoC then you need to read up on it in more detail. There are different ways to inject dependencies - with constructor injection the idea is that you know what dependencies are required to full create a valid object of the given type. Property injection is usually reserved for optional dependencies since they aren't required for instantiation. Note that in your second piece of code you aren't using late binding - you are just implementing your logging code per-type (late binding would be using reflection - everything is early bound in c# as far as I know) – Charleh Aug 24 '20 at 13:13
  • Read this question I answered a while back for more info: https://stackoverflow.com/questions/29890223/static-class-to-dependency-injection/29892572#29892572 – Charleh Aug 24 '20 at 13:14
  • This question indicates that maybe you should do some more research into what role interfaces play in IOC and what IOC actually is. the internet is full of resources that will explain it better for example here is a [youtube video](https://www.youtube.com/watch?v=NnZZMkwI6KI) that explains the concept. – CobyC Aug 24 '20 at 13:20
  • `What is difference between previous and next code` The first code block has `ProductService`. The latter doesn't. – mjwills Aug 24 '20 at 13:53

1 Answers1

0

This depends on your defintion of value. One advantage of IoC would be a better testability of your code, which many would argue adds a lot of value. You can easily inject mocked classes into your test code and only test the class you want to test.

By the way your example is not compileable because of the line Ilog logObj = new new LogInDB();

andy meissner
  • 1,202
  • 5
  • 15