0

I'd like to get a IQueryable<User> being POCO. I have two functions in my repository which converts O/RM to POCO and vice versa. This works fine when saving or getting one user, but what about getting an IQueryable of User?

public IQueryable<Domains.User> GetUsers()
{
    // This wont work because it can't convert to a store expression
    return context.JUsers.Select(u => ConvertUserORMToDomain(u));
}

Do I have to manually rewrite out my conversion on each POCO domain in every IQueryable Method I have? I'd be better off without patterns if thats the case.

Converter: private Domains.User ConvertUserORMToDomain(ORM.JUser ormUser)

Steven
  • 166,672
  • 24
  • 332
  • 435
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406

1 Answers1

1

Well a couple of points.

Your using Entity Framework, so why do you need to project the ORM objects into DTO's? If your using pure POCO's, then you don't need any projection at all.

If you want to keep doing what your doing, you need an intermediary layer to mediate betwen your ORM objects and your POCO's.

For example, your repository:

public IQueryable<JUser> GetUsers() // Note how it returns JUser, not the POCO
{
   return context.JUsers;
}

then your service:

public Domains.User GetUserById(int userId)
{
   return repository
             .GetUsers() // IQueryable<JUser>
             .Where(x => x.UserId == userId) // filter based on business req
             .ToList() // materialize query on server
             .Select(u => ConvertUserOrmToDomain(u)); // project into POCO
}

In other words, you need a manager which knows how to materialize the queries based on the business requirement (in this case, getting a user on id), and how to translate the objects into your domain entities.

As i said though, this is a waste of time IMO - use POCO's to directly represent your domain models, then you don't need that conversion process.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • 1
    thanks, could you give an example of what you are talking about with POCO to directly represent my domain models? – Shawn Mclean Jan 09 '11 at 22:49
  • @DVark - i assume you are using Entity Framework 4? If so, EF4 allows "pure POCO's", which means mapping the entities on your EDMX to POCO classes. There's a good Stack question here on EF4 POCO's - where to start? : http://stackoverflow.com/questions/2478081/entity-framework-4-poco-where-to-start. They have no reliance on EF, and you can add business rules/logic to these objects - they become your domain model. – RPM1984 Jan 09 '11 at 22:58
  • In other words - you can map your "User" entity on your EDMX directly to your "Domains.User" object. – RPM1984 Jan 09 '11 at 23:32
  • 1
    LOL! thanks man, I've been doing POCO wrong all along, well, I have no idea what I've been doing because I have my domain and I also let EF generate classes, then I convert those generated classes to the domain I write as in my question. – Shawn Mclean Jan 10 '11 at 00:39
  • Hehe, yeah - that's an unecessary step. DB table maps straight to POCO/domain object. – RPM1984 Jan 10 '11 at 02:27