mercredi 28 novembre 2018

Building complex data class

I have the following builder/factory which abstracts a serializeable model from a class.

public class FooBarFactory : IFooBarFactory
{
    public IFooModel Create(IFoo someClass)
    {
        // some complex model building code here
    }
}

And I have a concrete implementation of IFooModel like so:

public interface IFooModel
{
    string AbstractedData1 { get; }
    string AbstractedData2 { get; }
    int AbstractedData3 { get; }
}

public class ConcreteFooModel : IFooModel
{
    public string AbstractedData1 { get; set; }
    public string AbstractedData2 { get; set; }
    public int AbstractedData3 { get; set; }
    public bool ExtraData1 { get; set; }
}

Now arises the issue, I am struggling to find a way to not reference any concrete implementations in my builder/factory method, e.g.

public class FooBarFactory : IFooBarFactory
{
    public IFooModel Create(IFoo someClass)
    {
        // some complex model building code here
        var model = new ConcreteFooModel(someClass.data1, someClass.data1); // Aaargh
    }
}

Something about this code is smelly to me, perhaps this is the only way, but I don't like the idea of being forced into referencing the concrete implementation to instantiate the data class, IFooModel.

This gets more complex if I now introduce another data holder interface into the IFooModel

public interface IFooModel
{
    string AbstractedData1 { get; }
    string AbstractedData2 { get; }
    int AbstractedData3 { get; }
    IBarData BarData { get; }
}

public interface IBarData
{
    // some data in here
}

Forcing me then to create another concrete reference for the nested interface

public class FooBarFactory : IFooBarFactory
{
    public IFooModel Create(IFoo someClass)
    {
        // some complex model building code here
        IBarData barData = new ConcreteBarData();
        IFooModel model = new ConcreteFooModel(someClass.data1, someClass.data1, barData);
    }
}

Is there a better way to do this while still sticking to the SOLID principle and IoC?

Aucun commentaire:

Enregistrer un commentaire