Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio
This is the forum to discuss the Wrox book ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solution by Vincent Varallo; ISBN: 9780470396865
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 27th, 2009, 02:36 PM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Thanks!
Ingmar
Reply With Quote
  #2 (permalink)  
Old March 30th, 2009, 10:30 PM
Wrox Author
 
Join Date: Jan 2009
Posts: 73
Thanks: 0
Thanked 7 Times in 7 Posts
Default

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,
Vince
Reply With Quote
  #3 (permalink)  
Old March 31st, 2009, 01:55 PM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!
Reply With Quote
  #4 (permalink)  
Old March 31st, 2009, 05:49 PM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.

-realkewl
Reply With Quote
  #5 (permalink)  
Old April 1st, 2009, 07:56 PM
Wrox Author
 
Join Date: Jan 2009
Posts: 73
Thanks: 0
Thanked 7 Times in 7 Posts
Default

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.
Reply With Quote
  #6 (permalink)  
Old April 30th, 2009, 05:24 PM
Authorized User
 
Join Date: Apr 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

regards

Last edited by luckystar; April 30th, 2009 at 07:01 PM. Reason: clarification
Reply With Quote
  #7 (permalink)  
Old May 13th, 2009, 11:37 PM
Wrox Author
 
Join Date: Jan 2009
Posts: 73
Thanks: 0
Thanked 7 Times in 7 Posts
Default

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.
Reply With Quote
  #8 (permalink)  
Old May 14th, 2009, 04:37 AM
Authorized User
 
Join Date: Apr 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

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.

regards

Last edited by luckystar; May 14th, 2009 at 04:59 AM. Reason: clarification
Reply With Quote
  #9 (permalink)  
Old June 1st, 2009, 02:04 PM
Authorized User
 
Join Date: Apr 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

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
Public
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

regards
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Columns & Dynamic Grouping ??? nileshgambhava BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 1 March 24th, 2008 08:58 AM
help writing dynamic form data to dynamic table ublend SQL Server ASP 1 June 1st, 2007 09:09 AM
help writing dynamic form data to dynamic table ublend Classic ASP Professional 1 June 1st, 2007 09:08 AM
Dynamic Columns & Dynamic Grouping ??? nileshgambhava SQL Server 2005 0 December 12th, 2006 12:45 AM
Dynamic Checkboxes using dynamic Select Values bsheridan Classic ASP Databases 0 March 12th, 2006 10:17 AM



All times are GMT -4. The time now is 02:10 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.