How to use transactions in DotNetNuke (entangled with L2S)? - linq-to-sql

I use L2S at my module. The problem occurs while I'm using the default DNN entities at the same TransactionScope with my L2S data access, then I get a DTC request which I want to avoid.
How can I share the connection/transaction for both DNN entities and my L2S data access?

Sadly, as stated here - transactions are currently not the strong part of DNN (5.1.X), thus L2S operations should not be entangled with DNN core operations to prevent transactions escalation.

I don't know about DNN, but the L2S datacontext has a constructor that takes a SqlConnection (well, a IDbConnection of a SqlConnection) as a parameter. So if you have a connection already established, just pass it in to the L2S datacontext when you create a new instance.

Related

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

Can I reuse the connection from DataContext in Linq to Sql?

DataContext has Connection property of type DbConnection and I was wondering if I could re-use it. I've tried to use it creating a command using CreateCommand and using a reader off of it but I've gotten errors saying there is a pending transaction or something similar.
Basically I'm trying to find out if there is a best practice or guidance on re-using that connection object.
The connection string itself doesn't really help since I can't create a new connection object with the abstract type and I don't want the code to know the specific provider type either.
One alternative approach I'm thinking of doing is having my DataContext derived type to have members that provide a factory method to create a new connection using the same connection information.
FYI, this inquiry stems from the need to invoke an ad-hoc stored procedure through DataContext, not the design-time ExecuteMethodCall variety. I didn't know about ExecuteQuery and for now that suffices. But for other situations where ExecuteQuery is inadequate, I'd need the low-level data access using connection/command etc.
Why not turn it around, and instead supply connections to the datacontext by using the constructor that takes a connection as a parameter? That way you can control when connections are created and disposed, opened and closed, and can reuse them for other purposes without having to worry about the internal behavior of the L2S datacontext...
I can't say definitely, but a connection is a connection. Until it's been closed (or returned to the connection pool), it should be viable to use, whether via LINQ or other means.

Struts- Best way to connect to Mysql in struts 2?

Unable to determine what is the best way to connect to mysql database in struts..
We can always use DriverManger and Class.forName() to connect.
DataSource interface - but this has problems I am getting compliation error for
DataSource dataSource = (DataSource)context.getAttribute(Globals.DATA_SOURCE_KEY);
or when Action.Data_SOURCE_KEY is used. when searched I found that these variables are depricated.
How can I use connection pooling in struts?What is best place to place url,username,pass for database?DO i still have to use datasource configuration in same way in struts-config? Then why was this facility depricated?
Too many queastions but I cannot find a definite source to learn struts.
Struts doc can be but then revisions and backword compatibility are the issues which a learner cannot get easily... Pls suggest a good source to learn struts2.
Struts is an MVC framework, not a database access framework. You should use some sort tool for your Data Access Layer. Spring makes it really easy to manage connections, transactions, and the sort, and integrates well with ORM tools like Hibernate or the JPA implementation.
Where Struts fits in in this is that it will manage the request, delegate to an action, which in turn will invoke a service that uses your data access layer. You could put your DAL in your actions, but I wouldn't -- I would put them in a service.
Struts is a framework having the MVC approach. It makes you to create application in an efficient way. Connection between database is somewhat risk compare to someother connection.

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.

CRUD operations on linq when datacontext ObjectTrackingEnabled = false

A small question. I am using LinqToSql in an n-tier application. I have a datacontext, and I wish to manipulate CRUD operations. I'd rather my datacontext not have object tracking for reasons of scalability. But then, when I have object tracking set to fault I cannot use the built in CRUD operations the datacontext has to offer. So... how do I work around this?
Thank you very much in advance.
You can use Stored Procedures, but with object tracking off, you will not be able to use the "SubmitChanges" method to persist new objects/changes to the database. (also, just fyi, be sure to look into ADO.NET Entity Framework before committing to LINQ to SQL)
Scott Gu - LINQ to SQL Stored Procs
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
-
LINQ to SQL overview
http://msdn.microsoft.com/en-us/library/bb425822.aspx
-
ADO.NET Entity Framework Overview
http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx