views:

34

answers:

2

Hi,

I have a basic view that returns the same columns as a table (give or take 1 field)

in my DAL code, i am returning a list of MyTableObject, however in some cases, i will call the view to return the same data, but from different sources.

List<MyTableObject> tableObjects = new List<MyTableObject>();
if (case1)
  tableObjects = entities.MyTableObjects.Where(criteria).ToList();
else
  tableObjects = entities.MyViewObjects.Where(criteria).ToList(); // <-- This will obviously break
return tableObjects;

is there a way to Map view entities to be returned as table entities? (other than having table and view implement the same interface and return that interface) i would like to keep the return type as MyTableObject.

I came across Auto Mapper, but not sure if it would be suitable for this scenario..

A: 

You can write a stored procedure (or CommandText in the model, without creating DB Object) that will simply call "Select * from View". Then create Function Import for this procedure and set the return type to MyTableObject.

Devart
A: 

Looks like i found a cool solution to this..

Initially I tried to implement interface approach and run into some casting issues (using interfaces alongside my predicate builder), and also with interfaces having to create partial classes for each entity that implement the interface..

the answer.. POCOs.

Iused Poco Template for EF, and than simply edited xxxPocoGenerator.Context.tt to return MyTable object from MyViews collection (one line).

public ObjectSet<Trade> v_Trade {
        get { return _v_Trade  ?? (_v_Trade = CreateObjectSet<Trade>("Trades")); }
}

nice and easy..

Sonic Soul