08.09.2013 Views

Microsoft .NET: Architecting Applications for the Enterprise ... - BattleIT

Microsoft .NET: Architecting Applications for the Enterprise ... - BattleIT

Microsoft .NET: Architecting Applications for the Enterprise ... - BattleIT

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1 Architects and Architecture Today 11<br />

words, we were left to choose between ei<strong>the</strong>r of two unpleasant and painful options: writing<br />

our own Object/Relational mapping tool, or rearchitecting <strong>the</strong> middle tier to use a different<br />

(and much simpler) object model.<br />

With over 500 presenters in <strong>the</strong> Model View Presenter–based user interface directly consuming <strong>the</strong><br />

object model, having to make this decision was our worst nightmare. We knew it would require a<br />

huge amount of work on <strong>the</strong> middle tier, consuming both fi nancial resources and time. We lobbied<br />

<strong>for</strong> more time and successfully stretched <strong>the</strong> deadline. Then we built our own tailor-made data<br />

access layer <strong>for</strong> a domain model. (After you’ve read Chapter 6, “The Data Access Layer,” you’ll have<br />

a clear picture of what this all means.)<br />

Changing <strong>the</strong> Constructor’s Signature<br />

Don’t think that architecture is only about high-level decisions like those involving <strong>the</strong> design<br />

and implementation of parts of <strong>the</strong> middle tier. A requested change in <strong>the</strong> signature of a<br />

class constructor might get you in a fi ne mess, too.<br />

Imagine a scenario where you handle an Order class in your application’s object model.<br />

You don’t see any reason to justify <strong>the</strong> introduction of a factory <strong>for</strong> <strong>the</strong> Order class. It is a<br />

plain class and should be instantiated freely. So you scatter tons of new Order() instructions<br />

throughout your code. You don’t see, though, that Order has some logical dependency on,<br />

say, Customer.<br />

At some point, a request <strong>for</strong> change hits you—in <strong>the</strong> next release, an order will be created<br />

only in association with a customer. What can you do?<br />

If you only add a new constructor to <strong>the</strong> Order class that accepts a Customer object, you<br />

simply don’t meet <strong>the</strong> requirement, because <strong>the</strong> old constructor is still <strong>the</strong>re and only new<br />

code will follow <strong>the</strong> new pattern. If you drop or replace <strong>the</strong> old constructor, you have tons of<br />

new statements to fi x that are scattered throughout <strong>the</strong> entire code base.<br />

If only you had defi ned a factory <strong>for</strong> <strong>the</strong> Order class, you would have met <strong>the</strong> new requirement<br />

without <strong>the</strong> same pain. (By <strong>the</strong> way, domain-driven design methodology in fact suggests that<br />

you always use a factory <strong>for</strong> complex objects, such as aggregates.)<br />

Changing a Member’s Modifi ers<br />

When you design a class, you have to decide whe<strong>the</strong>r <strong>the</strong> class is public or internal and<br />

whe<strong>the</strong>r it is sealed or fur<strong>the</strong>r inheritable. And <strong>the</strong>n you decide whe<strong>the</strong>r methods are virtual<br />

or nonvirtual. Misusing <strong>the</strong> virtual and sealed modifi ers might take you along an ugly route.<br />

In general, when you use <strong>the</strong> sealed and virtual modifi ers you take on a not-so-small<br />

responsibility. In C#, by default each class is unsealed and each method on a class is nonvirtual.<br />

In Java, <strong>for</strong> example, things go differently <strong>for</strong> methods, which are all virtual by default.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!