Web Layer & Business Layer communication

  • Thread starter Mike Gleason jr Couturier
  • Start date
M

Mike Gleason jr Couturier

Hi,

I'm programming a web site and I separated the web UI layer from the
business objects layer...
But the objects in the business layer needs a query string for database
access.. The client (the website) is responsible to provide this connection
string.

Right now, every time a page needs to interact with a business object
(methods), the web page pass the connection string to the businees object
(each methods of the business objects has a connectionstring parameter).

Instead of doing this, I thought that I could initialize the business layer
with my connection string in the Application Start only once. ie.:
BusinessLayer.ConnectionInfo.ConnectionString =
ConfigurationManager.ConnectionStrings[...].Con[...]; The business objects
would then refer to BusinessLayer.ConnectionInfo every time it has to
connect the to the database...

But I don't know how to propery do that with static objects and
multi-threads implications and all that stuff... Is my idea good? How do you
guys proceed in those scenarios!?

Thanks

Mike
 
S

sloan

Can't you just pass down the DatabaseKeyName to the BusinessLayer?
Maybe in the constructor of your BusinessLayerControll objects?

Go here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry

Get the code:


Change

public class CustomerController
{
public CustomerController()
{

}


to

public class CustomerController
{
private string _dbInstanceName = string.Empty; //DatabaseKeyName

public CustomerController(string dbInstanceName)
{
_dbInstanceName = dbInstanceName;
}


Then push that value to the DAL.

And instead of

["MyAppConnectionString"]

You'll use the

[dbInstanceName] .. that you sent from the BAL to the DAL.



Download my code sample, it'll make sense then.
 
M

Mike Gleason jr Couturier

sloan said:
Can't you just pass down the DatabaseKeyName to the BusinessLayer?
Maybe in the constructor of your BusinessLayerControll objects?

Go here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry


I see... that's because my Business Objects are static classes:

ie.: MyUser u = BusinessLayer.MyUser.Authenticate(user, pass);

So the Authenticate method must take its connection string from somewhere...
right now, its:

MyUser u = BusinessLayer.MyUser.Authenticate(connString, user, pass);

Mike
 
S

sloan

Now you know the one little tiny reason I use non static classes for
controllers.
I can have an overloaded constructor, so I can rely on the default db...or
use a different dbInstanceName.


You can (do what you put in your previous post) HOWEVER: ... I would rename
your parameterName:


MyUser u = BusinessLayer.MyUser.Authenticate(dbInstanceName , user, pass);
OR
MyUser u = BusinessLayer.MyUser.Authenticate( databaseKeyName , user, pass);


Because it would be risky to keep passing full connection strings down.



You can "name" your connection strings.



<connectionStrings >




<add name="SalesDB" connectionString="server=.;database=MyDB1;Integrated
Security=SSPI; Pooling=false; " providerName="System.Data.SqlClient"/>

<add name="CustomerDB" connectionString="server=.;database=MyDB2;Integrated
Security=SSPI; Pooling=false; " providerName="System.Data.SqlClient"/>

</connectionStrings>



In this case, I would pass down SalesDB or "CustomerDB", the KEY name, not
the connection string.
 
M

Mike Gleason jr Couturier

You can (do what you put in your previous post) HOWEVER: ... I would
rename your parameterName:


MyUser u = BusinessLayer.MyUser.Authenticate(dbInstanceName , user, pass);
OR
MyUser u = BusinessLayer.MyUser.Authenticate( databaseKeyName , user,
pass);


Because it would be risky to keep passing full connection strings down.

But What if the data layer is used in a Desktop Environment where it does
know a thing about a web.config?

Thanks!

Mike
 
M

Mike Gleason jr Couturier

Forget my last post, applications uses the same approach with config files!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top