Question

I have a service where certain other services can report their status to. By status I don't mean whether they are running or not, but whether they have received data. The point is that other services can ask if data they need has been received.

I have two possible options of implementing this.

One is as follows:

public interface IDataPrerequisiteFulfillmentService
{
    bool ArePrerequisitesFulfilled(params DataPrerequisite[] dataPrerequisites);
    void ReportDataPrerequisiteFulfilled(DataPrerequisite dataPrerequisite);
}

However, that would mean I would need an extra dependency in any service that needs to report to this service. Some services already have multiple dependencies and I like to keep dependencies to a minimum.

Another way to approach this would be to do the following:

public interface IDataPrerequisiteFulfillmentService
{
    bool ArePrerequisitesFulfilled(params DataPrerequisite[] dataPrerequisites);
}

public interface IDataPrerequisiteService
{
    event EventHandler<DataPrerequisiteEventArgs> PrerequisiteFulfilled;
}

public class DataPrerequisiteFulfillmentService : IDataPrerequisiteFulfillmentService
{
    private IList<DataPrerequisite> fulfilledPrerequisites = new List<DataPrerequisite>();

    public IDataPrerequisiteFulfillmentService(IEnumerable<IDataPrerequisiteService> services)
    {
        // receive services via injection
        foreach (var service in services)
        {
            service.PrerequisiteFulfilled += (s, e) => 
            {
                _fulfilledPrerequisites.Add(e.DataPrerequisite);
            }
        }
    }

I prefer option 2. It also inverts the dependencies. However, it makes for an interface that doesn't communicate the fact that the implementation of IDataPrerequisiteFulfillmentService will also need input.

    public bool ArePrerequisitesFulfilled(params DataPrerequisite[] dataPrerequisites)
    {
        // check _fulFilledPrerequisites here
    }
}

Is it ok to have an interface that only communicates half of how the implementation works?

Was it helpful?

Solution

It is absolutely fine to have an interface that doesn't communicate how the implementation works. In fact that is the point of an interface, to hide implementation details!

Your second option is fine in this scenario.

Licensed under: CC-BY-SA with attribution
scroll top