Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "Efficiency of Objects Layer" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Reply « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1  
Old May 18th, 2008, 08:58 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default Efficiency of Objects Layer

I have a class within my objects layer that contains various properties relating to a client; forename, surname, address etc.

I have several other client pages that contain different client information, which comes from different related db tables. In total there must be about maybe 60-70 different properties, possibly more.

Would it be better to have all these properties in one class or split them between classes depending on what data is required on a page? Does it make any difference to the efficiency of the code?

I'm just thinking on each page you'd be creating a new instance of a class with several properties that aren't being used on that page.
Reply With Quote
Sponsored Links
  #2  
Old May 18th, 2008, 09:27 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

I'm thinking it would be inefficient to create multiple classes like that.

One thing I noticed from looking over your code is that you are not using constructors. You can have multiple constructors to perform initialization of your class and would look like below:

Code:
Public Sub New()
End Sub
 
Public Sub New(ByVal firstname As String, ByVal middlename As String, ByVal lastname As String)
    Me.FirstName = firstname
    Me.MiddleName = middlename
    Me.LastName = lastname
End Sub
 
Public Sub New(ByVal lastname As String, ByVal email As String)
    Me.LastName = lastname
    Me.Email = email
End Sub
As you can see, the above allows you to initialize your class in different ways. FirstName, MiddleName, LastName and Email are all properties of this sample class. The above also allows you to do the following when calling on the class:

Code:
Dim myclass As New MyClass("Richy", "", "Rich")
Instead of having to explicitly set the value of each property like so:

Code:
myclass.FirstName = "Richy"
myclass.MiddleName = ""
myclass.LastName = "Rich"
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #3  
Old May 19th, 2008, 04:17 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

OK. Makes sense. So you have constructors within your BOL layer that are called from your DAL layer.

How do you then deal with NULL values?

At the moment by explicitly setting the value of each property, I can use:-
Code:
if not isdbnull(rs("middlename")) then client.middlename= rs("middlename")
Using a constructor containing middlename as string would then give an error on NULL values wouldn't it?
Code:
Public Sub New(ByVal middlename as String)
      Me.middlename = middlename
end sub
 
Private _middlename as String = String.Empty
Public Property middlename() as String
       Get
              Return _middlename
       End Get
       Set(ByVal value As String)
              _middlename = value
       End Set
End Property
Code:
Dim client as new ClientBOL(rs("middlename"))
What would happen if middlename was NULL?
Reply With Quote
  #4  
Old May 19th, 2008, 04:50 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Yeah, just tried it for a couple of properties and it gave an error when converting a NULL value...

Hmmm....Is it best to alter the DB to not store NULL values or to handle the object creation differently? It worked fine without the constructors and explicity defining each property.

I've always understand that it's better to use NULL as it's more "definite" than blank or empty values. I can't see the point in dealing with the NULL values in the SQL code as you may as well not store them as NULL in the first place.

What do you think is the best way to go?
Reply With Quote
  #5  
Old May 19th, 2008, 07:30 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

Quote:
Originally Posted by richyrich View Post
OK. Makes sense. So you have constructors within your BOL layer that are called from your DAL layer.
Yeap

Quote:
Originally Posted by richyrich View Post
Hmmm....Is it best to alter the DB to not store NULL values or to handle the object creation differently? It worked fine without the constructors and explicity defining each property.

I've always understand that it's better to use NULL as it's more "definite" than blank or empty values. I can't see the point in dealing with the NULL values in the SQL code as you may as well not store them as NULL in the first place.

What do you think is the best way to go?
The use of NULL is a much debated concept. Here's a good read on the subject which is against the use of NULL: NULL values in a database

Personally, I do NOT like using NULL values. First off, they are more of a pain than they are helpful. Secondly, what does NULL mean? It's really not descriptive of why a value wasn't entered. The examples in the above article explain this.

If you do choose to use NULL in your database, then you can allow your properties you use Nullable data types as I have shown you earlier. In the case of strings, which donot have a Nullable Type, you just have to do some validation before passing the value to the object.
Reply With Quote
  #6  
Old May 19th, 2008, 08:00 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

OK, assuming I keep using NULL, is there an advantage to using a constructor?

From your previous post, you said that it wouldn't be a good idea to create new classes for each client page with different information. So, if the ClientBOL class contains all the properties anyway, creating an instance of this class is still going to give me some redundant properties on each page isn't it? Irrespective of whether I use a constructor or not.

I just imagine that a class with 60-70 properties is going to be more resource intensive than one containing 20-30 properties. Or is each class compiled when the App is loaded, irrespective of whether you create an instance of it on the page or not?

Does that make sense?
Reply With Quote
  #7  
Old May 19th, 2008, 08:22 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

Constructors help initialize member variables. Take a look at the following:

VB.NET Language - Lesson 12: Class Construction and Destruction

Pay particular attention to the sections labeled Class Construction and Class Deconstruction.

Considering most experienced programmers go about this the way I have been showing you, I doubt there are any performance issues. Especially since .Net automatically performs garbage collection.

Regardless, if you do experience performance issues, maybe you need to redesign your class. Why do you have so many properties? Perhaps I misunderstood your question about creating multiple classes. I have apps that have multiple classes. Some of these classes use other classes as properties. For example, an application for an address book might have the following classes: Contact and Address

With these two classes, the Address class could be used as a property of the Contact class. This breaks down the amount of properties for each class. Is this what you were considering?
Reply With Quote
  #8  
Old May 19th, 2008, 08:39 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Basically, the App contains details of our clients. As there's alot of information, it's split between various pages.

So, the first page contains name details, address, phone numbers etc. The second page contains some other details, the third page some more details etc.

So, if you were to view Page 2, for example, you wouldn't need to know the address details, so it seems a waste of resource to create an instance of a class that holds all the properties of a client for each page.

Does that make more sense?
Reply With Quote
  #9  
Old May 19th, 2008, 08:44 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

Absolutely! In this case, I would definitely have this broken into multiple classes: User and Address, with address also containing the phone numbers.

You could also have address as a property of the User class as in my example above, if you needed to.
Reply With Quote
  #10  
Old May 19th, 2008, 08:54 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

So, would something like this be right?
Code:
Public Class ClientDetails
 
     Private _forename as String = String.Empty
     Public Property forename as String
           Get
                   Return _forename
           End Get
           Set(ByVal value As String)
                   _forename = value
           End Set
      End Property
 
      Private _AddressDetails As AddressDetails
      Public Property AddressDetails as AddressDetails
            Get
                   Return _AddressDetails
            End Get
            Set(ByVal value as AddressDetails)
                   _AddressDetails = value
            End Set
       End Property
 
End Class
 
Public Class AddressDetails
 
        Private _add1 As String = string.empty
        Public Property add1 as String
             Get
                    Return _add1
             End Get
             Set(ByVal value as String)
                    _add1=value
             End Set
        End Property
 
End Class
Then to access the address details have something like:-
Code:
Private ClientDetails as ClientDetails
 
ClientDetails.AddressDetails.add1 = rs("add1")
.
.
.
.
txt_add1.text = ClientDetails.AddressDetails.add1
Am I along the right lines or way off?
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Tags
class, constructors, properties

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -4. The time now is 07:36 PM.



Content Relevant URLs by vBSEO 3.2.0