填充界面

Populating Interface

谁能告诉我如何在界面中填充字段 IAccount?我在 x.Add(new IAccount ...

中遇到错误
public class IPersonRepo : IAccount
{
    string connectionstring = @"Server=SLI002/SQLEXPRESS;Database=atengturonDB;Trusted_Connection=true;";
    public int AccountsID
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountUserName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountPassword
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountSalt
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public void getAccount()
    {
        SqlConnection conn = new SqlConnection(connectionstring);
        using (SqlCommand comm = new SqlCommand())
        {
            List<IAccount> x = new List<IAccount>();
            comm.Connection = conn;
            comm.CommandText = "Select AccountsID,AccountsUserName,AccountsPassword,AccountsSalt from Accounts";
            comm.CommandType = CommandType.Text;
            SqlDataReader reader = null;
            conn.Open();
            comm.ExecuteNonQuery();
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                x.Add(new IAccount
              {
                  AccountsID = (int)reader["AccountsID"],
                  AccountUserName = (byte[])reader["AccountsUserName"],
                  AccountPassword = (byte[])reader["AccountsPassword"],
                  AccountSalt = (byte[])reader["AccountsSalt"]
              });

            }
            conn.Close();
        }
    }
}

请将IPersonRepo重命名为PersonRepo,前缀I表示接口,但显然是class。其次,它看起来不像 repo (=repository),而是像 Person(但这值得商榷...:))

第三,您正在尝试创建接口 - 但您必须实例 class 来实现该接口:

//x.Add(new IAccount
//x.Add(new IPersonRepo
//x.Add(new PersonRepo
x.Add(new Person
          {
              AccountsID = (int)reader["AccountsID"],
              AccountUserName = (byte[])reader["AccountsUserName"],
              AccountPassword = (byte[])reader["AccountsPassword"],
              AccountSalt = (byte[])reader["AccountsSalt"]
          });

第四点也是最后一点,也许你应该看看任何 ORM,比如 NHibernate 或 Entity Framework。可以为您提供帮助,但这取决于您:)

首先,在决定class 名称时切勿使用I 前缀。这不是编译错误,但它非常令人困惑,因为约定是使用 I 作为接口名称的前缀。
所以你的 class 应该叫做 PersonRepo 而不是 IPersonRepo.
(但是,您可以将以 I 开头的 class 命名为 (Ice),只是不要使用 I 作为前缀)

其次,不能实例化接口。您可以使用接口类型的变量,但实例化实际的 class: IAccount MyAccount = new PersonRepo();