Dependency injection 工作单元和实体框架-计算属性

Dependency injection 工作单元和实体框架-计算属性,dependency-injection,entity-framework-4.1,inversion-of-control,unit-of-work,Dependency Injection,Entity Framework 4.1,Inversion Of Control,Unit Of Work,假设我拥有以下POCO实体: public class SomeEntity { public int SomeProperty { get; set; } } 以及以下存储库 public class SomeEntityRepository { Context _context; public SomeEntityRepository(Context context) { _context = context; } publ

假设我拥有以下POCO实体:

public class SomeEntity
{
    public int SomeProperty { get; set; }
}
以及以下存储库

public class SomeEntityRepository
{
    Context _context;
    public SomeEntityRepository(Context context)
    {
        _context = context;
    }

    public List<SomeEntity> GetCrazyEntities()
    {
        return _context.SomeEntities.Where(se => se.SomeProperty > 500).ToList();
    }
}
public类sometentityrepository
{
语境(Context)语境;;
公共SomeEntityRepository(上下文)
{
_上下文=上下文;
}
公共列表GetCrazyEntities()
{
返回_context.SomeEntities.Where(se=>se.SomeProperty>500).ToList();
}
}
出于某种原因,我必须在某个实体上实现计算属性,如:

class SomeEntity
{
    ...
    public List<SomeEntity> WellIDependOnMyOnRepositry()
    {
        ...
        return theRepository.GetCrazyEntities().Where(se => se.SomeProperty < 505).ToList();
    }
}
class-SomeEntity
{
...
公共列表WellidependonMyonRepository()
{
...
返回respository.GetCrazyEntities().Where(se=>se.SomeProperty<505.ToList();
}
}
如何使用适当的UnitOfWork实现来处理POCO实体知道存储库/上下文的情况

我一直在研究IoC和依赖注入,但我有点太蠢了,根本无法理解它


一些启示?

如果没有阅读您在评论中提到的更新,我可以说您应该从存储库中获取某种域服务对象中的疯狂实体,执行您需要的任何计算,并将结果分配给您的实体

此外,理想情况下,如果您想研究依赖注入(使用我们的不带IoC容器的系统),那么您的存储库应该实现一个接口

如下所示:

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;
   }
}
公共接口IsomentityRepository
{
列出GetCrazyEntities();
}
公共类SomeEntityRepository:ISomeEntityRepository
{
//…实现就在这里。
}
公共类MyDomainService
{
私有只读等实体存储库;
公共MyDomainService(IsoEntityRepository存储库)
{
存储库=存储库;
}
public SomeEntity与CrazyEntity()合作
{
var something=Repository.GetCrazyEntities();
var结果=/…进行各种疯狂的计算。
var someEntity=new someEntity();
someEntity.CalculatedProperty=结果;
返回实体;
}
}
希望这能给你一些想法。也许在你更新你的问题后,我可以更好地了解你需要什么


问候。

所以。。什么样的“原因”使您希望在实体中保留对存储库的引用?现在,WellIDependOnMyRepository方法似乎属于某个实体Repository,而不是某个实体。是的,这个例子没有反映我遇到的真正问题,我正匆忙离开工作。你是对的。当我在家时,我会用更准确的代码编辑我的问题,因为我相信这会让我得到更好的答案。