How do I mimic the functionality of IQueryable <T> .Include (expression <Func <T, TProperty >>) in my custom collection?
Firstly, a little background. I am developing a REST API using ASP.NET Web API and Entity Framework 5, however the system requirements are such that there are several levels of logic between my ApiControllers and my DbContext. These logic levels include disconnecting my objects from DbContext, applying sets of hypothetical changes to objects in memory (the process that I call materializing a set of changes), and then allowing users to check the new state of the system if these changes get applied. The new state of objects is not saved to the database immediately. Instead, the materialization is stored in memory on a web server, and users can check the current data or one of the many materializations of many change sets.
Now for my problem.
public interface IIdentifiable
{
long Id { get; set; }
}
public class Foo : IIdentifiable
{
public long Id { get; set; }
public string Name { get; set; }
public List<Bar> Bars { get; set; } // Navigation Property
}
public class Bar : IIdentifiable
{
public long Id { get; set; }
public string Name { get; set; }
public long FooId { get; set; } // Foreign Key Property
public Foo Foo { get; set; } // Navigation Property
}
public class Materialization
{
IEnumerable<Foo> Foos { get; set; }
IEnumerable<Bar> Bars { get; set; }
}
public interface IRepository<TItem> : IQueryable<TItem>, ICollection<TItem>, IDisposable
where TItem : class, IIdentifiable
{
IRepository<TItem> Include<TProperty>(Expression<Func<TItem, TProperty>> path);
// Other methods
}
public class MateriailizationRepository<TItem> : IRepository<TItem>
where TItem : class, IIdentifiable
{
private Materialization _materialization;
public MateriailizationRepository(Materialization materialization)
{
_materialization = materialization;
}
public IRepository<TItem> Include<TProperty>(Expression<Func<TItem, TProperty>> path)
{
// Populate navigation property indicated by "path"
}
// Other methods
}
, , Foo , Bar.Foo Foo.Bars , . , Materialization.Foos Materialization.Bars , , (.. List<T> s). - ApiController.
public IQueryable<Foo> Get (bool includeBars = false)
{
Materialization materialization;
// Materialize
using (IRepository<Foo> repository = new MateriailizationRepository<Foo>(materialization))
{
IRepository<Foo> query = repository;
if (includeBars)
query = query.Include(f => f.Bars);
return query;
}
}
MateriailizationRepository<Foo> Foo, Materialization, Bar Materiailization.Bars .
MateriailizationRepository.Include(), IQueryable.Include()?
:
- MaterializationRepositories , Effort, .
- "" "". , . , , , . , , IEnumerable . , ( , ), .
, , , - switch , .
, , , .