简体   繁体   中英

Is class diagram required to change if DB contains Views on tables?

I'm a newbie in designing class diagrams. I follow DAO pattern in my design.

In my project, one user can add multiple contacts which can further be added to many groups.

Let's say 1 contact -> many groups. As per my requirement UserContact and UserGroup are classes which manage contacts and groups.

In DB contacts and groups are stored in two different tables.

There is one use case to retrieve all contacts along with their groups so that I need to retrieve all contacts first and then using contactID again I need to make a query to get its relative groups.To avoid this in DB, there is VIEW on these two tables. Now,with VIEW, I need to make one query to get user contacts and groups.

How can I add this method in DAO? How do I change my classes so that contacts and groups can be mapped to my objects? The following are the classes involved in this use case. 在此输入图像描述

If I understand the question correctly; you can create a GroupContactDAOImpl class that has only query (get) methods, eg getGroupContacts(groupId) or getAllGroupContacts() if you will. Since "group contacts" is not an entity that has its own table, UserContactDAOImpl.addContact and UserGroupDAOImpl.addGroup methods would be sufficient to add new records.

If I need to get groups associated with one contact, how can I map contact information and groups to one object?

If you want to map contact info and groups to one object, you can add a UserGroup property to UserContact class (like in Hibernate ). Then GroupContactDAOImpl will have a method like: UserContact getUserContactWithGroups(contactID) .

Similarly, if you'd like to get a group's info with all the contacts asscoiated; you can add a UserContact property to UserGroup class. GroupContactDAOImpl will have a method like: UserGroup getUserGroupWithContacts(groupID) .

There might be other methods to do the same thing; hope this helps.

As I need to insert and update a contact using UserContact, it should contain only contactID, email and name. I use UserContact and UserGroup as DTOs. Can I create a new class which holds UserContact and UserGroup that uses UserContactDAOImpl and is it a preferable way?

Yes, you can follow that way if you'd like to leave UserContact class as it is. You'd have a new DTO (eg UserContactWithGroups ) and return it from UserContactDAOImpl (eg . getContactWithGroups method.) You can also create a separate DAO class, but it doesn't matter much. It's best to adapt it to your own use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM