简体   繁体   English

使用存储库模式时如何返回可枚举的对象?

[英]How to return an enumerable object when using repository pattern?

I am working on a simple WCF application and I am having trouble with properly setting up the Repository pattern. 我正在开发一个简单的WCF应用程序,但无法正确设置存储库模式。 The communication pattern of the application is roughly as shown here . 应用程序的通信模式被大致如图这里

I am concerned with the part that says Admin Console Mode . 我担心“ Admin Console Mode这一部分。 In this mode, the admin has access to some admin features like adding users and viewing existing users etc. 在此模式下,管理员可以访问某些管理员功能,例如添加用户和查看现有用户等。

Abstract contract of one of the entities, Users , needed for this mode, is as follows: 该模式所需的实体( Users之一的抽象合同如下:

public interface IUserRepository
{
    byte AddUser(string _loginname, string _loginpass);
    Users ShowAllUsers();
}

The concrete implementation of this repository: 该存储库的具体实现:

public class UserRepository : IUserRepository
{
    public UserRepository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public byte AddUser(string _loginname, string _loginpass)
    {
        . . .
    }

    public Users ShowAllUsers()
    {
        string query = "select login_name,created_time from users";

        using(SqlConnection conn = new SqlConnection(_connectionString))
        {
            using(SqlCommand cmd = new SqlCommand(query, conn))
            {                    
                conn.Open();

                using(var reader = cmd.ExecuteReader())
                {
                    if(!reader.Read())
                        return null;

                    return new Users
                    {
                        Login_Name = reader.GetString(reader.GetOrdinal("login_name")),
                        Login_Pass = reader.GetString(reader.GetOrdinal("login_pass")),
                        Created_Time = reader.GetDateTime(reader.GetOrdinal("created_time")),
                    };
                }
            }
        }
    }
}

From the Host layer, How do I access the list of Users objects returned from the ShowAllUsers method? 从主机层,如何访问ShowAllUsers方法返回的Users对象列表? I tried this way: 我这样尝试:

public void ShowUsers()
{
    Users user = _repo.ShowAllUsers();

    Console.WriteLine("Name\tCreated Time");

    foreach(Users u in user)
    {
        Console.WriteLine("{0}\t{1}",u.Login_Name,u.Created_Time);
    }
}

From what I understand, this does not work obviously since the Users object is not an enumerable object. 据我了解,由于Users对象不是可枚举的对象,因此这显然不起作用。 How do I modify the Users entity and the repository contract and the repository implementation so that the Users object is returned to be displayed on the screen? 如何修改Users实体, repository contractrepository implementation以便返回Users对象以在屏幕上显示?

Users.cs Users.cs
UserRepository.cs UserRepository.cs
IUserRepository.cs IUserRepository.cs

Entity: 实体:

// note the singular form, it makes much more sense for me, as far as the object represents a single entity (user)
// also not an interface, optionally
public class User : IUser
{
}

Repository: 库:

public class UserRepository : IUserRepository
{
    public IEnumerable<IUser> ShowAllUsers()
    {
        ...
        while (reader.Read())
        {
             yield return new User
             {
                 ...
             };
        }
    }
}

I recommend yo use interfaces everywhere, because if you will want to switch from ADO.NET query to an ORM, you will need to rewrite much less code. 我建议您在任何地方都使用接口,因为如果您要从ADO.NET查询切换到ORM,则需要重写的代码要少得多。


Usage: 用法:

foreach (User u in users)
{
    Console.WriteLine("{0} created on {1}", u.Login, u.CreationTime);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用存储库模式时如何获取单个对象? - How to get single object when using repository pattern? 如何使用 enumerable 和 model 返回 enumerable? - How to return enumerable with enumerable and with model? 如何使用NHibernate和存储库模式从域对象的属性返回分页列表 - How to return a paged list from a property of a Domain Object using NHibernate and a Repository Pattern 存储库模式 - 验证对象和返回消息 - Repository Pattern - Validating Object and Return Message EF 4:如何使用具有存储库模式的MVC正确更新DbContext中的对象 - EF 4: How to properly update object in DbContext using MVC with repository pattern 存储库模式和返回类型 - Repository pattern and return types 在使用存储库模式时,我应该将我的存储库方法放多少逻辑? - How much logic should i put my repository methods when using repository pattern? 如何在存储库模式中处理对象上下文 - How to dispose object context in repository pattern 使用实体框架时如何自动将所有 Enumerable 转换为 List? - How to automatically convert all Enumerable to List when using Entity Framework? 处理具有收益回报的Enumerable对象的正确模式是什么? - What is the proper pattern for handling Enumerable objects with a yield return?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM