English 中文(简体)
如何在使用储存方式时归还可数物体?
原标题:How to return an enumerable object when using repository pattern?

我正致力于一个简单的世界合作框架申请,我对妥善建立托存模式感到不安。 申请的通信模式大致如下:

我对“<代码>”这一部分感到关切。 Admin Console Mode。 在这种模式下,行政部门可以接触一些行政特征,如增加用户和浏览现有用户等。

Abstract contract of one of the entities, Users, needed for this mode, is as follows:

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

具体落实这一存放处:

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")),
                    };
                }
            }
        }
    }
}

从主层看,我如何从<条码>中查阅从方法中回归的用户物体清单? 我试图这样做:

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

    Console.WriteLine("Name	Created Time");

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

根据我的理解,这并不明显奏效,因为用户的标语并不是一个数字物体。 如何修改<代码>Users entities和 交存合同 交存执行,以便 用户物体在屏幕上显示?

Users.cs
UserRepository.cs
IUserRepository.cs

最佳回答

实体:

// 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
{
}

交存:

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

我建议在各地使用“奥约”接口,因为如果你想要从“阿片剂”转用。 NET询问一个办公室,你将需要改写远少得多的法典。


使用:

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

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...