DPS907 notes – Tue Oct 28

Welcome back, after the study week break. Associations among entities.

.

Return and discuss Test 1 work

Planned for today.

.

Feedback on the recent survey

Planned for today.

.

Introduction to programming Assignment 1

We will discuss; your professor will give an overview. It will include security topics.

You will have about three weeks to work on it, and will be due during week 10.

Note: If you want to write a web service to support work in another course (e.g. Android app dev, third-year systems project), then it may be possible to use either or both Assignments 1 and 2 for that work. Please prepare a brief proposal and discuss with Peter McIntyre.

.

Associations among entity classes

In your ASP.NET MVC web apps course, you probably had a reasonable treatment of associations.

Today, we’ll discuss some (see the following), and study a code example (named “Associations”). The problem domain that the code example covers is a workplace. A workplace has employees. Employees have job duties, usually one or more. However a job duty is often performed by many employees. An employee lives somewhere, and works somewhere – these are addresses.

.

One to many

Already done, a common situation. In this course, we have worked with easy-to-understand “one to many” associations, so we will not repeat this coverage in today’s code example. You already know how to write the design model classes.

.

One to one

An Employee has one “home” Address, and one “work” Address.

From the Address perspective, it is linked to an Employee.

When writing the entity classes, you must know the following, and be aware of some rules:

  • In this association, you MUST select a “principal” end of the association, and a “dependent” end
  • We will decide that the Employee is the principal end
  • This means that an Employee object can OPTIONALLY have an associated Address object – an Employee object can be created, without having an already-existing Address object
  • However, as a result, the Address is the dependent end
  • This means that an Address object MUST have an associated Employee object – an Address object cannot exist without an associated Employee object, so when you create an Address object, you must know about the associated Employee object

How do you code this?

It’s simple. Two tasks:

In the dependent class, add the [Required] data annotation attribute to the navigation property that points to the principal class. Here, in other words, add [Required] to the “Employee” property.

    [Required]
    public Employee Employee { get; set; }

Add another property, a nullable int. Its name is a composite name – EmployeeId.

    public int? EmployeeId { get; set; }

How do you use this?

  1. Make sure that you have a reference to a principal object (e.g. an Employee)
  2. When creating a dependent object (e.g. an Address), set its “Employee” property to the principal object
  3. Set the EmployeeId property to the principal object’s identifier

.

Self-referencing one to one, to the same entity

An Employee has a supervisor/manager, who is also an Employee.

When writing the entity class, you must know the following, and be aware of some rules:

  • Two properties are required
  • One property is a nullable int – int? – it will hold the identifier of the associated object
  • The other property is the class data type

.

Self-referencing one to many, to the same entity

A supervisor/manager Employee has a collection of Employees supervised/managed.

This is actually easy, as you may speculate. Simply add a collection property; its data type is the class data type.

.

Many to many 

An Employee can have several ‘job duties’ (e.g. teacher, researcher, coordinator), and a ‘job duty’ is performed by many Employees (e.g. School of ICT coordinators include Peter McIntyre, Ian Tipson, Scott Apted, etc.).

Maybe surprisingly, this is also actually easy. Simply add collection properties to each class, similar to a ‘one to many’ association.

The class diagram below is from this week’s code example, “Associations”. Click to open it in its own tab/window.

ClassDiagramOfOtherKindsOfAssociations

.

When you are writing entity classes, are associations required among all classes?

No.

It is not necessary to ‘connect’ every class to another.

.

Learn more about this topic theme

In this web services programming course, and in the prerequisite web apps programming course, we use a learning strategy that enables the student to focus on the important and most-used associations among entities. The Entity Framework, and its technologies, offers much more than we have time to cover in this course.

Therefore, as you read/skim the linked resources below, use your critical thinking skills to put a topic into the proper context and usage pattern. In other words, don’t be literal in how you use and apply the information.

The biggest difference-maker for us is that we are writing web services and web apps. As a result, operations on data are atomic – a ‘read’ is done as a database server connection open, a read, and an immediate close. Similarly, a ‘write’ is done as an open, a write, and an immediate close. Many of the discussions in the linked documents (below) are for runtime environments where a database connection is persisted as a longer-lifetime object. So, if you have questions about syntax and differences with what you’ve learned so far, ask your professor.

Microsoft – Entity Framework Relationships & Navigation Properties

Microsoft – Entity Framework Code First Conventions, Relationship Convention section

Morteza Manavi – Associations in EF Code First – a multi-part series; parts 1, 2, and 3 are useful

MSDN Entity Framework topic page, lots of good stuff here to pick-and-choose from

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: