[英]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 contract
和repository 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.