0

1. Main Method

class Program
    {
        static void Main(string[] args)
        {
            ILogger _log = new Logger();
            IDataAccess _da = new DataAccess();

            BusinessLogic bs = new BusinessLogic(_log,_da);
            bs.ProcessData();
            Console.ReadLine();
        }
    }

2. BusinessLogic Class implements IBusinessLogic interface

class BusinessLogic : IBusinessLogic
    {
        ILogger _log;
        IDataAccess _dataAccess;

        public BusinessLogic(ILogger log, IDataAccess dataAccess)
        {
            _log = log;
            _dataAccess = dataAccess;
        }

        public void ProcessData()
        {
            _log.Log("Start");
            Console.WriteLine("process");
            _dataAccess.LoadData();
            _dataAccess.SaveData("processInfo");
            _log.Log("Finish");
        }
    }

3. Logger Class implements ILogger interface

class Logger : ILogger
    {
       public void Log(string p)
        {
            Console.WriteLine(p);
        }
    }

4. DataAccess Class implements IDataAccess interface

class DataAccess : IDataAccess
    {
        public void LoadData()
        {
            Console.WriteLine("Loaddata");
        }      

        public void SaveData(string name)
        {
            Console.WriteLine("Saving Data");
        }
    }

4. Interfaces

interface IBusinessLogic
    {
        void ProcessData();
    } 

interface ILogger
    {
        void Log(string p);
    }  

interface IDataAccess
    {
        void LoadData();
        void SaveData(string name);
    }

With respect to the above code i have 3 interfaces for BusinessLogic, Logger and DataAccess for all 3 classes respectively.....

If this is the correct implementation for Dependency Injection then let me know the improvements or suggestions for the code if any.....

OR incase it is NOT correct, suggest me the modifications to achieve the same.

Thanks In Advance!

chrisis
  • 1,983
  • 5
  • 20
  • 17
PPB
  • 101
  • 4
  • 1
    Yes, it is valid DI, but as things get more complex it is common to use an Inversion of Control container to handle lifetimes and nested dependencies. – Crowcoder Jun 06 '20 at 12:07
  • Thanks @Crowcoder, any `improvements/suggestions` within this code? – PPB Jun 06 '20 at 12:08
  • 3
    Well, all it does it write to the console so there is not much to critique, and code review is off topic on SO but you [can ask here](https://codereview.stackexchange.com/) – Crowcoder Jun 06 '20 at 12:15
  • @Crowcoder Out if these `classes` which is `Client class` , `Service class` and `Injector class`? If you could help me with this... – PPB Jun 06 '20 at 12:22
  • 2
    Sounds like homework. What do you think the answer is? – Crowcoder Jun 06 '20 at 12:55
  • @Farhad-Taran Can you please directly edit the improvement said. – PPB Jun 06 '20 at 16:32
  • @Crowcoder `Client class - Program`, `Service class - Logger and DataAccess`, `Injector class - BusinessLogic class`? – PPB Jun 06 '20 at 16:39
  • I wouldn't go with that... A client is something that consumes a service. – Crowcoder Jun 06 '20 at 17:04
  • @Crowcoder `Client class - BusinessLogic class`, `Service class - Logger and DataAccess`, `Injector class - Program class`, this? – PPB Jun 07 '20 at 09:16

0 Answers0