ADO.Net 模拟上下文生成器接口问题

发布于 2024-10-18 08:47:39 字数 2865 浏览 1 评论 0原文

我正在使用 EF 4 和存储库模式。在尝试对 EF 数据上下文进行单元测试的过程中,我遇到了 ADO.Net 模拟上下文生成器模板,正如名称所描述的,它有助于模拟数据上下文。

虽然我的上下文确实构建得很好,但我的存储库遇到了问题,这有点神秘。这是我的存储库界面(缩写):

 public interface IRepository<T> where T : class
{
    /// <summary>
    /// Finds all entities
    /// </summary>
    /// <returns></returns>
    IQueryable<T> Find();

    /// <summary>
    /// Find alls entities and loads included relational properties.
    /// </summary>
    /// <param name="includes">Relational properties to load.</param>
    /// <returns></returns>
    IQueryable<T> Find(params Expression<Func<T, object>>[] includes);

    /// <summary>
    /// Adds an entity to the repository.
    /// </summary>
    /// <param name="entity">Entity to add.</param>
    void Add(T entity);

    /// <summary>
    /// Updates an entity in the repository.
    /// </summary>
    /// <param name="entity">Entity to update.</param>
    void Update(T entity)...etc

通过模拟上下文,您将获得一组新的具有虚拟属性的生成实体。我为这些实体提供了与真实实体相同的命名空间,以便模拟它们,并且它们与我的真实实体具有相同的类型,仅引用模拟实体。

问题是,当我实现存储库接口时,我会得到接口未实现的异常,即使我实际上正在实现该接口。仅当我尝试构建项目时才会发生这种情况。 Intellisense 认为一切都很好(即界面下没有小箭头告诉我需要实现它)。

下面是通过我的 ICategoryLocalized 接口(它继承自 IRepository)实现的接口。

public class CategoryLocalizedRepository : MockDatabaseRepository, ICategoryLocalizedRepository
{
    #region Constructor

    public CategoryLocalizedRepository(MockContext context)
        : base(context)
    {
    }

    #endregion

    #region Data Methods

    public IQueryable<CategoryLocalized> Find()
    {
        return Context.CategoryLocalized;
    }

    public IQueryable<CategoryLocalized> Find(params Expression<Func<CategoryLocalized, object>>[] includes)
    {
        return CreateObjectQuery<CategoryLocalized>(Context.CategoryLocalized, includes);
    }

    public CategoryLocalized Get(int id)
    {
        return Context.CategoryLocalized.SingleOrDefault(d => d.CategoryLocalizedID == id);
    }

    public CategoryLocalized Get(int id, params Expression<Func<CategoryLocalized, object>>[] includes)
    {
        IObjectSet<CategoryLocalized> query = CreateObjectQuery<CategoryLocalized>(Context.CategoryLocalized, includes);
        return query.SingleOrDefault(d => d.CategoryLocalizedID == id);
    }

    public void Add(CategoryLocalized categoryLocal)
    {
        AddEntity<CategoryLocalized>(Context.CategoryLocalized, categoryLocal);
    }

    public void Update(CategoryLocalized categoryLocal)
    {
        UpdateEntity<CategoryLocalized>(Context.CategoryLocalized, categoryLocal);
    }...etc

我不知道为什么编译器说接口(IRepository)没有在 CategoryLocalizedRepository 中实现,而它显然是实现的。

I'm working with EF 4 and a repository pattern. In my adventures in trying to unit test my EF data context, I came across the ADO.Net Mocking Context Generator template, which as the name describes, aids in mocking the data context.

While my context does build fine, I'm having problems with my repository and it's a bit of a mystery. Here is my repository interface (abbreviated):

 public interface IRepository<T> where T : class
{
    /// <summary>
    /// Finds all entities
    /// </summary>
    /// <returns></returns>
    IQueryable<T> Find();

    /// <summary>
    /// Find alls entities and loads included relational properties.
    /// </summary>
    /// <param name="includes">Relational properties to load.</param>
    /// <returns></returns>
    IQueryable<T> Find(params Expression<Func<T, object>>[] includes);

    /// <summary>
    /// Adds an entity to the repository.
    /// </summary>
    /// <param name="entity">Entity to add.</param>
    void Add(T entity);

    /// <summary>
    /// Updates an entity in the repository.
    /// </summary>
    /// <param name="entity">Entity to update.</param>
    void Update(T entity)...etc

With the mock context, you get a new set of generated entities with virtual properties. I have given those entities the same namespace as my real entities for the purpose of mocking them and they are identical types as my real ones with only a reference to the mock entities.

The problem is when I have an implementation of my repository interface I get interface not implemented exceptions, even though I am in fact implementing the interface. This only occurs when I try to build the project. Intellisense thinks everything is fine (ie, no litte arrow under the interface telling me I need to implement it).

Below is an implementation of the interface via my ICategoryLocalized interface (it inherits from IRepository).

public class CategoryLocalizedRepository : MockDatabaseRepository, ICategoryLocalizedRepository
{
    #region Constructor

    public CategoryLocalizedRepository(MockContext context)
        : base(context)
    {
    }

    #endregion

    #region Data Methods

    public IQueryable<CategoryLocalized> Find()
    {
        return Context.CategoryLocalized;
    }

    public IQueryable<CategoryLocalized> Find(params Expression<Func<CategoryLocalized, object>>[] includes)
    {
        return CreateObjectQuery<CategoryLocalized>(Context.CategoryLocalized, includes);
    }

    public CategoryLocalized Get(int id)
    {
        return Context.CategoryLocalized.SingleOrDefault(d => d.CategoryLocalizedID == id);
    }

    public CategoryLocalized Get(int id, params Expression<Func<CategoryLocalized, object>>[] includes)
    {
        IObjectSet<CategoryLocalized> query = CreateObjectQuery<CategoryLocalized>(Context.CategoryLocalized, includes);
        return query.SingleOrDefault(d => d.CategoryLocalizedID == id);
    }

    public void Add(CategoryLocalized categoryLocal)
    {
        AddEntity<CategoryLocalized>(Context.CategoryLocalized, categoryLocal);
    }

    public void Update(CategoryLocalized categoryLocal)
    {
        UpdateEntity<CategoryLocalized>(Context.CategoryLocalized, categoryLocal);
    }...etc

I don't know why the compiler is saying that the interface (IRepository) is not implemented in CategoryLocalizedRepository when it clearly is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-10-25 08:47:39

事实证明,我引用了存储库中的另一组实体,并引用了我的模拟数据项目中的另一组实体。

Turns out I had a reference to another set of entities in my repository and a reference to a different set in my mock data project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文