I know this is a strange name, and I know that inheritance is not possible with enumerations, so let me explain.
If I have the following classes:
- Fruit (abstract)
- Apple (concrete - derived from fruit)
- Orange (concrete - derived from fruit)
And I have the following method implemented with generics:
public ICollection<T> FindFruit<T>() where T : Fruit
{
return _fruitRepository
.Fruits
.OfType<T>()
.ToList();
}
And I use it as follows:
var apples = _fruitServices.FindFruit<Apple>();
All perfectly.
Now I have the following enumeration:
public enum FruitAssociations
{
Color,
Manufacturers
}
Basically, Fruits can have many associations that determine which associations to include in the result from my repository.
So the method FindFruitabove looks something like this:
public ICollection<T> FindFruit<T>(FruitAssociations[] assocs) where T : Fruit
{
return _fruitRepository
.Fruits
.OfType<T>()
.IncludeAssocs(assocs)
.ToList();
}
Here is this extension method:
public static ObjectQuery<Fruit> IncludeAssocs(this ObjectQuery<Fruit> source, FruitAssociations[] assocs)
{
if (assocs.Contains(FruitAssociations.Color))
query = query.Include("Color");
}
Also works great. The problem that I am facing right now is to have associations with a particular Fruit.
eg
public enum OrangeAssociations
{
OrangeFamilies
}
, FindFruit, , T.
, :
var oranges = _fruitServices.FindFruit<Orange>(new[] { OrangeAssociations.OrangeFamilies });
var apples = _fruitServices.FindFruit<Apple>(new[] { AppleAssociations.AppleFamilies });
, , FindFruit , , T.
, , , Entity Framework 4 , , .Include ( , ). , , , , .Include.
, , :
public ICollection<T> FindFruit<T>(FruitAssociations<T> assocs) where T : Fruit
:
var associations = new FruitAssociations<Orange>(OrangeAssociations.OrangeFamilies);
var apples = _fruitServices.FindFruit<Apple>(associations);
, .
, /.