0

假设我有以下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(); 
    } 
} 

然后由于某种原因,我要实现对计算性能SomeEntity like:

class SomeEntity 
{ 
    ... 
    public List<SomeEntity> WellIDependOnMyOnRepositry() 
    { 
     ... 
     return theRepository.GetCrazyEntities().Where(se => se.SomeProperty < 505).ToList(); 
    } 
} 

我该如何处理POCO实体意识到回购使用适当的UnitOfWork实现的itory/context?

我一直在寻找IoC和依赖注入,但我有点太愚蠢,无法理解它的蝙蝠。

一些启示?

+1

那么..什么是可以让你想要在你的Entity中引用仓库的“某种原因”?现在,WellIDependOnMyRepository方法看起来像属于SomeEntityRepository,而不是SomeEntity – surfen 2012-03-30 21:42:40

+0

是的,这个例子并没有反映我所遇到的真正问题,而是我急于离开工作。你是对的。当我回家时,我会用更准确的代码编辑我的问题,因为我确信它会导致我获得更好的答案。 – 2012-03-30 22:13:03

回答

1

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

此外,理想情况下,如果您想查看依赖注入(无需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; 
    } 
} 

希望这给你的一些想法。也许在你更新你的问题后,我可以在你需要什么的情况下变得更好。

问候。