tags:

views:

54

answers:

2

I'm struggling to decide on a good way to structure my object model / web services in a webapp I'm working on. We have a common issue where different pages need a slightly different view of a standard object. For example, we have Team and an Organization types where an Organization can have one-to-many Teams. On one page all I need is something like

class Team {
    long TeamID { get; set; }
    string Name { get; set ;}
    TeamType TeamType { get; set; }
}

But in another page I need a different projection of the data such as

class Team {
    long TeamID { get; set; }
    string Name { get; set ;}
    string OrganizationName { get; set; }
    string TeamPicture { get; set; }
}

Often times the projection of an object spans multiple tables. My thought is that it is bad to query out and pass around ALL of the data for a Team and Organization and then just use the parts I need on the page. It seems like I'll end up with huge amounts of unnecessary data being queried and flowing around the system.

My current model is to create separate types for each projection. I like the type safety but the problem I'm running into with this is I've got types like Team, SimpleTeam, TeamAndOrganization, etc. I'm using team/organization as the example, but I find the same problem with many different types in my system.

How do you handle this? I use WCF to pass data between the webapp and a back-end system.

+2  A: 

While generally I think model-per-view is good way to go, I'd be a little concerned about this in the context of a service. Ideally, you'd like your service to be more general I would think so that you're not getting an explosion of methods, one per view. Generally, I'm willing to provide a little more data than necessary if it means that I can make my service more general. In my case, I've got different types of clients (mobile vs. standard web app) and they generally get the same data, but the mobile app uses a reduced subset. In this case I don't provide separate models, but the mobile app just doesn't use some of the data.

tvanfosson
It definitely does lead to an explosion of service methods, which is awful. Am I overly concerned about perf/scalability in my worry about extra data floating around. The product is selling well, we've steadily grown our user base. If we continue our course we'll see 1000+ concurrent users next year.
AgileJon
You need to strike a balance between performance and maintainability. Generally I would go with maintainability until performance becomes a problem, then refactor as needed to improve performance.
tvanfosson
Wow, it seems no matter how many times I read/hear that general idea (premature optimization is the root of all evil) it doesn't resonate in my designs. Thank you!
AgileJon
A: 

I would not tie your object model to a page view. Once you do that, it's difficult or impossible to change your pages. It's a subtle coupling between layers that should be avoided.

Perhaps it's possible to filter the data that ends up at the client. Lazy evaluation from the database can help as well.

duffymo
Since I've separated the webapp from the database with web services, lazy evaluation from the DB doesn't seem like an option. I was hoping to get more reuse of my 'projections' but haven't seen much (ie: you are dead on about coupling). However, it seems like this makes changing a page easier rather than harder. I don't have to worry about side-effects to other pages because they all use their own projection of the data.
AgileJon
What would happen if your web interface went away entirely, replaced by a Flash or Flex UI, or you decided to add a parallel, simpler mobile interface? How would your design deal with those changes?
duffymo
It would be a huge pain! I misunderstood what you meant by difficult/impossible to "change" pages. I was thinking just tweaking my existing pages.
AgileJon
Exactly. And how likely is it that your app could stick around longer than your interface? Something to think about.
duffymo