Linq to SQL and ASP.NET MVC 3 Integration - linq-to-sql

How tightly can the ORM data classes generated by LINQ to SQL be integrated with the Model layer in MVC? Could these ORM data classes act directly as the model, if so, how would validation occur as it is usually done in MVC 3 with DataAnnotations? It seems that it would be diffiult to add these to the generated classes.

Separation of Concerns suggests that the classes that make up your domain model shouldn't be dependant on the technology that's used to implement your Data Access Layer, so I make every effort to ensure that they're not propogated through the UI.
If you use Entity Framework instead of Linq-to-SQL, you can use the POCO generator instead of EF's default EntityObjects. While these are still generated from the EF database model, you can replace the data access implementation but retain the POCO's for your domain model.

Related

What is the difference between that various data templates microsoft provides?

Note: This is not about the difference between Database first, Model first, and Code first.
Microsoft has a number of tools to simplify using DbContext. Unfortunately, there seems to be almost no description and also no documentation on what they are, much less what they do.
What is the difference between:
Entity Framework Power Tools CTP1
ADO.NET C# DbContext Generator
ADO.NET C# POCO Entity Generator
I don't usually answer my own questions, but here is what I figured out:
The difference between the ADO.NET C# DbContext Generator and the ADO.NET C# POCO Entity Generator is that the former creates a context based on the DbContext and the latter creates them based on the ObjectContext.
Basically, these are used in the Model First and Database First approaches. The difference between Model First and Database First is that in Database First, you define your data model in the database, then reverse engineer the model (ie. create an .edmx file) from the database. While with Model First, you create your model in the designer (again, the .edmx file) or by hand, then generate the database from that model.
In both cases, you then generate POCO classes and either a DbContext or ObjectContext from the .edmx file.
The Entity Framework Power Tools CTP1 reverse engineers a Code First model from the database, including POCO class, the DbContext (don't think it offers ObjectContext generation) and the mappings (via the OnModelCreating method).
So what this boils down to is that in Database First and Model First, the "model" is defined by the xml .edmx file (or in some cases, several files). While in Code First, the model is defined using fluent code mappings in OnModelCreating.
When using the Power Tools to reverse engineer the database, it doesn't create an .edmx file, instead creating the mappings in code. Thus, skipping the xml middle man.
ADO.NET C# POCO Entity Generator exists for a long time and is part of the Mode First approach. Read about it here.
The differences between ADO.NET C# POCO Entity Generator and ADO.NET C# DbContext Generator are discussed here: ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext).
Entity Framework Power Tools CTP1 is a drop of useful tooling for the project's context menu.

Linq-to-SQL - Application architecture

I'm trying to design application that will have UI with database in the backend.
I will be using Linq-to-SQL as the database layer to update and insert.
Now I'm trying to find out the best practice to use in designing the project, suppose I have 2 tables in the DB (Customers, Orders)
Shall I depend on the generated Linq-to-SQL classes, or shall I still create classes for Customers, Orders?
Shall I wrap the generated Linq-to-SQL inside another class to add validations?
I hope my questions are clear.
L2S is in my opinion an excellent light-weight data access method. If you have control over the database and have limited application data processing logic it is often a good choice.
If you have a two-tier app with a UI communicating directly with the DB then you can depend on the L2S generated classes. If you have a multi tier app with a client communicating with e.g. a WCF service you probably need Data Transfer Objects.
Use the partial methods on the L2S classes for validation.
I think you should use other ORMs for better implementation DAL for example Entity Framework or Nhibernate this ORMs allow you Model First approach without attributes
and the validation logic you should separate in other classes for exmaple MyEntityValidator
And also good approach to use the Repository pattern this pattern allow doesn't depend on Data access EF or Nhibernate
and look at this Entity Framework and Repository

Developing enterprise level application using LINQ

Using LINQ to SQL make application development faster but dissolves the logical layers in the application. The data access layer and the business objects layers almost have no identity, they sit in the same dll. Does any one has an idea on how to develop an enterprise level application using LINQ to SQL. How do we cleanly separate the business object and the LINQ generated entities ? How would they communicate, how would data be transferred between our business objects and LINQ entities. Any article or any suggestions towards this would be greatly appreciate. Thanks.
We're using L2S for our next generation of software that manages our plant operations and related applications. This is for a $2.5B thin film solar company. We have built a clearly defined L2S based n-tier application framework.
We also created our own code generator to generate an application set of entities, a L2S set of entities, a business logic layer and data access layer. The L2S set of entities is for back-end use only. The application entities (which have no L2S plumbing built in) is for transferring data back and forth from application to server. We use WCF for application tier to server tier communication.
Our applications use WCF to call to the back-end businss logic layer for data processing. The business logic layer calls to our data access layer for low level Linq based data access. Our application entities get passed to and from our back-end. In the back-end, we have very efficient mapping that maps an application entity to each L2S entity.
Works very well for us.
Randy
You can get very far with L2S (as StackOverflow has proven), but IMHO Linq2SQL is not well suited (nor intended, I think) for "enterprise level applications".
Now that Entity Framework 4.0 has been released, you may want to consider going with EF instead. It supports POCO and will allow you do have a much nicer layered architecture.
Check out:
The ADO.NET Entity Framework
ADO.NET C# POCO Entity Generator
I recently ported a substantial code base from L2S to EF 4.0. Since EF now supports lazy-loading, you can have a very smooth transition from L2S to EF, leveraging the advanced features of EF only when you need them.

Use LINQ generated classes directly?

LINQ will generate a set of classes from the SQL files. Should these be used directly, or should they be wrapped in another class so the model is not so dependent on the implementation?
You can do it either way. Generally I wrap the Linq to SQL classes in a repository, but if the app is small you can use the repository methods directly.
If the app is larger you can add a business layer.
If you actually need to abstract from your sql database's model, then Linq-To-Sql is probably the wrong choice. Sure, you can make it work (but that isn't what it was made for).
If you need that level of abstraction, you will want to move on to a more "enterprisey" ORM like Entity Framework. They require more configuration, which is used to specify the more intricate mappings that allow your object model and database model to not resemble each other,
On the other hand, if this is overkill then use Ling to Sql. It's simple and it's easy, as long as you can stick with its simplified approach to mappings.
I think it's fine to use the generated model classes directly in your business and presentation tiers - however, I would definitely encapsulate data access for those entities inside a repository pattern of some description (GetOne(), Save(), Search(), Delete() etc).
The main reason for doing so is to 'disconnect' query results before returning them to a calling layer, so that clients don't inadvertently execute queries directly against the database when they use LINQ on returned results. Eg, calling ToList() on an IQueryable<T> will return a local copy of the sequence that can be managed using plain LINQ to Objects.
It also promotes better separation of layers and less coupling, as clients will interact via interface methods on the repository, rather than use LINQ to SQL directly for data access, so if you do decide to chuck LINQ to SQL in favour of the Entity Framework (shudders), it's easier to do the refactoring.
The one exception I would make is when LINQ to SQL objects need to cross a service boundary, ie, sent as data transfer objects to or from a WCF service. In this case, I think it's a good idea to have a separate, light-weight object model that supports serialization - don't send your LINQ to SQL objects directly over the wire.

Does anyone use the generated entity classes on a large project?

In the NerdDinner example they use a repository pattern to decouple the business from the data layer. But then they use the Linq to SQL generated classes (Dinner specifically) as the entity class used throughout the project. So how decoupled is that really? It’s not like you could easily exchange Linq-to-SQL.
On my last project I created a separate entity class that I populated with left/right in the linq query because I found that even if you use a partial of the linq generated you cannot populate any additional fields that you add at query time.
LINQ to SQL is strongly tied to the database schema, which is why I wouldn't use it. I'd use Entity Framework instead, as it permits a mapping between the conceptual and logical models.