p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio (http://p2p.wrox.com/forumdisplay.php?f=477)
-   -   Dynamic DataContext? (http://p2p.wrox.com/showthread.php?t=73600)

softwarea March 27th, 2009 01:36 PM

Dynamic DataContext?
Hi Vince,

I just got your book a couple of days ago and I really like how everything engages with each other. However I have a question concerning the DataContext in the DAL.

Though I'm still in chapter 4 I assume that sooner or later you will add every table in your database to the HRPaidTimeOffDataContext. Creating classes like ENTUserAccountData for every table ensures that every entity object knows how to insert, update or delete itself in the database. So far so god.

My problem is that all code in the DAL classes is using ONE DataContext (HRPaidTimeOffDataContext). What if your application would have more than one DataContext class?

Here is a scenario why I'm interested in that: Imagine you had two different customers who would both want to use your application. Instead of setting up two separate platforms (2 applications + 2 databases) you want to be smart and have rather one application for both of them. You would set up two databases though, one for each customer. The two databases would be pretty similar but not equal, because every customer has his special requirements. In the end you would probably have at least two DataContext classes like "EverybodysDataContext" and "CustomerSpecificDataContext". The tables that both customers have in common go in the EverybodysDataContext, the other tables go in the CustomerSpecificDataContext. Now that you have two or more DataContexts you would still want that any ENTBaseData object knows how to handle the CRUD operations, right? But the declaration of the ENTBaseData class refers to one concrete DataContext.

Do you see any way to have the type of DataContext being dynamic in your base classes? In other words: no matter how many DataContexts my application would have, all my entites could still inherit from ENTBaseData.

I realize this can not be answered with a simple Yes or No. And in case I didn't simplify the problem enough, please feel free to ask. But I would appreciate any suggestion you had for me.


varallov March 30th, 2009 09:30 PM

Great question. What you can do is modify the ENTBaseData class so the DataContext is a generic parameter.

publicabstractclassENTBaseData<T, DC>
where T : IENTBaseEntity
where DC: DataContext, new()
publicabstractList<T> Select();
publicabstract T Select(int id);
publicabstractvoid Delete(DC db, int id);
publicvoid Delete(string connectionString, int id)
using (DC db = new DC(connectionString))
Delete(db, id);

Replace any instance of the HRPaidTimeOffDataContext in the base class with DC. Then when you define the inheriting data classes you simply tell the compiler which DataContext to use.

publicclassMyNewClassData : ENTBaseData<ENTEmail, HRPaidTimeOffDataContext>

Hope this helps,

softwarea March 31st, 2009 12:55 PM

Hi Vince,

yes, indeed, that helps a lot. I was thinking of such a solution already.
Just wasn't sure of possible side effects.

So, thank you very much for your help!

realkewl March 31st, 2009 04:49 PM

Hey Vince, do you know if linq to sql has been implemted on a large scale. I will be working with 100's of sql tables and lots of relationships and to create an entity for each table seems like a lot of work. Just curious, I'm from the old school of using stored proc and no user ever has access to the table objects.

Please excuse me, but I'm also new to linq to sql so maybe I just need more time to learn it. However I do link the fact that I can drop in the stored procs.


varallov April 1st, 2009 06:56 PM

I use it on large scale databases but I create multiple data contexts for "groups" of tables. I try to group the tables together by department or something that makes sense. This makes it easier to have multiple developers working on one project especially when using sourcesafe.

If you prefer not to use LINQ to SQL you can always pass the data between the DAL and BLL with datareaders. Everything else in the pattern will still work, you'll just need to make some changes in the bases classes and the BLL classes that reference the entity object. This is how I used to do it before LINQ to SQL. I'm now changing it againg to work with the Entity Framework.

luckystar April 30th, 2009 04:24 PM

That worked fine until I got to the BaseEditPage class where one needs to specify the datacontext. my ENTBaseEO is declared to cater for multiple databases ie

PublicMustInheritClass ENTBaseEO(Of DC As {DataContext, New})
Inherits ENTBaseBO

The BaseEditPage use ENTBaseEO as a constraint
I am very reluctant to have the UI reference the DAL. Any idea is appreciated


varallov May 13th, 2009 10:37 PM

See my original response above. The base class can be changed to make the DataContext a generic. When you define the Data class you simply tell it which data context to use.

luckystar May 14th, 2009 03:37 AM


Yes I know, please read my question carefully I did the same with the business classes EO objects then when I got the BaseEditPage which uses ENTBaseEO I get stuck; my ENTBaseEO is also a generic type ENBaseEO(Of DC as DataContext..) as it uses DC for
save update etc.. The reason I get stuck is I am reluctant to specify a datacontext in the BaseEditPage class for several reasons 1- the UI should not talk to the DAL, 2- the BaseEditPage class is used by many inheriting pages and cannot be datacontext specific.

And also in your response above Using db as new DC(connectionString) vb.net does not seem to accept the connection string parameter.


luckystar June 1st, 2009 01:04 PM


I also have multiple datacontext. I needed to use generics in the BLL classes as well to make them datacontext independent.

I am encountering issues with the baseEdit page and audit trail.

My ENTBaseEO is declared as
MustInheritClass ENTBaseEO(Of DC As {DataContext, New})
Inherits ENTBaseBO

and when I come to the base editPage I encounter the problem of having to specify the datacontext (which would not work of course so to solve the problem the edit page is a follows

PublicMustInheritClass BaseEditPage(Of T As {ENTBaseBO, New})
Inherits BasePage

I use the ENTBaseBO which is common to all

I am now encountering problems with the auding where for example the added mehtod "AuditAdd" to the ENTBaseEO is also context specific. (chapter 12 page 436)

So please, if you have had similar issues I would be grateful if you could share them


All times are GMT -4. The time now is 10:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.