今際のアリス
今際のアリス

Reputation: 354

DI Question, is this considered loosely coupled?

I am using a factory class here to inject DataAccess and Logging to my BusinessLogic, is this considered loosely coupled?

I am trying to follow the DIP (Dependency inversion principle), and IoC (Inversion of Control) and DIP (Dependency Inversion Principle). I have included my classes and my interfaces in the post.

Note that I am not using a IoC Container, maybe I should?

public interface IDataAccess
{
    void SaveData(string data);
}

public interface ILogger
{
    void Log(string str);
}

class Program
{
    public class Logger : ILogger
    {
        public void Log(string str)
        {
            Console.WriteLine("Logging {0}", str);
        }
    }

    public class BusinessLogic
    {
        ILogger _logger;
        IDataAccess _dataAccess;
        public BusinessLogic(Logger logger, DataAccess DataAccess)
        {
            _logger = logger;
            _dataAccess = DataAccess;
        }
        public void ProcessData()
        {
            _logger.Log("Hello!");
            _dataAccess.SaveData("Hey");
        }
    }

    public class DataAccess : IDataAccess
    {
        public void SaveData(string data)
        {
            Console.WriteLine("Saving {0}", data);
        }
    }

    static class Factory
    {
        public static DataAccess getDataAccess()
        {
            return new DataAccess();
        }
        public static Logger getLogger()
        {
            return new Logger();
        }
    }

    static void Main(string[] args)
    {
        BusinessLogic bLogic = new BusinessLogic(Factory.getLogger(), Factory.getDataAccess());
        bLogic.ProcessData();
        Console.ReadLine();
        Console.WriteLine("Hello World!");
    }
}

Upvotes: 0

Views: 48

Answers (1)

Nkosi
Nkosi

Reputation: 247451

is this considered loosely coupled?

Your business logic class is tightly coupled to implementation details (concrete classes)

BusinessLogic

//...

ILogger _logger;
IDataAccess _dataAccess;

public BusinessLogic(Logger logger, DataAccess DataAccess) { //<-- classes?
    _logger = logger;
    _dataAccess = DataAccess;
}

//...

instead of abstractions (interfaces/abstract classes).

BusinessLogic (Updated)

//...

ILogger _logger;
IDataAccess _dataAccess;

public BusinessLogic(ILogger logger, IDataAccess DataAccess) { //<-- abstractions
    _logger = logger;
    _dataAccess = DataAccess;
}

//...

The updated example above is now decoupled from implementation concerns/details.

Main appears to be your composition root so just new up all the required dependencies and explicitly inject them using pure DI, so there really is no need for the static factory either.

static void Main(string[] args) {
    ILogger logger = new Logger();
    IDataAccess dataAccess = new DataAccess();
    BusinessLogic bLogic = new BusinessLogic(logger, dataAccess);
    bLogic.ProcessData();
    Console.ReadLine();
    Console.WriteLine("Hello World!");
}

Note that I am not using a IoC Container, maybe I should?

Whether you should or should not is debatable and is likely to be answered with opinions, putting it outside the scope of what is acceptable on SO.

Review the linked article and make an informed decision on your own.

Upvotes: 3

Related Questions