Looking for Java and ASP.NET developers

I have a few contract openings for Java and ASP.NET developers.

For more updates and announcements, follow me on Twitter - @alehmatus.

VS 2010 SP1 available for download

Microsoft released Service Pack 1 for Visual Studio 2010. Click here to download the installer.

For more news and announcements, follow me on Twitter: http://twitter.com/alehmatus.

June Architect: Domain Model Structure - Part 5: Loading Value Objects into Memory

In my last article, we talked about Value Objects, domain model elements with no conceptual identity. We said that Value Objects represent descriptive aspects of the domain and are fully defined by their state. This month, we will review the options for loading Value Objects into memory.

Potluck Principle of getting Value Objects

The Potluck Principle states that if you need to obtain a Value Object in your code, you have the following three options:

  1. You may be able to create the Value Object yourself
  2. You may be able to ask another object to give the Value Object to you
  3. You may be able to reconstitute the Value Object from a previously stored state

Option 1: Create Value Objects yourself

You can create a Value Object by calling it's constructor or factory. This is a very simple method but there is a catch: since Value Object are immutable, you need to know all their required attributes at the moment of construction.

Example: Creating an instance of the Address class based on a submitted HTML form.

Option 2: Ask other objects to give Value Objects to you

If you need a Value Object in your code, who could you ask for it? Since Value Objects are often descriptors, you may be able to query objects their describe (usually Entities or other Value Objects).

Example: asking Person Entity for the Person's Age or asking Email Address Value Object for it's Domain.

Another popular way is to obtain a Value Object as a result of an operation call closed under the Value Object interface.

Example: (new Money($10)).Multiply(2) results in new Money($20) or Color.Blue.Mix(Color.Yellow) returns Color.Green.

Option 3: Recreating a Value Object from a previously stored state

As discussed in my previous posts, Value Objects are tracked only as part of Entities they describe, in which case they are loaded into memory using Option 2. Value Objects are not recreated from previously stored states.

However, there is a situation when Value Objects need to be created independently. For example, it is a fairly common requirement to display the list of Value Objects in a drop-down list.

Example: User submits a new Purchase Order. As part of the entry screen, the user chooses Billing and Shipping Address for the new Purchase Order. All 50 States are selectable on the Billing Address form, but only 48 States are selectable on the Shipping Address form.

How shall we populate drop-down lists of 48 and 50 States for the New Purchase Order screen?

I have heard software designers making arguments towards promoting State to an Entity. Then, State Repository would be responsible for retrieving the right lists of States to display in each situation. I disagree with such design. It introduces a responsibility of managing States into the domain model, which can be easily avoided if we keep States as Value Objects.

Another important point against introducing the IStateRepository interface is that business rules of whether to display a particular State in a drop-down or not do not belong to the State class. For example, the company could introduce a new Product, which could be shipped to all 50 States.

My preference is to assign the responsibility of knowing about Value Object lists to aggregate repositories. In our example, IPurchaseOrderRepository would have FindStatesForShipping and FindStatesForBilling methods.

Happy coding! To be continued...

.NET consultant available

Let me know if you are looking for a .NET consultant. A friend of mine is a recent winner of U.S. Green Card Lottery and is available on short notice.

I will support him on your project and offer you an attractive rate. Let me know. Thank you.

Silverlight Event on Tue, April 20, 2010

Who: Adam Grocholski
What: Windows Phone 7 and Silverlight - Don't Make Any Assumptions
When: Tuesday, April 20, 12:00 - 1:30 PM
Where: Microsoft Office at 8300 Norman Center Dr., Suite 950, Bloomington, MN 55437
Cost: Free

Registration is required. Click here to register.

March Architect: Domain Model Structure - Part 4: Value Objects

In the Part 2 and Part 3 of the Domain Model Structure series, we continued our discussion around how to organize classes inside your domain model. We defined Entities, looked at their relationships, and reviewed options for loading them into memory. This month, we will take a look at Value Objects.

Discovering Value Objects

Value Objects are domain model elements with no conceptual identity. They represent descriptive aspects of the domain and are fully defined by their state. Their behavior depends on what they are, not who they are. If they need to be tracked, they should be tracked with the elements they describe. Thus, they do not need an identity of their own.

We prefer to keep Value Objects immutable, i.e. changeable by full replacement only. Immutable Value Objects are safe to share (which increases performance), and easy to understand (which allows you to build complex behaviors). Let me give you an example based on a classical OO problem:

Assume you have two buckets of paint: b1 and b2. The first bucket b1 contains 1 gallon of yellow paint. The second bucket b2 contains 1 gallon of blue paint. Without looking into the code, could you answer the following questions about the results of the operation b1.Add(b2)?:

  1. What will be returned by the Add method? Is it 2 gallons of green paint or nothing?
  2. What will be in the the first bucket b1? Is it 2 gallons of green paint or 1 gallon of yellow paint?
  3. What will be in the second bucket b2? Is it 1 gallon of blue paint or nothing?

If you notice, we do not have a requirement to track buckets of paint. Thus, I will assume that Bucket is a Value Object and should be immutable. Since both buckets are immutable, they will contain their original values of 1 gallon of paint each and the result of the Add operation will be 2 gallons of green. With no convention in place, it would be impossible to answer these questions without checking the code or better yet unit tests first.

You domain has many Value Objects, a lot more than Entities. If you are having troubles seeing them in your system, take a closer look at the Entities. How are they being described? What are their attributes and properties? How are they being searched for or ordered by? The chances are you will be talking about Value Objects.

Examples: Address, DayPoint, Money, Range, SSN, etc.

Domain Model Structure

Value Objects along with Entities are main elements of the domain model. Many of them are small simple classes that will be widely used in your application. Place them together in a sub-folder under the Domain Model root. We name this folder "Capability":

Domain Model Structure - Capability folder

You are likely to have a number of very complex Value Objects as well. I will try to discuss some of them in my future articles.

Summary and Additional Tips

  1. Never define an identity for a Value Object.
  2. Consider overriding equality operators to match Value Objects using object attributes rather than object references.
  3. Keep Value Objects immutable.
  4. Place Value Objects used across many applications into a dedicated Domain Kernel library.
  5. If you are interested in creating a simple base Value Object class, you can start with the one listed below:

    Domain Model Structure - Value Object Template

Happy coding! To be continued...

Videos from MIX10 conference available to download

Check out these session recordings from the MIX10 conference in Las Vegas (March 15-17, 2010): http://live.visitmix.com/videos/. Enjoy!

Visual Studio 2010 and Silverlight 4 launch date announced

Microsoft announced the launch of both Visual Studio 2010 and Silverlight 4 on April 12, 2010 at the ASP.NET and Silverlight conference and expo in Las Vegas. Click here for the conference details.

Happy coding!

January Architect: Domain Model Structure - Part 3: Repositories

Last time, we continued a discussion around how to organize classes inside your domain model. We defined important domain model elements: Entities, aka Reference Objects, and looked at their relationships. This month, we will review the options for loading Entities into memory.

Potluck Principle of Getting Objects You Want

The Potluck Principle states that if you need to obtain a reference to an Entity in your code, you have the following three options:

  1. You may be able to create the Entity yourself
  2. You may be able to ask another object to give the Entity to you
  3. You may be able to reconstitute the Entity from a previously stored state

Choosing the right option

The option #1 is usually implemented via a constructor or a factory. It works well when you need to create a new entity object, not yet tracked by the application.

As we discussed last month, Entities span through long periods of time and run across multiple systems. Unless you are writing a data entry application, you are going to work with existing Entities a lot more often than with new ones. When you need to create an existing Entity, use the option #2 or the option #3.

Let us take a look at the last month's example: User logs in into the system to check the status of their loan applications. Assuming we have a Person loaded, how shall we obtain their Applications?

Entity Relationship

To enable the option #2, design the Person class with a method or property that returns the Person's Applications. I generally prefer to pass a qualifier, such as loan type or date, that would transform one-to-many relationship between Person and Application into one-to-one with a qualifier, but this is a topic for another conversation. For now, let us assume that the Person class defines the Applications property that returns a list of Applications. This requires you to either get the Applications when the Person object is loaded into memory or implement a lazy load mechanism to defer loading Applications until the Applications property is called.

To enable the option #3, design the ApplicationRepository class with a method that returns a list of Applications for a given Person: "IList FindByPerson(Person person)".

Domain Model Structure

Repositories in the Domain Model

My preference is to place repository interfaces in the model and move repository implementation into a separate Persistence library. Keeping repository interfaces in the model helps clients understand how entities are supposed to be loaded into memory. Having repository implementation separate from the model keeps the model free from infrastructural detail.

Happy coding! To be continued...

November Architect: Domain Model Structure - Part 2: Entities

Last month, we started a discussion around how to organize classes inside your domain model in a clear, easy-to-understand, and easy-to-maintain way. We reviewed our objectives and discussed why they are important. This month, we will start revealing a story of a business domain for the purposes of developing a software application. We will look into what is being managed in the domain day to day: Entities.

Discovering Entities

There are a number of technical definitions of an Entity that are popular in the industry. For example, here is a definition from Eric Evans' book Domain Driven Design, p. 89: An object that is not fundamentally defined by its attributes, but rather by a thread of continuity and identity.

The way I prefer to think about Entities is what we manage in our domain day after day. Entities span through long periods of time and run across multiple systems. They often have lives well beyond our applications and must be effectively tracked. As a general rule, assume that mistakes in Entity identification and tracking are expensive to notice and repair.

To discover Entities in your domain, talk to your functional people and end users. Ask them to walk you through scenarios the new system should implement. How are your users going to use your application? What is important to them and why? What do they really care about? What is important to your company and how does it need to be reported? The chances are you will be talking about what needs to be tracked and how, thus you will be talking about Entities.

Examples: Person, Account, Loan, Application, Payment.

Entity Relationships

What are the relationships between your Entities? Before getting into technical details, create a simple analysis diagram that shows the Entities you have discovered and their relationships. Lets consider a simple example:

Scenario 1: User logs in into the system to check the status of their applications and loans.

Scenario 2: User calls a Customer Service Representative (CSR) to reset their password.

Entity Relationship

Your research and conversations about the domain model revealed 4 Entities: Account, Person, Application, and Loan. Let us walk through the first scenario:

  1. User logs in into the system. The system needs to retrieve the user's security Account and verify their username, password, and any additional validation factors.
  2. Upon successful login, the system retrieves a previously stored Person record based on Account information. Thus, you see an arrow from Account to Person. You learned that each Account has a corresponding Person record, but not all People have created an online Account. That explains the numerical values on the diagram.
  3. Based on Person, the system retrieves their Application and Loan information. Thus, the arrows are shown from Person to Application and Loan. Person is created on the system with their first Application, but not all Applications are approved to become a Loan. Notice that there is no arrow between Application and Loan - we cannot determine its direction based on our current scenarios.
Now, let us take a look at the second scenario:
  1. User cannot verify the security information during login and calls CSR to reset their password. CSR asks them a series of Personal questions and retrieves their Person record.
  2. CSR retrieves the user's Account record for additional verification and resetting the password. Thus, you see an arrow from Person to Account.
Do not worry about having a circular dependency between Account and Person on the Analysis diagram. In one of our next articles, we will show several implementation options for the relationships. We will not have a circular dependency in the final implementation.

Domain Model Structure

Account, Person, Application, and Loan are our first classes in the Domain Model. For now, place them together in a sub-folder under the Domain Model root. We name this folder "Operation":

Domain Model Structure - Operation folder

Summary and Additional Tips

  1. Entity is an object that is not fundamentally defined by its attributes, but rather by a thread of continuity and identity.
  2. Entities span through time and space and are being tracked and managed in the day-to-day operations.
  3. Discover Entities by talking to your functional and end users. Create simple Analysis diagrams as a result of these conversations.
  4. Entity's primary responsibility is to maintain its continuity. Keep it simple by leaving complex behaviors out of its class definition.
  5. Managing life-cycle of Entities is a complex and risky job. Try to keep the number of Entities in the system down. Too many Entities will defuse your model and create no value.
  6. Compare Entities of the same type by their identity regardless of their form and history. Make identity type a Value Object.
  7. If you are interested in creating a simple base Entity class, you can start with the one listed below:

    ///<summary>Base Entity Class </summary>
    public class Entity <T>
    {
        public T ID { get; protected set ; }
        public static T NewID { get { return default (T); } }

        ///<summary>Returns true if the object has not being stored in the system. </summary>
        public bool IsNew { get { return Equals(NewID, ID); } }
        public Entity(T id) { ID = id; }
    }

Happy coding! To be continued...

Welcome to ModelBlog

Thank you for visiting ModelBlog. We hope the time you spend with us will be both entertaining and worth your while. Have fun!

Authors

Search

Archive

Tags