user122222
user122222

Reputation: 2419

Inject appropriate class C#

I need some advice on how to better structure my classes.

I have generic interface and class:

public interface IReader
{
        IEnumerable<Change> ReadChanges(string log, int count, string from);
}
public class Reader : IReader
    {
        private readonly MongoDb _db;

        public StageChangesReader(MongoDb db)
        {
            _db = db;
        }

        public IEnumerable<Change> ReadChanges(string log, int count, string from)
        {
             //....
        }
    }

Then I made three other classes that use this reader (they implement the same method and the only difference is log that I pas) something like this:

 public class SpecificReader
    {
        private readonly IReader _reader;

        public SpecificReader(IReader reader)
        {
            _reader = reader;
        }

        public IEnumerable<Change> ReadChanges(int count, string from)
        {
            return _changesReader.ReadChanges("specific log", count, from);
        }
    }

finally in the third class I want to inject these and based on the type use specific logging in the function:

var changes = _type == MyType.SpecificReader
                ? _specificReader.ReadSpecific(_count, from)
                : _somethingElse.ReadSomethingElse(_count, from);

I would like to make this more generic that instead of having if I could pass this one generic method.

so it would be like:

var changes = _Reader.ReadChanges(_count, from)

Is that possible?

Upvotes: 0

Views: 44

Answers (1)

FArk
FArk

Reputation: 41

Considering adding a method in your interface and either overwriting it in your derived concrete classes, so you can call IConcreteClass.LogMethod() on all of them, making them responsible for the way they log.

Upvotes: 1

Related Questions