Advice on Linq to SQL mapping object design

Posted by fearofawhackplanet on Stack Overflow See other posts from Stack Overflow or by fearofawhackplanet
Published on 2010-03-19T12:19:47Z Indexed on 2010/03/19 12:51 UTC
Read the original article Hit count: 217

Filed under:
|
|
|
|

I hope the title and following text are clear, I'm not very familiar with the correct terms so please correct me if I get anything wrong. I'm using Linq ORM for the first time and am wondering how to address the following.

Say I have two DB tables:

User
----
Id
Name


Phone
-----
Id
UserId
Model

The Linq code generator produces a bunch of entity classes.

I then write my own classes and interfaces which wrap these Linq classes:

class DatabaseUser : IUser
{
    public DatabaseUser(User user)
    {
        _user = user;
    }

    public Guid Id 
    {
        get { return _user.Id; } 
    }

    ... etc
}

so far so good.

Now it's easy enough to find a users phones from Phones.Where(p => p.User = user) but surely comsumers of the API shouldn't need to be writing their own Linq queries to get at data, so I should wrap this query in a function or property somewhere.

So the question is, in this example, would you add a Phones property to IUser or not?

In other words, should my interface specifically be modelling my database objects (in which case Phones doesn't belong in IUser), or are they actually simply providing a set of functions and properties which are conceptually associated with a User (in which case it does)?

There seems drawbacks to both views, but I'm wondering if there is a standard approach to the problem. Or just any general words of wisdom you could share.

My first thought was to use extension methods but in fact that doesn't work in this case.

© Stack Overflow or respective owner

Related posts about orm

Related posts about LINQ