I’m trying to build a set of reusable EF models and interfaces. One such interface is called ICategorised, and looks as follows:
public interface ICategorised {
int CategoryID { get; set; }
Category Category { get; set; }
}
And here’s what the Category object looks like:
public class Category {
public int CategoryID { get; set; }
public string Title { get; set; }
public Type Type { get; set; } // Is 'Type' the wrong type for this?
public string Description { get; set; }
}
I quite like the principle of this but I’m slightly stumped on how best to get and set the Type for any object which implements ICategorised. My end goal is the ability to create an object such as:
public class Car : ICategorised {
public int CarID { get; set; }
public string CarName { get; set; }
public int CategoryID { get; set; }
public Category Category { get; set; }
}
.. and somehow be able to query Categories where Type==Car.
I think you should reconsider the current usage of
ICategorizedas the Existential Type. It seems to me that actually this should beCategory, becauseCategoryprovides definitions for everything inICategorizedbut doesn’t actually implement it at present. If you want to keepICategorizedI would makeCategoryimplement it as a first point.Secondly, I would make
Category abstractbecause it shouldn’t be instantiated directly. Then yourCarand any other categorized class you create will derive directly from theabstract Category. This makes themICategorizedinherently too if you change your hierarchy as noted above.That would achieve what you are after I think, and lead to a cleaner hierarchy that is extensible for future types. You can then query on
Typedirectly as each specificCategorysubclass will have an explicitTypeset, such asCar.Hope that helps, or at least is a step in the right direction.