Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How UML "association class" should be implemented?

Status
Not open for further replies.

liuk

Programmer
Jan 16, 2006
54
0
0
IT
Hi everyone,
i'm new to object-oriented programming and i have a question.
Let's consider this example : two classes Employee and Project related by an association class Role which depict the role each employee has in projects.

Now suppose that i want to get a list with projects, employes and their roles.

I think i have to provide Project class with a method that retrieve from DB the list of all project and for each one the data about the employes and their role.

The question(s) is (are):
- should i create an instance of employee and role for each project i retrieve from the DB (suppose that i want to invoke some method of Role and get other data to show)
- If i want the detail about a specific employee should i load data from db (also about role and project) within the employee class and then create an instance of the related class? or each class should load data by itself?

I don't know which is the best way to implement this situation. does anyone have some advice?
thanks.
 
Here is one possible solution.

A Role class, with an Employee property and a Project property.

An Employee class with a GetRoles() method, which returns a list of all the roles they have in various projects.

A Project class, with a GetRoles() method that returns a list of all the roles it is currently being fulfilled by.

This structure is traversable so that the associations can be displayed in various list forms.

Another option would be to use a query to join the three tables and return a denormalized view for each employee or project for the purpose of quickly displaying lists.
 
thank you dragonwell for your reply.
generally speaking in situation like that where association class are involved do i always need to instantiate all three class to save data, for example, to DB or to retrive data from it ? And if yes, each one does save their own data or its better to have some DBClass that performe this kind of operation receiving the instance of the class to obtain the data to store( or load to) ?
 
Hi liuk,
In retrospect I might not have been exactly correct in suggesting the Role class as the association class. Maybe it would be better to have a Assignment class, with three properties: Employee, Project and Role. Assignment being the "Employee-Project" association. Role describes an Assignment.

...do i always need to instantiate all three class to save data, for example, to DB or to retrive data from it ?
At any rate, to view a single employee instance and all of his/her assignments (each of which references both a Role and a Project) you would end up instantiating objects of each class. You could begin by loading an Employee object, and walk it's relationships to load the other objects as needed. Being able to lazy-load like this really depends on the sophistication of your data-access infrastructure. It some cases it may even be more efficient to make one large "chunky" query to get all the data needed to populate the employee, the assignments, the roles and projects.

...each one does save their own data or its better to have some DBClass that performe this kind of operation receiving the instance of the class to obtain the data to store( or load to) ?
Personally I prefer designs where classes do not save/load their own data, but are managed by another type of class that is responsible for persistence, such as a Repository.
 
I really appreciated your tips.
thank you.
 
<Personally I prefer designs where classes do not save/load their own data, but are managed by another type of class that is responsible for persistence, such as a Repository.

I second that preference.

A simple example of how role works in UML is to simply have an employee class with a self association. When there is an employee self association, each employee object plays a certain role in that association. One end of the association could be manager, and the other could be report. So, as you can see, the role describes, and also places limits on, the association.

Your questions are intelligent, and dragonwell's responses are very good. :)

Bob
 
It's always me :)
I've got another question : in my web application i need to show a list of objects (that could be the list of customer' s order) in a datagrid (i use asp.net and vb.net).

At the moment i have a method in my class that return a dataset (with all the data i need to show) which i bind to the grid.

If i had a list of object (order), which could be a good way to show the data keeping separate the view (grid) from the object itself ? I really don't know i could bind the data containde in each class instance.

thanks a lot.
 
Hi liuk - AFAIK, you can bind a list of objects (such as an ArrayList of Orders) to a GridView. The Order class must have public properties, each of which can be bound to columns in the grid the same way "columns" in a DataTable are. You might want to ask the question in the ASP.NET forum to get some more ideas...

Good luck with it - d

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top