“Assert Your Architecture”

Archive for April, 2008

Gumby and Architecture Rules

Written by MikeNereson

April 16th, 2008 at 7:52 pm

Posted in Uncategorized

With no comments

I keep my desk pretty minimal. Its pretty big, but the desktop is usually about 80% cleaned. The few things that I keep on it, other than a notebook and pen, are strategically placed and are designed to be conversation pieces. These items include a 2 gallon fish tank, with a Tiger Barb and plant, a poseable Gumby figure, and the latest addition, my Architecture Rules mug.

Now, the whole reason for these artifacts is to draw those two pass by my desk into a conversation – this offers a break in the day and a chance to get to know the people around me better.

The fish tank has been very popular, a lot of people can relate, or have a story, or just like to look at the fish. Gumby is a lot of fun. Its fun to see those who recognizes him, and those who don’t even know who Gumby is. I wanna order a couple of Blockhead figures to go along side him.

tigerbarb gumby mug

So the new one is the Architecture Rules mug that I designed and put up on CafePress. This has been a great way for me to get the people around me to talk about software, and specifically, about software architecture. I highly recommend that, if you use Architecture Rules, you pick up one of these mugs, put it on your desk, and start telling the world that you are actively mitigating architecture risk everyday. Hey, that’s something you, your coworkers, your customers, and your bosses can get behind.

Or, if you’re just looking for a new trinket to put on your desk, this mug is a lot cheaper and easier to maintain than a fish tank is. I have personally tested the mug and had successfully results with coffee, water, various teas, apple cider, and soup.

I have to be honest, the price that CafePress charges, of $13.99, is rather steep for a mug, but it is a tall mug with a capacity of 15 oz., and a buck or so goes to supporting Architecture Rules. Right now we are trying to raise funds to snag the domain architecturerules.com. Your purchase would go a long way in helping to secure that domain.

Here you can check out the upclose graphic. And if you can’t afford the mug, but you wanna help us get the domain or help us towards our current fund raising goal, you might be able to donate some change to the project.

So, what toys do you keep on your desk? What are the best conversation starters? Do you keep any trinkets branded with a tool or technology (my pen branded with Sun Java)?

Configuration Method Chaining

Written by MikeNereson

April 5th, 2008 at 7:54 pm

Posted in Uncategorized

Tagged with

With 2 comments

We have all seen method chaining, especially in configuration classes. Architecture Rules has a typical configuration class that currently does not support method chaining. Modifying the public API of a tool is hardly ever a good thing, but in this case, the change would still be backwards compatible. So we decided to investigate method chaining to determine if it should be introduced into this project or not.

What is it

Method chaining is a programming technique that is supported by many public interfaces. It allows the programmer to call one method from – directly after – another. For example, in Java:

Chained

Event event = new Event()
.setDescription("")
.setStartDate(start)
.setEndDate(end)
.setPrivate(true);

Unchained

Event event = new Event();
event.setDescription("");
event.setStartDate(start);
event.setEndDate(end);
event.setPrivate(true);

Where Is It

We see it very often with configuration classes. Such as the Hibernate Configuration class.

Configuration configuration = new Configuration()
.addClass(org.hibernate.Item.class)
.addClass(org.hibernate.Bid.class)
.setProperty("...dialect", "...MySQLInnoDBDialect")
.setProperty("...connection.datasource", "java:jdbc/test")
.setProperty("...order_updates", "true");

Its also used commonly with the Builder Pattern which simply creates a complex object using a number of steps.

How To

Designing method chaining into your code is pretty simple. In Java, rather than returning void by a method, such as a setter methods, return the entity being acted on. In Java, just return this;

class Event {

public Event setDescription(String description) {
this.description = description;
return this;
}

public Event create() {
... something complicated ...
return this;
}
}

So, to change an existing class to support method chaining in Java, change all your void methods, to return the entity type, and add return this; at the end of the method. It is pretty much a trivial task.

Concerns

There are some concerns with method chaining.

First, there are some out there who claim that chained methods are less readable. To them, it could be less readable. To other, more readable. Aligning the chained method calls on new lines can increase readability. A single line of method calls is certainly less readable than a single method call on a single line. Compare this to the examples of the Event class above.

Event event = new Event()
.setDescription("").setStartDate(start).setEndDate(end);

Second, mixing setter methods, with action methods can be very confusing to the readers. The problem comes with determining where these complex actions are taking place. Consider the following.

int eventId = new Event()
.setDescription("")
.setStartDate(start)
.setEndDate(end)
.setPrivate(true)
.insert()
.getId();

This is simple example. Most everyone understands that the insert() call is utilizing JDBC code to insert the Event in to a table, it probably sets the Event id property, and then the getId() returns that id. But what if the entity was more obscure, and the method name was not so clear? It might not be obvious where complex actions are taking place.

A third concern is when do you stop? and when don’t you stop? Let say an entity has a dozen or so setters that are commonly called as part of setting up a usable entity. Should you call all twelve? Two groups of six? Group by type? What is more readable? what is less readable? I don’t know. Depends on the author, the readers, the context. There are a lot of considerations.

When to Use

Method chaining is pretty commonly used when you are performing one complete action, building a complete entity, or performing an atomic task.

First, performing an action, such as a query. Here is an example from Hibernate.

List cats = session.createCriteria(Cat.class)
.add( Restrictions.like("name", "Fritz%") )
.add( Restrictions.between("age", minAge, maxAge) )
.list();

Second, building an entity. Sometimes this is done with a Builder class. With the example of the Event class, calling the setters could be chained. Calling insert() and getId() should not be chained.

Finally, an atomic task, such as validation, could reasonably be chained.

boolean valid = validator(value)
.isGreaterThan(0)
.isLessThan(10)
.isCurrancy();

Our Dilemma

We did this research to determine weather or not we should modify the Architecture Rules API to support method chaining in the Configuration and Rule classes.

rule_class_diagram configuration_class_diagram

Based on the research that we have done, since the Configuration and Rule classes have no actionable methods – just setters, they are essentially POJOS – we think it would be appropriate to chain those setters together, and that the resulting code would be no less readable or deceiving. We will include method chaining in 2.1.0.

Resources

http://martinfowler.com/dslwip/MethodChaining.html
http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html
http://kasparov.skife.org/blog/src/method_chaining.html

Architecture Rules Tutorial: 101

Written by MikeNereson

April 2nd, 2008 at 7:55 pm

Posted in Uncategorized

Tagged with

With 2 comments

How about a short, sweet introduction to Architecture Rules.

The Basics

  • Java.
  • Open Source.
  • Asserts architectural rules via unit tests.
  • Write your rules in XML.
  • Run your tests with junit, ant, or a continuous integration server.

Your Rules

You define your rules. For example, if you are developing n-tiered software, you probably have three layers. A DAO/integration layer, a service/business layer, and a presentation layer. Generally, the presentation layer does not interact directly with the integration layer. You could describe this rule as: com.company.project.presentation can not depend on the com.company.project.integration package. Its that easy.

The XML

The XML is in architecture-rules.xml and contains two section: <configuration> and <rules>. Configuration describes where your files are, and the rules simply describe a package, and one or more packages that it can not depend on.

<architecture>

<configuration>
<sources>
<source>targetclasses</source>
</sources>
</configuration>

<rules>

<rule id="dao">
<packages>
<package>
com.company.project.presentation
</package>
</packages>
<violations>
<violation>
com.company.project.integration
</violation>
</violations>
</rule>

</rules>

</architecture>

Thats it. You have a simple architecture-rules.xml.

The Unit Test

Just extend and run your tests.

public class SimpleArchitectureTest
extends AbstractArchitectureRulesConfigurationTest {

public String getConfigurationFileName() {
return "architecture-rules.xml";
}

public void testArchitecture() {
assertTrue(doTests());
}
}

More Features

In Architecture Rules 201, we’ll show you how to check for cycles (cyclic dependencies), write more action-packed rules, and describe how to handle source paths that can’t be found. We can also take a gander at the various Exceptions that can be thrown when rules are broken.

Go Get It

The next thing to do, is to go get it and try it out. Download, svn checkout, or put it in your pom.xml. Just add this repository:

<repository>
<id>architect-rules-repo</id>
<name>
architecture-rules hosted by code.google.com
</name>
<layout>default</layout>
<url>

http://architecturerules.googlecode.com/svn/maven2/

</url>
<releases>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</repository>

And this dependency:

<dependency>
<groupId>com.seventytwomiles</groupId>
<artifactId>architecture-rules</artifactId>
<version>2.0.3</version>
</dependency>

architecture rules xml architecture rules java

Modified Journalist Template   • Last Updated Sep-2010 © 2007 - 2010