mercredi 22 janvier 2020

What is the best practice to avoid putting an "initialize" method in an interface for a data service?

I have an interface for a data service and object repositories in my domain layer, but as I write the implementation for these interfaces, I find that I am needing an "initialize" or "fill" method in the case of an XmlDataService implementation. I need to read the xml, then "load" the repositories from the xml. This feels a lot like a leaky abstraction. What is the best practice here or I am way off base?

My Interface:

public interface IDataService
{
    event EventHandler<string> StatusUpdated;
    IRepository<Points.Core.Point> GetPoints();
    ...

    void Fill();
    Task FillAsync();
}

XmlDataService implementation:

    public class XmlDataService : IDataService
{
    public event EventHandler<string> StatusUpdated;
    public void OnStatusUpdate(string s) => StatusUpdated?.Invoke(this, s);

    public IRepository<Point> GetPoints() => _pointsRepo;
    ...

    public void Fill()
    {
        using (var ds = new DataSet())
        {
            OnStatusUpdate("Reading dataset...");
            ds.ReadXml(_reader, XmlReadMode.ReadSchema);
            ds.EnforceConstraints = false;
            OnStatusUpdate("Loading points...");                
            _pointsRepo.Initialize(ds);
            ...
        }
    }

    public XmlDataService(XmlReader xmlReader)
    {
        _reader = xmlReader ?? throw new ArgumentNullException("xmlReader");
        //instantiate repos
        _pointsRepo = new PointsRepository();
        ...
    }
}

Aucun commentaire:

Enregistrer un commentaire