Sunday, July 30, 2006
Thinking about view objects with DDD
First of all, I admit that the use of a Builder pattern doesn’t strike me as the best overall solution to all the view objects problems, but I liked the separation of concerns that it conveys. I would tend to keep the code away from the Façade because for me, the Façade is merely an entry point and a coordinator.
Maybe we could solve this problem as with any other domain related problem. I think we are guilty of focusing too hard on technical issues and wanting to solve the problem with a silver bullet instead of realizing that this is simply a different domain in which we, as developers, are the domain experts. Perhaps we have should try to face this issue having in mind that our existing model (Employees and Office) and our screen requirements are the data we have about this domain. In this context, each use-case (or screen) has the information necessary to help us build a model: a model composed of view objects and some sort of repositories (Sergio’s Builder?).
In several projects I was involved in it always surprised me how little one screen reuses view object code from the next (or most code except for UI components). It was like each developer coding a screen was living in complete isolation from the rest of the team. Perhaps giving the problem a more DDD approach we could start having a more supple design to this specific problem. At this stage, the use of a Builder pattern (or other pattern for that matter) could start to arise as deeper insight into our model.
Summing things up, I don’t think we will ever reach a consensus on what is the “best way” of solving this issue. Some UI domain will be better solved with a particular model subset, while other will not. Sergio’s solution is an already digested solution to his domain but it isn’t fitting Jesse’s needs any more than a generic Cargo Shipping model (like the one in the book) would fit a real cargo company software. Nevertheless I will keep Sergio’s proposal (as well as others) as Patterns and/or starting points to address my future UI domain modeling.
Tuesday, July 11, 2006
Injecting into domain objects
I've sometimes participate in DDD forum and the some of the most recurring question is whether using Repositories (aka DAO but from a model POV) inside Entites is a good thing. My belief is that most of the time it is a necessary thing if you want to avoid using services for everything but the simplest of operations inside your model. So if your are using an ORM and Spring (defacto web app architecture nowadays) the problem arises on how to inject the repository implementation (the DAO implementation) inside the instantiated objects hibernate or another ORM is doing the instantiation instead of Spring. I will not detail on how to do this but just point to 2 blog entries that explain this better than I do.
If you are using the newest Spring 2.0 this issue is actually addressed and is explained here and here. There are some things to note if you're using hibernate.
If you are using an older version on Spring you have other options which are discussed here
Hope it helps someone out there. I know it will help me again to have the information readily available here :)
Wednesday, July 05, 2006
Custom data exception with Spring + hibernate
Out first reaction was to dig into hibernate and look for mechanisms to intercept exception but unfortunately neither hibernate interceptors or the new event system has support for a onException or onError pointcut. We quickly turned to spring and realized this was actually a better idea since spring already does it own convention of multiple exceptions into its coherent exception hierarchy.
What we ended up doing was this:
1) Create your own exception. Lets call it MyCustomException (what and original name)
2) Copy sql-error-codes.xml from Spring source to your classpath.
3) Modify the copied sql-error-codes.xml and add your own entry in the database dialect you are using (MS-SQL, Oracle, etc). We added the 20999 code to Oracle so our bean definition looked something like this:
<bean id="Oracle" class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="badSqlGrammarCodes"><value>900,903,904,917,936,942,17006</value></property>
<property name="dataIntegrityViolationCodes"><value>1400,1722,2291</value></property>
<property name="cannotAcquireLockCodes"><value>54</value></property>
<property name="customTranslations">
<list>
<bean class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
<property name="errorCodes"><value>20999</value></property>
<property name="exceptionClass"><value>MyCustom</value></property>
</bean>
</list>
</property>
</bean>
Note the use of CustomSQLErrorCodesTranslation to set a special bean we will look at later. Later you input the errorCodes that you should trap and the exception it should throw. What puzzeles is why I need to copy the entire sql-error-codes.xml. Maybe someone can check if this actually is needed or not.
4) In you hibernate respository or DAO make:
a) If you make use of Spring hibernateTempalate and callback Spring will automatically wrap you exceptions. It will look something like this (in this case I’m not inheriting from HibernateDAOSupport from Spring that should make this could simpler)
public Collection loadProductsByCategory(final String category) throws DataAccessException {
HibernateTemplate ht = new HibernateTemplate(this.sessionFactory);
return (Collection) ht.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery("from test.Product product where product.category=?");
query.setString(0, category);
return query.list();
}
});
}
b) If you don’t use callback you’ll have to tell Spring to do the exception convertion explicitly like:
public Collection loadProductsByCategory(String category) throws DataAccessException, MyException {
Session session = getSession(getSessionFactory(), false);
try {
List result = session.find("from test.Product product where product.category=?",category, Hibernate.STRING);
if (result == null) {
throw new MyException("invalid search result");
}
return result;
} catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
As you can see, there is a call to convertHibernateAccessException that converts the hibernate exception to the defined Spring hierarchy.
Now you can have typed exceptions thrown from your repositories instead of nasty generic Hibernate or JDBC exceptions. Have fun!
Monday, July 03, 2006
Cell phone carriers opening their gardens
A recent news has shown that at least this is changing an several 2 startups Q121 and Rabble are already taking advantage of this new scenario to create a better improve social networks using mobile phones. In an interesting analogy (similar to a previous link of mine) the article goes on saying that 'This "walled garden" concept harkens back to the closed dial-up computer networks of yesteryear, such as AOL, Compuserve, and Prodigy'
This is a great step forward for the mobile phone to become the next computing platform.
Sunday, June 11, 2006
1st class - distance learning
For the course I was given a DVD with recorded lectures that I have to watch on certain days with addition to reading some chapters from the "A Discipline for Software Engineering" from Watts Humphrey. Every week we also get into a java based virtual classroom where we basically chat and ask questions to our instructor. So far there has been nothing really new except some statistics. Hope it gets more useful down the road.
Wednesday, May 17, 2006
PSP distance learning
But is seems CMU is going to give me a hand and throw a distance course my way that is to start on may 29th: Personal Software Process. I already received a DVD with distance lectures (videos) and bought the book; what I peeked so far looked interesting.
Thursday, May 04, 2006
Sending papers in
What a busy month this was!!
After receiving CMU acceptance letter I had to go and get funding, plan a "what if" scenery with my personal life (read girlfriend) and how to make a clean exist out of my current employment, so I had little time to write. Also, MSE program director David Garlan was visiting
After much discussion with other international students I got to realize that financially speaking, one has to get enough money to start college and plan on getting funding along the way in order to finish it, instead of coping with the entire cost of the program at the beginning. After the first 8 months, most former students seem to agree that a student loan, even without a
To close this posting, I received today an email about some distance course preparation before attending campus that is supposed to start at the en of May, so I'll keep writing on about that in the future. Right now I'm really scared on how all this will turn out hoping I'm making the right choice. Of course, I'm aslo very very excited to start...what a contradiction right?
Tuesday, April 04, 2006
Java from Scratch - part II
A few week back I posted about how long it was since I've built anything with java from scratch. What I want to do in the long run is to produce a DDD oriented projects with hibernate+spring. Bear in mind the last time I used java was with EJBs and JBuilder 8.
So after reading about the myriad of different options that are in the Java world I decided not to try to configure everything for myself and instead rely on a "template" project that would help me. I went with AppFuse to kick start the project since it comes with a mixture of framework decisions you can dream of, from hibernate, IBATIS, Spring and others to JSP, Tapestry and JSF for presentation with included security, transaction support and even
So here are the dummy proof steps I followed:
Getting started: Java, Ant, Tomcat and MySQL
The first thing is to download and install the JDK 5.0 in your local computer. For windows there is a simple installer that you can get from Sun's Java site. Try and find something like "Download JDK 5.0 Update XX". After that you should add the JAVA_HOME environment to you system and JAVA_HOME\bin to your path. To do so in XP press Win + Brake, go to Advanced -> Environment Variables and add a new "System Variable" and enter JAVA_HOME and the directory of your java. In my case it looks like this: "C:\Program Files\java\jdk1.5.0_06". Afterwards, find the PATH variables and at add at the end a %JAVA_HOME%\bin entry.
Another thing to install is Tomcat and Ant which I will not get into to much detail. For Tomcat you have the option of a windows installer (I grabbed my 5.5 version here) and for ant you should simply download the latest binaries and uncompress them to the desired folder. After that you should repeat the same procedure as with the JAVA_HOME environment and PATH addition but with CATALINA_HOME and ANT_HOME, the former pointing the Tomcat home installation and latter to ant's.
Finally you need to download MySQL database. Although version 5 is available I downloaded version 4.1 since there seems to be more support so far. A good idea is to also download the Administrator and Query Browser.
Eclipse
Then we need a IDE for easier code writing, in this case eclipse. Eclipse is actually more than just an IDE, but a platform in itself. This is why you will not find a simple download for developing in Java. If you head to the download section you will see smacked in the middle of the screen the more "usual" download: the latest stable download for the eclipse SDK. This download includes the tools necessary to build plugins and expand eclipse, that since are written in Java, serves as an excellent Java IDE (actually the java tools are called Java development tools (JDT)).
Fortunate for me there was a new production release of what is known as the Web Tools Platform (WTP) a few weeks ago. This project aims helping the creation of J2EE and web projects and is bundled with several subproject and goodies that might prove useful in the future. So try and grab the latest version of the WTP and uncompress it to a folder and lunch eclipse.exe.
After you start eclipse, the first thing you should do is to set add the JDK as an installed JRE environment. For this, in eclipse, go to Windows -> Preferences. There, find the Java / Installed JRE entry in the left side tree and click on "Add". In the dialog presented enter any name you feel comfortable with (something like JDKJRE1.5.xx) and point to the same directory as the JAVA_HOME environment set earlier. Why eclipse is not smart enough to add this on its own? My guess is because it searches the Windows registry.
Most of the steps above are similar to what you can find here, but I found so much information on the page confusing for some reason.
AppFuse
As I said before, AppFuse is nice "template" project that help you get started on a new project. It uses Ant to its work, and what you end up having is a project with the name you assign to it in its own directory structure, ready to be built and deployed to a server. The standard tools AppFuse uses is ant and java, Tomcat as a web container and MySQL as its database. That is why with set up those things at the beginning. Also, the default architecture consists of hibernate+spring+struts with other assorted goodies. There are instructions at AppFuse wiki on how to configure these things differently but I haven't tried them yet. I’ll probably delve into this later since I want to use Tapestry instead of Struts.
Obviously the first thing to do is download the latest version and extract it to any directory. Then, with you command prompt pointing in the AppFuse directory type:
ant new
If you set everything alright it will be ask 1) the name of the application, 2) the database name and 3) the name of the packages. This will generate a directory with your project name that is ready to be built, tested and deployed.
Since AppFuse knows nothing about how do you wish to connect to your database you should supply these values by modifying build.properties file at the root of the new application directory. There, just unremark the lines with
database.admin.password=myrootpassword
After that, navigate to the new project directory and type
ant setup
This should build the DB, configure tomcat and deploy the resulting application. After this point you should be able to go to localhost:8080/
Since this blog entry is quite long so far, I will cut it here to return later and explore how AppFuse automates other development tasks and how to use Eclipse to edit the files.
Wednesday, March 29, 2006
Carnegie Mellon financing
While investigating for loans I found and interesting site called MyRichUnlce that lends money, but instead of repaying the loan the traditional way with a interest fee, you are bound to pay 1% of your salary for an X amount of time after graduation. Seems really interesting idea since these guys know that once you get your higher education your probably have better paying job opportunities.
The next step is to complete and submit the required forms, that include standard user forms and medical records and then wait for the I20 which will allow me to get my student Visa.
Sunday, March 19, 2006
Carnegie Mellon acceptance!!
Wow!! Today I received an acceptance letter from Carnegie Mellon Master in Software Engineering program. I'm ecstatic and really happy for the news. I remember taking the GRE 2 years ago in case I one day decided to got to a US school and also sending the application forms on december when I actually thought my chances where slim. I believe it was my work at my current company with CMM practices and belonging to the SEPG that helped. Anyways...it's going to be cool.
The only thing left now is to find financial support to go there since the Argentinean economic collapse 2 years ago has left the country (and my savings) with a devaluated 350%. A good salary development salary here is about U$S 18.000 a year. You do the math...So here I am analyzing my possibilities with a strong conviction to make it somehow (donations and loans accepted :) ).
And I haven't got into the difficulty that’s going to be surviving the program. I promise I'll keep posting how this process advances.
Friday, March 10, 2006
Web 2.0 rants
After the news spread about google buying writleyit seems we will not need office anymore. Google seems to be THE web company since they understand what the internet is all about. Anoter site that cought my atenttion today was thumbstacks, an online PowerPoint style program that looks promising.
I just can't wait to see what's next!
Tuesday, March 07, 2006
About DTO, ViewObjects and friends
A few weeks ago at the DDD forum a user (plind69) asked a question about how to send business objects to the presentation layer but have them show only a subset of data based on security settings. Needless to say, this is a much debated topic as witnessed by the large response and variety of opinions. Most solutions where based on the idea of DTOs, weather they are static or dynamic with proxies.
Users that had used DTO in a solution know how painful they can be: for starters DTOs are hard to maintain since every time a domain class changes so must the DTO (and also their assemblers/desassemblers). Then, there is this strange code-smell feelilng that you're duplicating code all over just for the presentation to use. To avoid this some suggest using dynamic DTOs (a nice dynamic DTO framework can be found here).
Although somewhat similar to dynamic DTO, some suggested using dynamic proxies to restrict the view. The solution looks promising for the particular issue of restrict certain fields, but I still believe that other issues should be accounted that are not possible to address with this particular implementation, like the fact that sometimes there are performance implications that make it undesirable to expose domain objects directly. Per example, imagine a fictional complicated purchase algorithm. If the result is calculated on the server, there is no need to send the actual value but a cached value. DTOs are perfect for the task since all its members are cached values. A DTO is basically only a cached set of data from the domain.
So, is there a solution? I don’t know J. First of all, let me tell you that I don't believe there is a silver bullet (at least not that I found) to solve this issues. I think that the mechanics of how to present domain classes to the user (or WS for that matter) should be considered on a use case-by-use case basis.
Maybe using a mix of adapter and proxy pattern could make life easier? A domain object could be proxified to return (or not according to security permissions) the information from the domain object itself, but if needed it could return a cached value. This could be similar to how you use a mock object but instead of creating all mock interfaces by default you pass in the actual domain object. So assuming you could do something like this:
viewObject.cacheValue(“complexProperty”).returnValue(complexValue);
viewObject.restrictView(“restrictedProperty”).throwException(IllegalAccessException.Class);
Saturday, February 25, 2006
Location based communities
One of my latest interests has been mobile phone technologies. A few years back I remember looking down on these devices as limited and awkward computer wannabes. Today, the mobile phone market looks really promising and similar to the early years of the PC. The increasing use of the phone as a way to access the internet will bring about an improvement on how people use the web today.
Web 2.0, more about technology, is about giving the power to the users: user created content and photos uploaded and tagged are few examples of how people are creating and communities and sharing interests around particular issues. What is missing from most of the content today is location, some that can be improved by mobility.
In a couple of days I will attend a U2 concert in
Imagine if I as soon as I upload the picture I'm automatically assigning it a geolocation based on my current position. A few weeks later I could search for other people that took a snapshot of Bono the same day and share live again the experience from a different angle. I could share my experience in my blog and have it linked to other blog users that where nearby the same day and added blog entries of their own. Creating a mashup with this information together with sites like GoogleMaps will be trivial as I already have the coordinates for the information that was uploaded. If you have used google local or even map quest you’ll realize the potential for queries in the vicinity of you current position as you surf.
Finally, locating a mobile phone will become more commonplace with the new 911E proposal that requires that a cell phone could be located in case of need with a mandatory GPS in each phone. The industry is also showing increasing support for location based services with API for geolocation using their network.
Friday, February 17, 2006
DDD Specification pattern
I just read a nice blog entry from Segio Bossa giving an example of the DDD Specification pattern. I wanted to keep his article in here so I could reference it in the future.
Following the specification discussion, in his book, Eric explores the Specification pattern more in depth and analyze the possibilities of doing composite Specifications. Then I remembered something I saw a while back on the Apache Commons project. The org.apache.commons.collections package (I wonder why is it there?) has a Predicate interface that fit's nicely with these concepts. Instead of a .isSatifiedBy() method you are given a evaluate(object) method.
I haven't really explored the idea in code by you could inherit a specifications interface and use the supplied AndPredicate, NotPredicate, etc to make composite Specifications.
Monday, February 13, 2006
Starting a J2EE project from scratch
My past 2 project were in .NET. When the projects begin I usually sit down and write some reference code myself and then let the developers continue. Lately I felt technology was getting by me since and I missed coding in Java. Wow how things have changed since Jbuilder 9!
I want to start a new project from scracth and know how I want the architecture to be based on my previous knowledge and reading countless forums and mails. I want the persistence to be based on hibernate, I need Spring for IoC and AOP for things like Transaction and logging. For the front end I'm still not sure. The application server is going to be JBoss since I need JMS and other goodies.
After much struggeling I realize what M$ has in it favor: it is stupidily easy to set up a project. I am overwhlmed by the amount of option and not sure how to even start this project in Eclipse. I even downloaded AppFuse in order help me jumpstart the project. I'll keep posting on how I advance as I go along.
Monday, February 06, 2006
POJO in Action Review
As a software architect I found most of the book similar to previous projects I've tried to build since I read the DDD book. The advantage to have this book at our company is that when a new developer gets on my team I can point him straight to a book that is easy to read and understand.
Thank you Chris!
Friday, February 03, 2006
Cell vs Laptop debate reviewed
Living in a what is considered a developing nation (Argentina) I can see all around me the pervasivness of the cell phone in peoples lives, realying on it not only to speak, but also to chat in chatrooms and take picture and upload them in their personal fotoblog (Argentina and Brasil have one of the highest clients in www.fotolog.net). Thus I strongly believe that the cheap laptop is no contender to the cell phone.
But still, as in the past, the cell phone makers and more importantly the telco companies are blind to see the potential for their product. Their industry major concern is that these new technologies will eat into their fat voice business model.
It was not long ago that telcos where afraid to interconnect their SMS systems and exploit it in fearing that people will speak less on their phone. Once they did, they realize the inmense potential of this new market. In fact, in Argentina this milestone happened about 20 months ago and cell phone sales has skyrocketed since. No the telcos are crazy offering chat, ringtones and stuff...Now comes MMS: it is still too expensive to be useful and interconetion problem exists. And I haven't even touched the subject about data transfer that still is in it infancy and incredibly expensive because they are afraid that once you'll be able to install Skype on your phone the game's over.
And then there is the problem with the cell phone itself: incompatible OS and programming languages (Java vs. BREW vs Java Flavors..) that ressembles the PC industry in the 70's.
Unfortunately until this problems are not solved I don't think the cell phone stand a chance in becoming the next PC revolution.