OOD Question

  • Thread starter Thread starter Alok Jain
  • Start date Start date
A

Alok Jain

This is a design / inheritance qus.
I have class Task.
This is a base class for some specific tasks

Say
WorkOrderTask : Task
TimeSheetTask : Task

Now Task has a function GetDetails() which gets all info about task. This
makes a database call.

I override this in the derived class. For implementing this in derived
class i see following 2 approaches.


Approach 1. WorkOrderTask .GetDetails()
{
Calls base.GetDetails(); to get base details, populates in the
object
Gets details specific to WorkOrderTask, populates in the object
returns;
}



Approach 2. WorkOrderTask .GetDetails()
{
Get details specific to WorkOrderTask as well as the base class
details, populates in the object
returns;
}


The problem with

Approach 1
I have to make 2 database calls.

Approach 2
If theres some change in base class I need to take care of these in all the
derived classes as well.
Whereas, Approach 1 is advantageous here as by modifying only the
Task.GetDetails funciton ( this is the base class ) function i have all the
values and i do not need to touch the derived classes.



Was thinking this might be a very common problem, so perhaps many of might
be able to guide me on the approach i must take here. Perhaps anything other
then the two i have talked about.

Thanks in advance
Sourabh
 
Hi Alok,


I would use the approach 1, Each class should be responsible of itself and
it may have some private functionality that a derived clas has no knowledge
of.

The second approach would get more complex as you keep adding inheritance,
so I would deprecate it.

Cheers,
 
Alok,

The approach I would take is to use attributes. It would seem that the
details that you get are stored in fields of your database which are
type-specific. For example, your work order task might have the work order
id that it needs to get.

In this case, you can do two things. You could have the base class
define virtual methods which the derived classes would provide
implementations for which would give information about how to alter the
query being made (additional fields, joins, etc, etc). However, since each
instance of the derived classes will give the same information, you don't
need to have this on an instance level. I would recommend using attributes
instead. You could create custom attributes which your derived classes
would place on themselves. These attributes would give the same information
about which fields to select, join on, etc, etc, that you would need to form
your query.

Hope this helps.
 
This makes sense. Thanks

Alok


Ignacio Machin ( .NET/ C# MVP ) said:
Hi Alok,


I would use the approach 1, Each class should be responsible of itself and
it may have some private functionality that a derived clas has no knowledge
of.

The second approach would get more complex as you keep adding inheritance,
so I would deprecate it.

Cheers,
 
If I understand this right This means that the base class takes care of its
members, it will have member which will define its attributes in the SQL
command and similarly we have another member (function) to assign values
from the returned result set.

class Task
{
protected string getAttributes()
{ return "TaskId,Description,...";
}

protected void setAttributes(DataReader dr)
{
//code to set respective attributes
}
}

class WorkerTask : Task
{

public void getDetail()
{
//construct sql
string sqlStr = "select D1, D2" + base.getAttributes();

//execute query

//call base.setAttributes to set base class attributes

}
}

At this time I don't see any implementation issue, I will like to hear if
there is some better approach.

Thanks again
Alok


Nicholas Paldino said:
Alok,

The approach I would take is to use attributes. It would seem that the
details that you get are stored in fields of your database which are
type-specific. For example, your work order task might have the work order
id that it needs to get.

In this case, you can do two things. You could have the base class
define virtual methods which the derived classes would provide
implementations for which would give information about how to alter the
query being made (additional fields, joins, etc, etc). However, since each
instance of the derived classes will give the same information, you don't
need to have this on an instance level. I would recommend using attributes
instead. You could create custom attributes which your derived classes
would place on themselves. These attributes would give the same information
about which fields to select, join on, etc, etc, that you would need to form
your query.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alok Jain said:
This is a design / inheritance qus.
I have class Task.
This is a base class for some specific tasks

Say
WorkOrderTask : Task
TimeSheetTask : Task

Now Task has a function GetDetails() which gets all info about task. This
makes a database call.

I override this in the derived class. For implementing this in derived
class i see following 2 approaches.


Approach 1. WorkOrderTask .GetDetails()
{
Calls base.GetDetails(); to get base details, populates in the
object
Gets details specific to WorkOrderTask, populates in the object
returns;
}



Approach 2. WorkOrderTask .GetDetails()
{
Get details specific to WorkOrderTask as well as the base class
details, populates in the object
returns;
}


The problem with

Approach 1
I have to make 2 database calls.

Approach 2
If theres some change in base class I need to take care of these in all the
derived classes as well.
Whereas, Approach 1 is advantageous here as by modifying only the
Task.GetDetails funciton ( this is the base class ) function i have all the
values and i do not need to touch the derived classes.



Was thinking this might be a very common problem, so perhaps many of might
be able to guide me on the approach i must take here. Perhaps anything other
then the two i have talked about.

Thanks in advance
Sourabh
 

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

Back
Top