1

I am working on a C# project and I need to manage a local cache of data and present it on a GUI.

For example I have:

    public class DataFromOtherLibrary

Now I want make a class to help translate the information I need out of it

    public class MyDataModel
    {
            private DataFromOtherLibrary cache;
            public MyDataModel ( DataFromOtherLibrary Source)
            {
                    cache = Source;
            }
            public long Field1 { get { return cache.SomeField; } }
            public long Field2 { get { return cache.OtherFiled; } }
     }

Now the issue I have is I need to create a MyDataModel for every DataFromOtherLibrary it would be nice to have a single translator class. I'm not sure how to do it and still implement properties (which I need to data-bind to).

Thanks Matt

Evk
  • 98,527
  • 8
  • 141
  • 191
MDK
  • 495
  • 1
  • 6
  • 18
  • 2
    Sounds like you need a Factory that returns an `IMyDataModel`. But that's somewhat a shot in the dark due to lack of information. – Jonesopolis May 11 '16 at 12:43
  • Agree, your goals are not clear – Liam May 11 '16 at 12:44
  • I also think you should use a Provider(Factory) Class which returns you a List with the MyDataModle Objects in it. – FoldFence May 11 '16 at 12:45
  • I think your after an [abstract factory pattern](http://www.dofactory.com/net/abstract-factory-design-pattern)? – Liam May 11 '16 at 12:48
  • 1
    As for me it seems like OP needs a proxy - class _with properties_ (because he needs to bind to them in UI library like WPF) which serves as a proxy for another class (DataFromOtherLibrary). – Evk May 11 '16 at 12:56

1 Answers1

1

You should use a Provider with all your DataModels in it Like:

public class MyDataModelProvider
{
    public List<MyDataModel> DataModelList { get; set; }

    MyDataModelProvider()
    {
        loadDataModelList();
    }

    private void LoadDataModel()
    {
        foreach (Cachobject c in Cache)
        {
            this.DataModelList.Add(new MyDataModel(c.valueA,c.valueB));
        }
    }
}

and for this Provider you also need a Factory Like:

[Singleton(true)]
public class DataModelListProviderFactory
{
    private static DataModelListProvider dataListProvider;
    public DataModelListProvider getInstance()
    {
        if (dataListProvider == null)
        {
            dataListProvider = new DataModelListProvider();
           return dataListProvider;
        }
        else
            return dataListProvider;
    }
}

Because now you have a single Spot with all your DataModels and you only must recieve them once. You can also easily search the List for a specific Model if you have a case or you show all the Data in a View.

You can read here more about Factory Patterns.

Hope that helps.

Community
  • 1
  • 1
FoldFence
  • 2,674
  • 4
  • 33
  • 57