Solved

shared classes and function in asp.net application

Posted on 2010-09-16
5
395 Views
Last Modified: 2012-08-13
when is it appropriate to use shared classes. Are there any adavantages/disadv. to using non-shared functions.

For a data access layer would it be ideal to use a shared class. Please suggest.
0
Comment
Question by:TrialUser
5 Comments
 
LVL 7

Accepted Solution

by:
klakkas earned 167 total points
ID: 33691137
My rule of thumb is the following:

Use a Shared class if all the code that uses this class works under the same settings. This way, you only initialize the Class once and everyone has very fast access to it.

The example you use of the Data Access Layer is ideal. When your application wants to get data from a DB, if you don't use a shared class you would have to initialize the connection every single time. Using Shared Class, you only initialize the connection once and you can access it from anywhere in your program.

Finally, using Shared Classes reduces the coding.
With non Shared, you should do
Dim obj as new myObject
Res = obj.doWork

As with shared, you just say
Res = myObject.doWork
0
 
LVL 25

Assisted Solution

by:Luis Pérez
Luis Pérez earned 167 total points
ID: 33691142
For my data access layer I always use shared functions. I think if not neccesary to instantiate explicit objects of my DAL to call data methods.

Think in shared functions this way. Do you really need to create a instance of an object to call a method or function that doesn't need to use any data of that object itself?

Hope that helps.
0
 
LVL 7

Expert Comment

by:ajolly
ID: 33691146
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 166 total points
ID: 33691278
Well, part of my standard Data Access library is implemented as an abstract factory, which means static/shared methods for creating database objects. Ditto for specific data manager classes. A method that retrieve Customers from the database, for example, isn't necessarily going to benefit from being in a concrete object if it is literally only acting as a proxy for a database procedure.
0
 

Author Closing Comment

by:TrialUser
ID: 33781801
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question