views:

25

answers:

3

A simple linq to SQL query Might return a product object. Obiously I could pass this object to my business layer, then read and update the data, directly against that object.

I've seen a bunch of implementations where following the execution of the linq query the resulting object is mapped (via automapper or manually) to a custom business object. So in the case of product where I might have the linq object:

product.ProductId and Product.ProductName

and then I would define a custom Product business object like so:

class BusineszProduct
{
    string ProductId;
    string ProductName;
}

and some simple mapping code like:

BusinessProduct myProduct = new BusinessProduct(); myProduct.ProductId = product.ProductId; myProduct.ProductName = product.ProductName;

and then pass myProduct around my business layer, modify it, read it and so on, then later update the linq object.

In what scenarios would I want to create the custom BusinessProduct class?

A: 

This website might get you started...

https://west-wind.net/Weblog/posts/160237.aspx

Yves M.
A: 

In my application, I do a Linq query which joins six different tables, and provides a couple columns from each one. There is no table-specific object type which matches that set of information. Hence I create a custom business class to handle the record set.

You can view it yourself (at http://www.njtheater.org)

I select from the Productions table by Date. It joins to the Play table (where I get the Title & Description), the Troupes table (where I get the theater company name), the Venues table (where I get the theater name & City). The Plays table joins to the PlayCredits table which joins the People table (where I get the playwrights' names)

James Curran
@James - You said, "There is no table-specific object type which matches that set of information. Hence I create a custom business class to handle the record set." Do you mean that you're creating a custom object like my BusinessProduct class above, or that your Linq result is in effect a custom business class?
Emilio
I created a specific class for the purpose, largely because I need to do some postprocessing on it before displaying theh info, and the properties in an anonymous class are read-only.
James Curran
+2  A: 

IMHO, the general reason is to decouple / disentangle your Business Entities from the Linq2SQL ORM baggage that comes with Linq2SQL Entities

However, in an extreme scenario, you may have multiple mappings:

  • Linq2SQL entities for the "Data Access" Layer - however these are tightly coupled to the L2S DataContext
  • POCOs / Business Entities would be used for business rule application, validation etc
  • If you are using Web Services or WCF, you might also represent the data as Message Entities e.g. if you need to present the entities in a very specific format when they are serialized across the wire
  • And finally, if you have a MVC / MVP / MVVM UI architecture, you might want entities tailored for your views
nonnb
@nonnb - Part 1-Great answer.Bullet 1-makes total sense b/c even tho Linq to Sql gives me db independence if I use the repository pattern,it still couples me to a relational db store. Whereas if I created another layer w/ this object mapping I could more easily change my data store to a nosql db for example. #2-It seems like Linq and MVC have a ton of native validation support so I'm not sure this one registers w/ me.
Emilio
@nonnb - Part 2-#3-Why might I want to represent the data in a specific format across the wire vs. using the Linq object?#4-Makes total sense. I've read that you should have a 1:1 mapping between your view and the model that backs that view,so it makes sense to do the mapping before passing the model data from the controller to the view.The icky alternative would be to create a Linq query for each view. My comments make sense?
Emilio
Sure - this is an extreme examplePoint 2 - Moot point, but in an N tier SOA stack, it just seems wrong to have assembly references to your DAL when using DI etc. Point 3 - If for instance you have committed to publish a specific XSD / WSDL contract to a consumer, then you do need fine level control over the xmnls, element vs attribute, names etc. But this wouldn't generally be required if the consumers are under your own control (e.g. Your own client / Ajax or similar)
nonnb