开发者

LINQ to SQL DAL + BLL + Presentation [closed]

开发者 https://www.devze.com 2023-04-12 10:52 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing th
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 1 year ago.

Improve this question

I have the following setup for a simple application:

  1. generated LINQ to SQL classes from my database tables (开发者_如何学CDAL?)

  2. created classes (BLL?) that use the LINQ to SQL datacontext, to insert, update, delete, select

  3. 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:

  • Support for lazy loading (although watch out for the 1:N problem)
  • You have a single set of generated entities (albeit datacontext aware)

However, for a heavier, more purist architecture, you could consider

  • Wrapping the Linq2SQL artifacts in a Repository Pattern
  • Map out the L2SQL entities into POCOs before returning them to your BLL / Service Tier
  • Depending on your network and presentation architectures, you may also need to consider what you entity looks like over the network (Serialization, WCF, JSON etc) and you might also look at View Models, in which case your entity will 'change' class and shape several times as it is mapped and transformed between layers.
  • In fact, you might consider another ORM than Linq2SQL entirely, e.g. NH or EF

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号