LINQ to SQL. What's better?

This question is a kind of pool. We are trying to determine the best architecture when using ORM, for example LINQ to SQL. The argument that we define refers to the structure in which other applications will access any trough directly, referring to a DLL or through a web service. We have .NET applications and PHP applications.

Capabilities:

Several data contexts: dividing the database into units of work and creating separate contexts for each of them.

Pros:

  • Ease of use
  • Classes will be split into different namespaces.
  • Smaller support domain

Minuses:

  • Objects should be duplicated if related to the creation of hell.
  • Objects cannot be transferred between contexts, creating the need for another hit in the database

The context of single data: all tables, views, procedures are in the same huge context.

Pros:

  • No duplication.
  • Relationships are easy to manage, basically LINQ will take care of that.
  • Better performance, fewer database hits.

Minuses:

  • All tables are in the same namespace, code completion becomes a nut
  • Not the best for a designer (at least on VS2008)
  • It cannot be selective in what to preserve and what not. Save all or delete the entire mode.

Well, that’s what occurred to me, so if I have other pros or cons, please tell me and I will include them in the message. Also choose your best.

Thank you all

+4
source share
4 answers

I understand your doubts. I had the same thing when I started using LinqToSql. To help me find the best way, I started creating a personal project where I could test all approaches without anxiety and without prejudice.

During this exercise, I found that a single contextual approach is most useful. This solution seems to be easier to maintain, and if you need to recreate the domain, you will manage only one file in only one project.

Another aspect that I realized during the exercise is that using LinqToSql is directly inefficient in an organization. If you have a project in which the team will carry out development instead of one person, you must "protect" LinqToSql from them. There must be a “sheriff” that will handle the domain, and you should also use some abstraction mechanism to protect the model from abuse (I implemented the repository template and it worked fine, but you could find different approaches).

I also ran into the problem of creating some logical groups within the domain. Well, what I did was to use some DDD (Domain Driven Design) methods to create the so-called aggregates. Aggregates are a logical arrangement of objects within a domain where you have a root object (which acts as an aggregator) and several other satellite objects connected between them. You can do this by creating several new objects in the LinqToSql domain. These new objects will be disconnected from the database and will work as aggregators. This approach will allow you to create “subdomains” within your domain and help you have a better design.

In the end, I realized that the best way to use LinqToSql is to use context as a simple DAL. Reuse its domain with some extensions (where we can use T4 to help us create code), where entities are converted to DTO (data transfer objects) to bring data to other levels.

I am posting (not yet finished) the steps I took during the exercise on my blog: http://developmentnirvana.blogspot.com/

+5
source

In my opinion, the data context is hidden right behind the repository interface, which allows us to replace the implementation if we like (LINQ-to-SQL / EF / NHibernate / LLBLGen / etc). Thus, the specifics of the data context (s) are mainly implementation details. While he passes unit tests; -p

Huge is rarely a good idea; tiny is rarely useful ... I usually break the system into related pieces (usually associated with various repository interfaces) and think about it at this level. There are a few other thoughts here: Pragmatic LINQ - although I will gladly give away any wisdom from Frans , etc.

+2
source

There are two more things to consider when using LINQ to SQL:

  • While it is not outdated, Microsoft said that the primary goal for future development will be the Entity Framework, not LINQ to SQL.
  • LINQ to SQL connects you directly to the structure of your database. If you use the Entity Framework, you can design your objects in a way that is independent of your database implementation. For example, if you decide to divide one object into two tables, your subscribers should not know.
+1
source

So, after about a year of development in several contexts, I learned that it is not recommended to use several contexts, the problem arises when you have a table that should really exist in 2 or more contexts, as a rule, there are many many relationships. What happens is that in the middle there can only be a record inserted when two other tables inserted their records, so if you use L2S in the same way as for the others, this record will be created first with FK equal to 0 ( Zero), and this is incorrect (referential integrity), which causes an error. This is one of the inconvenient I found, but I could list a few more if necessary. Now the solution that I use is to create a "Scheduler", this guy is responsible for waiting for certain conditions to be met (both parents have a valid identifier other than 0 (Zero)), then he inserts, this is done very generally in order to allow as many specializations as I need (I have 6 contexts), and it uses the PropertyChanged event to somehow notify scheduler changes.

So, the most important thing in this post, USE THE ONLY CONTEXT that if you don’t like headaches (I don’t do this at all). Then, after everything that was said, someone may ask why I continued with several Contexts, well, let's say, it was a decision that came on top of me, and I had zero strength to resist. (Everything, although I really tried, is hard)

0
source

All Articles