Reputation: 492
I'm trying to implement an MVC application without Entity Framework, instead I'm using datatables to populate my Business Objects but I have a lot of questions.
Suppose that I have this simple clasess:
public class Person {
public int Id { get; set; }
public string Name { get; set; }
}
public class Phone {
public int Id { get; set; }
public string PhoneNumber { get; set; }
public int PersonId { get; set; }
}
If I were using EF, Person class must have a navigation property to get Phones and Phone class must have a navigation property to Person. So If I had to display a Person list with phones, the sql result should look like this:
------------------------------------------------------------
| **PersonId** |**Name** | **PhoneId** | **PhoneNumber** |
------------------------------------------------------------
| 1 | John Doe | 1 | 111-55-5855 |
------------------------------------------------------------
| 1 | John Doe | 2 | 111-55-5521 |
------------------------------------------------------------
| 2 | Mary Jane| 3 | 254-565-855 |
------------------------------------------------------------
| 3 | J. Watson| NULL| NULL|
------------------------------------------------------------
So far, what I have done is this:
In my view, I have to show contacts with this format:
------------------------------------------------------------
|**Name** | ** Phones ** |
------------------------------------------------------------
| John Doe | 111-55-5855, 111-55-5521 |
------------------------------------------------------------
| Mary Jane| 254-565-855 |
------------------------------------------------------------
| J. Watson| - |
------------------------------------------------------------
I've created a third class, which I don't really know if its necesary. In this class, I'm storing the results that are being returned by my sql query. This results are exactly like the first table above, which are equals to what EF would return.
public class PersonForList {
public int PersonId { get; set; }
public string PersonName { get; set; }
public int PhoneId { get; set; }
public string PhoneNumber { get; set; }
}
I have a PersonManager with this method:
public List<PersonForList> GetPeopleWithPhones {
List<PersonForList> people = new List<PersonForList>();
SqlCommand command = new Command(query);
var dt = ExecuteDataTable(command);
foreach(DataRow row in dt.Rows){
var person = new PersonForList() {
... Fill object
PersonId = (int)row["PersonId"],
...
}
people.Add(person);
}
return people;
}
And last, my viewmodel:
public class PersonListViewModel {
public int PersonId { get; set; }
public string PersonName { get; set; }
public string Phones { get; set; }
}
(If you've really come this far, you must be a really patient person =S)
Questions:
If I have to paginate my list, how can I count different records but still have all the phones in my query?... For example, if page size were 3, with my current query I will retrieve only John Doe and Mary Jane Phones, because this are 3 rows, but not for different person.
How can I populate my view model? I don't know how to iterate over the List to get one person with all his phones.
Is my class PersonForList really necessary?
Somehow, this is the behaviour of Entity Framework and I'm just trying to imitate to get data with the minimum number of queries.
I tried to be very specific and keep question short, but if you need more details I can provide you.
Upvotes: 0
Views: 342
Reputation: 1258
For mapping data from one object to an other, I would use AutoMapper, other tools are available.
But I agree with everyone, use a ORM it will make your life 32% happier.
Upvotes: 1