0

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.

7
  • it's better to ask your question in more than one posts. Firstly, Why you don't want to use EF? for paging data it's depend of what version of SQL server do you use for example you can use OFFSET in SQL 2014, and Row_number in another version. Commented Aug 19, 2016 at 7:13
  • In my Book, PersonForList is not necessary. I think you can do it better with EF instaed your Query. Commented Aug 19, 2016 at 7:14
  • In my work, we are using our own ORM, but actually doesn't have support for Joins, so, when Joins are required, we need to make raw queries, and then mapping results manually. I'm using SQL Server 2014. Commented Aug 19, 2016 at 7:16
  • OK, keep going. try to use Offset in your query to make paging. and would you please explane what do you want more Commented Aug 19, 2016 at 7:18
  • If the first table above is my result set from database, how can I get, preferably with LINQ, a list of Person with his phones? How to make a Group By or somethig else to populate my view model? Commented Aug 19, 2016 at 7:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.