Hi,
I'm new to .NET and am trying to take advantage of the
object structure while accessing relational databases.
I started a small project for managing "projects". Here is
a description of my data structure :
A project would be made of tasks, and each task can
contain sub-tasks, so I would have something like
- Project1
- task1
- subtask A
- subtask B
- task 2
- Project2
...
(each line being a task).
Then, each task can "depend" on others : it could only
start when all the tasks it "depends on" are performed.
For instance : Project1/task2 "depends on"
Project1/task1/subtaskA.
So I end up with the following datamodel :
[Table "TASKS"]
int id_task [key] : unique identifier of the task
string name, description : ...
int id_parent : identifier of the parent task
[Table "TASK_RELATIONS"]
int id_task_1 [key] : identifier of the "preceding" task
int id_task_2 [key] : identifier of the "following" task
(the one that can only be started once id_task_1 is done).
And (here comes the problem), I would like to have an
object structure close to the following (I describe each
type ; "+" indicates a public property or method of the
described type) :
public static "MyApplication" : "global" container object.
+ TaskCollection "Projects" {get;} : collection of all
the top-level tasks (id_parent=0).
+ TaskCollection "AllTasks" {get;} : collection of all
the tasks.
public TaskCollection : a collection of "Task"s.
+ Task this[int index] {}
public Task : a task
+ int ID (this one might not even be useful as a public
prop)
+ string Name, Description : ...
+ string FullName {get;} // returns the concatenation of
this task and of all its
ascendants : "Project1/task1/subtaskA" for instance.
+ Task Parent {get;} // returns the task's parent task.
+ TaskCollection Children {get;} // returns a collection
with all the children tasks
+ TaskCollection PrecedingTaks // returns the tasks that
must be completed before this task can begin
+ TaskCollection FollowingTask // returns the task that
can begin once this one is done.
MyApplication contains also a "Data" property that is a an
object holding my 2 DataTables (MyApplication.Data.Tasks
and MyApplication.Data.Task_relations).
My BIG question is : how do I operate the mapping between
the two (the DB data model and the Object structure) ?
To have you better understand the requirements of my
model, here is a description of a "Projects" Form I
already started to write to allow the user to edit the
projects :
It contains a tree on the left (that uses
MyApplication.Projects to display all the tasks), and
a "detail" pane on the right. The detail pane is bound to
MyApplication.AllTasks, and the position of the
BindingManager is set by the Tree when the user clicks a
task.
The pane on the right contains standard controls
(TextBox "txt_Name", with property Text bound to "Name")
and some more complex controls
(ListBox "lst_preceding_tasks" with property DataSource
bound to "PrecedingTask").
Here are the first conclusions I could draw from reading
the documentation :
- for my "Tasks" to be bindable and editable in Windows
Forms, TaskCollection should implement IBindingList, and
Task should implement IEditableObject.
- I wanted to extend the DataView and DataRowView classes
as follow :
Task : DataRowView {}
TaskCollection : DataView {}
and make them "point" to the MyApplication.Data.Tasks
DataTable.
I had to give up as I saw that DataRowView cannot be
inherited (problem with the constructor, which is
internal, or something like that... [could you inherit
it ?]).
- Finally I tried to write to new classes "containing" the
DataRowView and DataView classes :
public class Task : IEditabledObject {
private DataRowView row;
public Task (DataRowView aRow) {
row = aRow;
}
public string FullName {...}
...etc.
}
public class TaskCollectoin : IBindingList {
private DataView view;
public TaskCollection (string filter) {
view = new DataView (MyApplication.Data.Tasks,
filter, "", DataViewRowState.CurrentRows);
}
... (implementation of some of the IBindingList
interface methods and properties) :
object AddNew() {
return new Task(view.AddNew());
}
object this[int index] {
get {
return new Task (view[int]); // what do you
think of this ?
}
}
... (mapping of ALL other methods and properties on the
view object) :
bool IBindingList.AnyProperty { get{return
view.AnyProperty;}}
...
}
What do you think of this solution ? Is it the right way
to do it ?
Here is a first problem, to begin with :
"Linking" a TaskCollection to a DataView of
MyApplication.Data.Tasks is OK as long as I want to use it
to edit the tasks. So for MyApplication.Projects it is
fine. I can Add Tasks to the Collection, it will insert a
new row in my "Tasks" table.
But for the "Children" or "PrecedingTasks" properties of
the Task object, the collection should rather be linked to
the MyApplication.Data.Task_relations table. Adding a Task
to the collection should insert a row in
the "Task_relations" table !
Should I have two different objects ?
For instance a TaskCollectionBase object, and one
extension of this class per type of collection I need ?
(PrecedingTaskCollection and FollowingTaskCollection would
point to the same table, but adding a Task to them would
insert a row with id_task_1=this.row[id_task] for the one
and with id_task_2=this.row[id_task] for the other).
Well, thank you very much for any hint, piece of advice,
or comment.
Please tell me if you know of any documentation on that
topic, or forums more specifically dealing with that kind
of problems...
Thanks again !
Olivier.