2013-03-26 58 views
0

我正在按照Repository PatternUnit Of Work模式的组合使用教程。工作模式单元 - 使用工厂获取软件仓库

我主要有:以上

interface IRepository<T> where T : class 
{ 
    //... 
} 
class Repository<T> where T : class 
{ 
    //Implemented methods 
} 
interface IFooRepository 
{ 
    IQueryable<Foo> GetFoos(); 
} 
class FooRepository : Repository<Foo>, IFooRepository 
{ 
    IQueryable<Foo> GetFoos() {} 
} 

代表我的repositories,在基本意义。然后我有一个Uow类。

public class MyUow 
{ 
    public void Commit() { } 
    public IRepository<Bar> Bars { get { return GetStandardRepo<Bar>(); } } 
    public IFooRepository Foos { get { return GetRepo<IFooRepository>(); } } 
    private IRepository<T> GetStandardRepo() 
    { 
    return RepositoryProvider.GetRepoistoryForEntityType<T>(); 
    } 
    private T GetRepo<T>() 
    { 
    return RepositoryProvider.GetRepository<T>(); 
    } 
} 

我的问题是即将在那里我下面只有永远的教程instansiates在RepositoryProviderDictionairy<Type, object>和似乎并没有坐满,所以在使用的方法是行不通的。

public virtual T GetRepository<T>(Func<DbContext, object> factory = null) where T : class 
{ 
    //Look for T in the dictionairy by typeof(T) 
    object repoObj; 
    Repositories.TryGetValue(typeof(T), out repoObj); 
    if (repoObj != null) 
    return (T)repoObj; 
    //Not found or a null value, make a new instance of the repository. 
    return MakeRepository<T>(factory, Context); 
} 
private T MakeRepository<T>(Func<DbContext, object> factory, DbContext dbContext) where T : class 
{ 
    var f = factory ?? _repositoryFactories.GetRepositoryFactory<T>(); 
    if (f == null) 
    //Exception here because this is null 
    throw new NotImplementedException("No factory for repository type"); 
    var repo = (T)f(dbContext); 
    Repositories[typeof(T)] = repo; 
    return repo; 
} 

我的问题基本上是什么正确的方式来实现这种模式,我哪里出错了?我应该使用已知存储库列表来创建Dictionairy<Type, Func<DbContext, object>吗?这看起来很脏。我疯狂地试图解决这个问题!

在此先感谢。

+0

什么一个回合的链接教程 – Tar 2013-03-26 16:08:27

+0

@Tar这只是一个辅助类在'Pluralsight'上,所以大多数人都无法访问它。 – LukeHennerley 2013-03-26 16:22:14

+0

我会说,如果你不能根据教程构建一个示例解决方案,那么这是一个糟糕的教程。 这个问题本身有点虚拟,不容易回答... – Tar 2013-03-26 20:17:12

回答

0

我从一开始看到的是,你的Repository<T>没有实现IRepository<T>,所以它应该是这样的:

class Repository<T> : IRepository<T> where T : class 
{ 
    //Implemented methods 
} 

那么你完全保密的教程,应该说明怎样_repositoryFactories.GetRepositoryFactory<T>()可以发现你的IRepository<T>实施者FooRepository - 也许它会自动发现,也许你需要在某处注册某些东西。

接下来,我又一无所知您的具体教程和工厂等,但我想你可能需要使用GetRepo<Foo>代替GetRepo<IFooRepository>,因为现在这个IFooRepository看起来毫无意义的...也许再错过这个IFooRepository东西声明,它应该像interface IFooRepository : IRepository<Foo> - 再一次,它很大程度上取决于您正在使用的工厂的特定发现实现。

+0

积分:1。我的问题不在于存储库,他们很好。 2.本教程不是“*秘密*”,它是一个支付服务的Pluralsight。如果您有权访问它,请随时前往观看。 3.为什么使用界面毫无意义?具体类型的使用意义较小。接口允许依赖注入。 4.问题本身涉及一组特定的模式,而不是我的代码。该代码只是一个主要缩小的例子,用一小撮盐:) – LukeHennerley 2013-03-26 17:00:32

+0

1.。GetRepositoryFactory 无法被发现 - 所以也许不如你想象的那样好 2. http://www.esreality.com /files/placeimages/2009/71946-SarcasmSign.jpg 3.我不谈论OOP的概念 - 我只是说从问题的角度来看,这个特定的接口是没有意义的。 4.模式没问题。代码不是。所以我们必须谈论代码:) – Lanorkin 2013-03-26 19:49:12

0

如果你还没有找到答案,我按照教程,并能够运行它(教程示例)。如果你确定你已经实现了它,请注意这一点,

存储库字典默认为空,并且只有在第一次请求时才会有非标准回购(例如IFooRepository)的值。因此,如果您正在检查Repository Dictionary的调试中的值,并且尚未请求IFooRepository,则肯定不会在那里看到它。有一个代码首先访问IFooRepository,然后它将在提供者类的MakeRepository方法中为该库创建一个存储库。

希望帮助

0

有一个叫RepositoryFactories.cs 您需要为您的自定义库的字典添加一个条目

{typeof(IFooRepository), dbContext => new FooRepository (dbContext)}