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; 
    } 
} 

希望這給你的一些想法。也許在你更新你的問題後,我可以在你需要什麼的情況下變得更好。

問候。