I have the following interface and class
public interface IOwner
{
int Owner_pkid { get; set; }
string Name { get; set; }
}
public class Owner : IOwner
{
public Owner()
{
}
public int Owner_pkid { get; set; }
public string Name { get; set; }
}
I then have the following Data Access Methods in a separate class
public List<IOwner> GetAllOwners()
{
var sql = "SELECT owner_pkid, name from dbo.Owners ";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.CommandType = CommandType.Text;
List<IOwner> owners = new List<IOwner>();
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
BindResultSet<IOwner>(reader, owners);
return owners;
}
}
private void BindResultSet<T>(SqlDataReader reader, List<T> items) where T : new()
{
int counter = 0;
if (!reader.IsClosed)
{
while (reader.Read())
{
T record = GetNextDataObject<T>(items, counter);
counter++;
BindRecord<T>(reader, record);
}
}
}
private T GetNextDataObject<T>(List<T> items, int pointer) where T : new()
{
if (pointer < items.Count)
{
return items[pointer];
}
else
{
items.Add(new T());
return items[items.Count - 1];
}
}
private void BindRecord<IOwner>(SqlDataReader reader, IOwner owner)
{
owner.Name = (string)reader["name"];
owner.Owner_pkid = (int)reader["owner_pkid"];
}
I am getting 2 separate errors with this code:
- In the GetAllOwners method I am getting an error with the call to BindResultSet which says
IOwner must be a non-abstract type with a parameterless constructor in order to be used here
I have a parameterless constrctor on the implementing class - don't think i can add one to the interface
- In the final BindRecord method I am getting an error whereby the two property names are not recognized. This is possibly as a result of the first issue