通用起订量存储库 - GetById(int i) 返回 T

发布于 2024-12-23 14:28:36 字数 443 浏览 1 评论 0原文

我有一个通用存储库,我正在尝试为我的单元测试创​​建最小起订量实现。我需要创建一个 GetById 方法。这是我决定将其转换为使用泛型之前的最小起订量实现。

mockUserRepository.Setup(r => r.GetById(It.IsAny<int>()))
.Returns((int i) => users.Where(u => u.Id == i).Single());

通用存储库将用于 MOQ 设置

Mock<IRepository<T>> mockRepository

通用假货列表将用于 LINQ where 子句

List<T> list

任何人都可以帮我转换上面的方法。

I have a generic repository and im trying to create a moq implementation for my unit tests. I need to create a GetById method. This is the moq implementation before i decided to convert it to use generics.

mockUserRepository.Setup(r => r.GetById(It.IsAny<int>()))
.Returns((int i) => users.Where(u => u.Id == i).Single());

A generic repository will be used for the MOQ setup

Mock<IRepository<T>> mockRepository

A generic list of fakes will be used for the LINQ where clause

List<T> list

Can anyone help me convert the method above.

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

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

发布评论

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

评论(1

|煩躁 2024-12-30 14:28:36

我不完全确定你在问什么。如果您只想创建通用存储库的模拟,则设置是相同的。

如果您想要一个帮助器方法来为任何实体类型创建存储库,这是可行的。您必须在实体上实现一个接口来检索 ID,或者将委托传递给帮助器方法。下面的 LINQPad 示例展示了第一种技术。

void Main()
{
    var users = new List<User>
        { new User { Id = 1 }, new User { Id = 2 } };

    var mockUserRepository = new Mock<IUserRepository>();

    mockUserRepository.Setup(r => r.GetById(It.IsAny<int>()))
        .Returns((int i) => users.Where(u => u.Id == i).Single());

    mockUserRepository.Object.GetById(2).Dump();

    var mockUserRepository2 = new Mock<IRepository<User>>();

    mockUserRepository2.Setup(r => r.GetById(It.IsAny<int>()))
        .Returns((int i) => users.Where(u => u.Id == i).Single());

    mockUserRepository2.Object.GetById(2).Dump();

    IRepository<User> userRepository3 = CreateTestRepository(users);

    userRepository3.GetById(2).Dump();
}

public static IRepository<TEntity> CreateTestRepository<TEntity>(
    List<TEntity> entities)
    where TEntity: IPrimaryKeyed
{
    var mockRepository = new Mock<IRepository<TEntity>>();

    mockRepository.Setup(r => r.GetById(It.IsAny<int>()))
        .Returns((int i) => entities.Where(e => e.Id == i).Single());

    return mockRepository.Object;
}

public interface IPrimaryKeyed
{
    int Id { get; }
}

public class User : IPrimaryKeyed
{
    public int Id { get; set; }
}

public interface IUserRepository
{
    User GetById(int id);
}

public interface IRepository<T>
{
    T GetById(int id);
}

顺便说一句,假设您的 ID 始终是整数是有风险的。您可以考虑在整个代码库中使 int 类型通用(当用于引用 ID/主键时)。通常我会调用 YAGNI,但搜索并替换 intGuid 配合效果不佳。 ;)

I'm not entirely sure what you're asking. If you just want to create a mock of the generic repository, the setup is the same.

If you want a helper method to create a repository for any entity type, that's doable. You have to either implement an interface on the entities to retrieve the ID or pass in a delegate to the helper method. Here's a LINQPad example that shows the first technique.

void Main()
{
    var users = new List<User>
        { new User { Id = 1 }, new User { Id = 2 } };

    var mockUserRepository = new Mock<IUserRepository>();

    mockUserRepository.Setup(r => r.GetById(It.IsAny<int>()))
        .Returns((int i) => users.Where(u => u.Id == i).Single());

    mockUserRepository.Object.GetById(2).Dump();

    var mockUserRepository2 = new Mock<IRepository<User>>();

    mockUserRepository2.Setup(r => r.GetById(It.IsAny<int>()))
        .Returns((int i) => users.Where(u => u.Id == i).Single());

    mockUserRepository2.Object.GetById(2).Dump();

    IRepository<User> userRepository3 = CreateTestRepository(users);

    userRepository3.GetById(2).Dump();
}

public static IRepository<TEntity> CreateTestRepository<TEntity>(
    List<TEntity> entities)
    where TEntity: IPrimaryKeyed
{
    var mockRepository = new Mock<IRepository<TEntity>>();

    mockRepository.Setup(r => r.GetById(It.IsAny<int>()))
        .Returns((int i) => entities.Where(e => e.Id == i).Single());

    return mockRepository.Object;
}

public interface IPrimaryKeyed
{
    int Id { get; }
}

public class User : IPrimaryKeyed
{
    public int Id { get; set; }
}

public interface IUserRepository
{
    User GetById(int id);
}

public interface IRepository<T>
{
    T GetById(int id);
}

As an aside, assuming your IDs will always be integers is risky. You might consider making the int type generic (when used to refer to IDs/primary keys) throughout your codebase. Normally I'd invoke YAGNI, but searching and replacing int with Guid does not work well. ;)

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