The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Business Object Design

I have the following sample class where the property value will come from the database and in the database the value can be null. I believe this is a common problem if one wants to design bisuness objects. I have read about the concepts in many books but no one talks about to solve this problem.

In .Net 2.0 there is a solution, but since this is a common problem, I was wondering how other have solved it before.

public class SampleClass
    {
        private string stringProp;
        private int intProp;
        private DateTime dateProp;

        public SampleClass()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public int IntProp
        {
            get
            {
                return intProp;
            }
            set
            {
                intProp = value;
            }
        }

        public string StringProp
        {
            get
            {
                return stringProp;
            }
            set
            {
                stringProp = value;
            }
        }

        public DateTime DateProp
        {
            get
            {
                return dateProp;
            }
            set
            {
                dateProp = value;
            }
        }
    }
pothik Send private email
Thursday, February 16, 2006
 
 
In .net 1.1, I used the nullable type libary that's part of the NHibernate contrib distribution as shown below. I think .net 2 has a nullable type library but not sure.

This worked quite well. Nulls could be stored/retrieved in the database while not making business objects into a confusing mess.

This nullable type libary is somewhere near http://www.hibernate.org/343.html

Eg)
-----------------------------
Imports System.ComponentModel
Imports Nullables

<Serializable()> _
Public Class CompanySite
    Inherits BusinessObject

    Private mDateDeleted As NullableDateTime

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property DateDeleted() As NullableDateTime
        Get
            Return mDateDeleted
        End Get
        Set(ByVal Value As NullableDateTime)
            mDateDeleted = Value
        End Set
    End Property

End Class
Kim
Thursday, February 16, 2006
 
 
And the problems were:
1) My business objects where components so databinding could be set up using the web forms designer. VS has a problem persisting the changes from the designer to the code hence this line.    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>

2) I've tried the same objects on win forms and it seems to have a problem databinding the nullable types to Textbox controls when they are null, although the data grid is fine. Might be a small problem.

But that's it. It was largely smooth sailing. The nullable types can be treated as normal value types for arithmatic but with the ability to check if they are null.

<rant>
This is yet another thing MS Access makes seem so easy. Why other popular languages make dealing with databases a complete pain is a mystery to me.
</rant>
Kim
Thursday, February 16, 2006
 
 
<i><rant>
This is yet another thing MS Access makes seem so easy. Why other popular languages make dealing with databases a complete pain is a mystery to me.
</rant></i>

MS Access isn't a language. It's more of a JET front end.

Languages don't make it a pain (or perhaps you could elaborate on which ones do). DBNulls aren't the same thing as a zero length string whatsoever. I don't want a crippled language that assumes or considers it as the same thing. Yes, you have to test for them and handle them if you use them (you should avoid them as much as possible in the first place), but that's how many things are... If you wanted so make them (DBNulls and zero lenght strings and such) act the same, you can easily make your own classes, similarly to the new nullable types. It's not that complicated nor a lot of work if you understand basic OOP concepts.
Well DUH
Friday, February 17, 2006
 
 
In .NET 1.1 I made my own nullable types.
Turtle Rustler
Friday, February 17, 2006
 
 
==> In .NET 1.1 I made my own nullable types

Why? You can use the already built-in SQLTypes. Seems like a lot of extra work to me.
Sgt.Sausage
Friday, February 17, 2006
 
 
My custom types handle a lot more than just the ability to handle nulls. They provide automatic mapping to database columns (no OR mapper needed) and things like maximum lengths, basic validation, and such. It is one part of an overall strategy to create a business object framework that didn't depend on OR mappers or code generators.

It has worked out nicely.
Turtle Rustler
Friday, February 17, 2006
 
 
'Well DUH ',
I did not mean to imply MS Access is a language. VBA is the langauge used in Access. It does not treat zero length strings as nulls. Who said it did??

"you can easily make your own classes, similarly to the new nullable types. It's not that complicated nor a lot of work if you understand basic OOP concepts"

Exactly, why is there not support for this already in a language like C#? Programmers need to do it themselves or get some additional library.

(little pain) X (lots of people) == (big pain)
That's what I call painfull!

Joel's rant here describes the problem well.
http://www.joelonsoftware.com/oldnews/pages/March2004.html
Kim
Monday, February 20, 2006
 
 
For each Business Object I create a corresponding Data Transfer Object. The DTO's properties are all System.Data.SqlType(s). I have static methods in my DAL which map the BO properties to the DTO properties. The DAL then uses the DTO to write to and from SQL Server. This can be facilitated using CodeSmith
Robert Zurer Send private email
Sunday, February 26, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz