Reputation: 2117
Assuming I have the following POCO entity:
public class SomeEntity
{
public int SomeProperty { get; set; }
}
and the following repository
public class SomeEntityRepository
{
Context _context;
public SomeEntityRepository(Context context)
{
_context = context;
}
public List<SomeEntity> GetCrazyEntities()
{
return _context.SomeEntities.Where(se => se.SomeProperty > 500).ToList();
}
}
Then for some reason I have to implement a computed property on SomeEntity like:
class SomeEntity
{
...
public List<SomeEntity> WellIDependOnMyOnRepositry()
{
...
return theRepository.GetCrazyEntities().Where(se => se.SomeProperty < 505).ToList();
}
}
How can I deal with the POCO entity being aware of the repository/context using a proper UnitOfWork implementation?
I've been looking into IoC and dependency injection, but I'm a little too stupid to understand it out of the bat.
Some enlightenment?
Upvotes: 0
Views: 321
Reputation: 6607
Without having read the update you mention in your comment I could say that you should get the Crazy Entities from the repository in some kind of Domain Service object, do whatever calculations you need and assign the result to your Entity.
Also, ideally, if you want to look into dependency injection (with our without an IoC container) your repository should implement an interface.
Something like the following:
public interface ISomeEntityRepository
{
List<SomeEntity> GetCrazyEntities();
}
public class SomeEntityRepository : ISomeEntityRepository
{
// ... Implementation goes here.
}
public class MyDomainService
{
private readonly ISomeEntityRepository Repository;
public MyDomainService(ISomeEntityRepository repository)
{
Repository = repository;
}
public SomeEntity WorkWithCrazyEntity()
{
var something = Repository.GetCrazyEntities();
var result = //.... do all sort of crazy calculation.
var someEntity = new SomeEntity();
someEntity.CalculatedProperty = result;
return someEntity;
}
}
Hope this gives you a few ideas. Maybe after you update your question I can get better in context of what is it that you need.
Regards.
Upvotes: 1