So, I've found several answers to half of my issue, but the two pieces I am trying to combine seem to put things together in ways that others haven't run into before (or at least they're not posting about it).
My objective is to be able use my code like this:
ModelFactory[DataModelX].DataY
What I know I can get is this:
ModelFactory.Get<DataModelX>.DataY
Admittedly not that much different, but at this point I've felt that I've gotten close and want to see if I can get it.
My current classes are:
public class ModelFactory<T> : : IReadOnlyList<IDataModel> where T : IDataModel
private static ModelFactory<IDataModel> _instance;
private ModelFactory() {}
public static ModelFactory<IDataModel> ModelFactory => _instance ?? (_instance = new DataModelFactory<IDataModel>());
private readonly List<IDataModel> _models = new List<IDataModel>();
private T GetHelper(Type type)
{
// check if model exists
foreach (var model in _models.Where(model => model.GetType() == type))
{
// model exists, return it
return (T) model;
}
// model doesn't exist, so we need to create it
var newModel = CreateModel(type);
_models.Add(newModel);
return newModel;
}
public T this[
Type type
] {
get
{
return GetHelper(type);
}
}
And DataModels like so:
public class DataModel1 : IDataModel
public string Alpha {get; set;}
public int Beta {get; set;}
// etc...
public class DataModel2 : IDataModel
public foo Cat {get; set;}
public foobar Dog {get; set;}
// etc...
And so on.
However, when I try to type ModelFactory.ModelFactory[DataModelX].
I can't get access to ModelFactory. The property is obviously public, and contained within the appropriate class. Also, I can't pass the class as a parameter (of course), but I don't know how to adjust the parameter on the array indexing to accept the class as a parameter.
Constaints:
- ModelFactory should be a static class or a singleton.
- DataModels are classes that all implement IDataModel.
- Data properties are unique to each individual DataModel