tags:

views:

79

answers:

2

I'm trying to implement a highly reusable web system where the source of data for the model can be changed at will. What I need is a generic type instead of relying on the type generated by either LINQ or entities.

This tutorial goes over separation for high reusability but what happens if the data representation between entities and LINQ changes ever so slightly? Then you'd have to change the view. Are the classes generated by LINQ and entities guaranteed to be the same for when they're passed to the view? Is it possible to create a class/interface that generically represents the data?

EXAMPLE

namespace Intranet.Areas.Accounts.ViewModels
{
  using Intranet.Areas.Accounts;
  using System.Collections.Generic;
  using Intranet.Areas.Accounts.Models;

  public class ContractsControlViewData
  {
    public IEnumerable<Contract> Contracts { get; set; }
    public IEnumerable<tblCC_Contract_CC> tblCC_Contract_CCs { get; set; }
    public IEnumerable<tblCC_Contract_Data_Terminal> tblCC_Contract_Data_Terminals { get; set; }
    public IEnumerable<tblCC_CDT_Data_Service> tblCC_CDT_Data_Services { get; set; }
    public IEnumerable<tblCC_Data_Service> tblCC_Data_Services { get; set; }
  }
}

The above code is taken from a small project I'm testing. Notice that I've got types that begin with "tbl". I've just dragged these on from a database onto the LINQ designer and I'm using them in this container which is passed to the view to later render multiple result sets on screen.

The problem is that if I ditch LINQ and use entities instead, will the data still be reflected in the same way as the types represented here? Will Contract still be valid? Bear in mind that these types in the previous code example are generated by LINQ. How consistent are they if you use a different data access technology?

This is why I wonder if we should, for separation of concerns, and for consistency of data instead create our own classes to represent the data in a consistent manner.

My brain hurts, someone save me from myself.

+1  A: 

Wouldn't the class you create then be very close to a DataTable, DataRow or even a NameValueCollection? Your object would have to include it's schema as part of itself and the View would have to iterate over the Collection(s).

hometoast
I use IMultipleResult from functions defined in my dbml file to return multiple result sets which have class representations as a container for when they're passed to the view. The container relies on the types generates by LINQ though which means there should be zero difference between auto-generated classes for output from queries. If you look at the classes generated by LINQ their 'schema' is just object representations of the data types (i.e. string, int etc.) so I didn't see it as initially difficult.
Kezzer
+1  A: 

This is why I wonder if we should, for separation of concerns, and for consistency of data instead create our own classes to represent the data in a consistent manner.

Yes 100%

I have no experience with linq to sql and entities myself. I used other ORM and the first thing I always checked was if I can still use my own POCO classes in all layers.

At my office some colleagues built a pretty large application using linq to sql. It was hard for me to believe that this is possible, because of how changes to the DB would ripple through all layers.

They even have a funny issue since SP1 in german: all collection classes got some german indicator for plurality. Thats pretty cool if you have developers in several countries. Better dont let developers in china regenerate your dbml.

It turned out that my colleagues did exactly what you describe: They had their own model (Poco) classes that they map to the generated linq classes in the DB layer.

Malcolm Frexner