I have the following setup for a simple application:
-
generated LINQ to SQL classes from my database tables (DAL?)
-
created classes (BLL?) that use the LINQ to SQL datacontext, to insert, update, delete, select
-
in my presentation layer, when inserting e.g. a new "product", I’m creating a new instance of object "Product", which comes from my DAL layer (this is what LINQ to SQL created for me)
My question is if this is the right way to go for separation of layers in this case. What’s strange to me is that I’m using an object type that was defined in the DAL, which shouldn’t be used in the presentation layer if you ask me. But if I want to make use of the LINQ to SQL objects without again creating new objects, this seemed like the way to go.
What’s your advice on this? I’m not sure what’s the best way to go about here.
I don’t think there is ever a one-size-fits-all solution, but, if you have a simple application, you probably don’t want to complicate things unnecessarily
If you are able to stomach keeping the data context open at your presentation tier, there are some ‘productivity’ gains in keeping the L2SQL entities:
However, for a heavier, more purist architecture, you could consider
EDIT:
You’ve got a couple of options if you don’t want to work with the VS generated L2SQL entities in your BLL or Presentation Layer.
Create a new set of POCOs and then map the L2SQL entities out in your repository / dal. Automapper can simplify this task.
You can use your own POCOs directly in Linq2SQL, by providing an XmlMappingSource, although this is a bit more work.