Archive for September, 2009

Domain Driven Design series: Layering in DDD

 

Layering in software architecture

Creating programs that can handle very complex tasks calls for separation of concerns, allowing concentration on different parts of the design in isolation. At the same time, the intricate interactions within the system must be maintained in spite of the separation.

One of the function of layering in the software architecture is to make the relationship between layers to become as loose coupling as possible. In the three layer architecture, the user interface will be handled by Presentation Layer. Presentation layer responsible for the display of required form element and data from database. All business logic will be handled by Business Logic Layer. Accessing database will be handled by Data Access Layer. The usage of interface will make the relationship of each layer loose coupling.

Problem in 3 layer architecture

Even though there are separator between layers, there are still some problem need to take care in the 3 layer based development. If we talk about business logic, it should be handled by business logic layer. But in practical, sometime the business logic will lies in the data access layer through the usage of stored procedure.The question is, who control the design of the software?

Layering in DDD

The value of layers is that each specializes in a particular aspect of a computer program. This specialization allows more cohesive designs of each aspect, and it makes these designs much easier to interpret.

User Interface (or presentation layer)

Responsible for showing information to the user and interpreting the user’s commands. The external actor might sometimes be another computer system rather than a human user.

Application Layer

Defines the jobs the software is supposed to do and directs the expressive domain objects to work out problems. The tasks this layer is responsible for are meaningful to the business or necessary for interaction with the application layers of other systems.

This layer is kept thin. It does not contain business rules or knowledge, but only coordinates tasks and delegates work to collaborations of domain objects in the next layer down. It does not have state reflecting the business situation, but it can have state that reflects the progress of a task for the user or the program.
 

Domain Layer

Responsible for representing concepts of the business, information about the business situation, and business rules. State that reflects the business situation is controlled and used here, even though the technical details of storing it are delegated to the infrastructure. This layer is the heart of business software.

Infrastructure Layer

Provides generic technical capabilities that support the higher layers: message sending for the application, persistence for the domain, drawing widgets for the UI, and so on. The infrastructure layer may also support the pattern of interactions between the four layers through an architectural framework.
 

Sep 23, 2009 Posted Under: Domain Driven Design   Read More

Domain Driven Design series: Repository

 

Domain object does not have any access to any persistence object, remember the term Persistence Ignorance? How do the domain objects make use of persistence object then? Domain object can access the persistence object through repository interface. And the implementation of the repository interface will be handle by Infrastructure layer. What are in the infrastructure layer? There are some repository object, which are implement all repository interface in domain layer.

What kind of method usually reside in the repository object? Based on my experience, I always put the following method in the repository object.

  • List<[DomainClass]> list[DomainName]Data,
  • List<[DomainClass]> list[DomainName]DataByCriteria,
  • [DomaiClass] get[Domain]ById,
  • [DomainClass] get[Domain]ByCriteria,
  • boolean is[Domain]Exist

Those method stated above will be implemented in the Infrastructure Layer. Hibernate ORM normally is used in the implementation of repository interfaces.

In the Spring application context, modify the xml:

<bean name="repositoryImplementationClass" class="SomeRepositoryImplementationClass">

</bean>

How to use those method in the presentation layer? Spring framework will handle the Dependency Injection. In the presentation class, just add:

DomainNameRepository domainService = null;

and create its getter and setter method.

In the Spring application context xml configuration, modify the file xml code

<bean name="presentationEditor" class="SomePresentationLayerClass">

     <property name="domainService" ref="repositoryImplementationClass"/>

</bean>

 

Sep 12, 2009 Posted Under: Domain Driven Design   Read More

Domain Driven Design series: Domain Service

 

Ok, in the previous posting about domain class, we can list all behavior that the domain class can do in the domain class itself. I will take an example of Role Based System. We need several domain class involved: we need the group class, role class, resources classes. Each of those class will contain method that shows the behavior of each domain.

Think of authentication and authorization method, where those methods should reside? In the user domain object? Group domain object? Role domain object? or Resource domain object? None of them. So, any method that is not member of any domain class can be classified in one place as Domain Service.

Authentication and Authorization can not be achieved without using one method in one domain, hence it must be cross domain. Authorization will use user domain’s method and role domain’s method.

Sep 12, 2009 Posted Under: Domain Driven Design   Read More

Domain Driven Design series: Entity and Value Object

 

As I have mention in the previous posting, DDD object can be modeling into Domain Object and Value Object.

The key defining characteristic of an Entity is that it has an Identity – it is unique within the system, and no other Entity, no matter how similar is the same Entity unless it has the same Identity.

Still don’t understand? Ok, think of house, is the house has identity? Off course it has. Is your house my house as well? No way!!! So the house can be treated as Entity class: it has identity, it is not sharable.

Im OK with the entity class concept, but is the value object the same with Value Object in the Java term…

It’s depend!!! In this DDD, the characteristic of Value Object is that it has no Identity. Ok, perhaps a little simplistic, but the intention of a Value Object is to represent something by it’s attributes only. Two VOs may have identical attributes, in which case they are identical. They don’t however have any value other than by virtue of their attributes.

Still blur…

Ok, the example is class paint. Let’s say our entity class want to use the class paint with  color attribute red. Let say, I am using that paint object to paint my house, and the remaining paint i pass to you to paint your house, so in this scenario, paint class is Value Object. It shareable. Let’s put the ID of the class paint, do you think the identification of the paint class is important? We do not need to take care the ownership of the class paint.

One more characteristic of Value Object is immutable, once created they cannot be changed or altered. You can create a new one, and as they have no identity, that is just the same as changing another one.

Ok, what should included in Domain Class anyway? Domain will contain all behavior. Just define any method that the object can do. Take the user domain object: what the user object can do? All getter and setter of all attribute. What else? We will go back to this again later in other post.

references:

Charlton, C.(2009). Domain Driven Design: Step by Step Guide
R.B.(2009).DDD Training Material

 

Sep 12, 2009 Posted Under: Domain Driven Design   Read More

Domain Driven Design series: Persistance Ignorance

 

Persistence Ignoring? You mean we must ignore the storing information in the system? No way!! No, it is not what the DDD means. In DDD, we talk just about the domain. How the behavior of the business object in term of domain. Of course there is still data access activity, but not in domain layer. Domain layer do not care about other layer, domain layer no need to know about how other layer works.

Then how do the object access the database? There is infrastructure layer who responsible to access the database. What kind of class residing in the infrastructure layer? In DDD, we call it Repository class. Repository will provide the access to database via the interface. In the domain layar, the domain object can use the interface to interact with database.

references:

Charlton, C.(2009). Domain Driven Design: Step by Step Guide
R.B.(2009).DDD Training Material

Sep 11, 2009 Posted Under: Domain Driven Design   Read More

Domain Driven Design series: Why DDD?

 

This article is taken from the discussion between me as software engineer with the team leader plus IT Manager.

In mid 1995, Object Oriented Programming concept is widely accepted in the software development. When internet starts to grow, there were a lot of new software development methodology appear in the software development world.

But, a lot of them are constrained by the technology. We must follow the code implementation based on the technology used. For example Enterprise Java Bean. There are rules how to implement our logic in the EJB based technology. We need to follow the rule about how to create a class to be ‘publish’ as Stateless Session Bean, Statefull Session Bean, Entitiy Bean, etc.

People start to leave the design part, they more concentrate in technology part of software development phase. Domain Driven Design is trying to bring back the  software developer from technology constraint to the design.

The Domain Driven Design series article try to elaborate more about what is Domain Driven Design and how to implement DDD in software development phase.

To be honest, me, myself, still new in DDD. I know how to implement 3-tier architecture: Presentation Layer, Business Logic Layer and Data Access Layer. But when new layer is introduced to me, Domain Layer, I need to learn how the Domain Layer will be ‘inserted’ in the 3-tier architecture. I will update this article during the DDD training in my software project team.

4 days later….

After a week attended the training, i was wrong in the previous paragraph. The domain layer is not ‘inserted’ to the 3 layer architecture. It’s totally different concept.

Domain Driven Design is an approach of software design that is focusing on the domain model among other things. Why DDD? The application is built for a domain-centric business.  If team size is small, DDD is suitable to be implemented since  the development can be tested in the domain layer. So it is important to balance the need to build a scalable software, and yet avoiding an overly complex architecture.

Domain Driven Design Characteristic

  • Domain Model is the center piece of the application. It should not be aware of any underlying technology built around it.
  • Domain Model should be Persistence Ignorance.
  • All business rule should be contained in the Domain Model
  • Business entity is modeled as: Domain Object if identity is important or Value Object if identity is not important.
  • Domain Object is organized via Root of Aggregate
  • Each Root of Aggregate is accessed via a Repository. The implementation of the Repository will take care of the persistence.
  • If there is any business rule that is not suitable to be put under any Domain Object or Value Object, then it will be moved into Domain Service.
  • Aggregate might be instantiated via Factory object.

But wait, what are the meanings of those jargon above? Ok, I will explain in the next post.

references:

Charlton, C.(2009). Domain Driven Design: Step by Step Guide
R.B.(2009).DDD Training Material

 

Sep 7, 2009 Posted Under: Domain Driven Design   Read More

Domain Driven Design series: The Ubiquitous Language

 

Ok, assume we got the project to develop the system in SoftONEAlliance Capital Investment Banking. We have a great  Java Enterprise Edition development  team: we have excellent Project Manager, experience System Analyst and ‘geek’ type of System Developer. Don’t forget we have sweet cute Software Testing girl (so if she raise the bug, the guys in development team will not angry to her  )

But, we don’t have domain expert on Investment Banking. Try and find from the client side. We need to have someone who are be able to be our reference in the development phase, person who expert in Investment Banking business process.

Ok, found it as well, now what???

We, the development team need to meet the domain expert. Let us find out all the requirements based on the domain expert explanation. But wait, can they , the domain expert, understand ‘our language’? Does the domain expert understand things like how we understand them? Does the development team have the same understanding like theirs?

Errr…. So?

Yes, we need to use an Ubiquitous Language.

The Ubiquitous Language in DDD is a negotiated language between the Domain Experts and the technical guys – but the rule is that the Domain Experts lead and the technical people follow. As the UL evolves, and this may take weeks or many months, the language becomes more and more refined.

This disjoint is what the concept of the Ubiquitous Language (or UL) is intended to eliminate in Domain Driven Design.

The concept is simple, that development team and the domain expert should share a common language, that both understand to mean the same things, and more importantly, that is set in business terminology, not technical terminology.

 

references:

Charlton, C.(2009). Domain Driven Design: Step by Step Guide

 

Sep 7, 2009 Posted Under: Domain Driven Design   Read More

Calling MS SQL Server 2005 Stored Procedure from Spring JDBCTemplate

 

The idea came from DotNetNuke design pattern, based on experience on DNN based project, SoftONE Logic’s team try to find the easier way to use Stored Procedure on the Spring JDBCTemplate . SoftONE Logic’s team has used iBatis to call stored procedure. Due to the ‘complexity’ of XML, the team start to do research to find other way to implement it.

Here are the outcome of the research.

  •     DB Server : MS SQL Server 2005
  •     Spring Framework 2.5 (JDBCTemplate features).
Get Object from DB

Create Stored Procedure named hr_GetRequisitionForm, has one input parameter.

USE [hr]
    GO
SET ANSI_NULLS ON
    GO
SET QUOTED_IDENTIFIER ON
    GO
CREATE procedure [dbo].[hr_GetRequisitionForm]
    @id int
as
    select
*
    from hr_vw_RequisitionForm
where
   id = @id

Create Method to call above stored procedure by using Spring JDBC Template.

public RequisitionFormVO getRequisitionDataByRequisitionId(int id){
     String queryString = "{call hr_GetRequisitionForm(?)}";
     Object args []= new Object[] {id};
     return (RequisitionFormVO)jdbcTemplate.queryForObject(queryString, args, new RequisitionFormMapper());
}

I assume reader has already familiar with Spring JDBC Tempalte. The hr_GetRequisitionForm stored procedure has one input parameter (id). Pass the value of id to the stored procedure with hr_GetRequisitionForm(?) statement, and Object args []= new Object[] {id}; . Cast the returned object from jdbcTemplate and use the method queryForObject as shown below:

(RequisitionFormVO)jdbcTemplate.queryForObject(queryString, args, new RequisitionFormMapper());

Get List from DB

Create Stored Procedure named hr_GetApprovedRequisitionForm

USE [hr]
   GO
SET ANSI_NULLS ON
   GO
SET QUOTED_IDENTIFIER ON
   GO
CREATE procedure [dbo].[hr_GetApprovedRequisitionForm]
   as
SELECT
   *
FROM
   hr_vw_RequisitionForm
WHERE
   HRManagerVerifiedStatus = 'verified' AND
   SGManagerApprovedStatus = 'approved' AND
   ManagingDirectorApprovedStatus = 'approved'

Create Method to call above stored procedure by using Spring JDBC Template.

public List getApprovedRequisitionData(){
     String queryString = "{call hr_GetApprovedRequisitionForm}";
     return jdbcTemplate.query(queryString, new RequisitionFormMapper());
}

The MapperClass is to map the Database column with ValueObject class.

package com.softonelogic.project.smoe.hr.utils.persistence.sqlmapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.softonelogic.project.smoe.hr.utils.vo.RequisitionFormVO;
public class RequisitionFormMapper implements RowMapper {
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
     RequisitionFormVO obj = new RequisitionFormVO();
     obj.setId(rs.getInt("ID"));
     obj.setPositionVacant(rs.getString("PositionVacant"));
     ...
     return obj;
     }
}

 

Sep 6, 2009 Posted Under: Java Enterprise Edition, Spring Framework   Read More

Introduction to LaTeX and Getting it to Work

 

What is LaTeX?

LaTeX is a typesetting software that can be used to produce documents. It is like the well-known graphical typesetting software such as MS. Words or Open Office Writer. However, the typesetting process in LaTeX is not so straightforward. You need to code it properly to produce high quality documents.

 

Why LaTeX?

LaTeX loses to MS Words in terms of the user-friendly but it has many advantages such as:

  • Documents produced by Latex has higher quality when it is displayed or printed.

  • The programmable typesetting ensures that the format consistency can be maintained

  • It is simply the best package for documents containing mathematics and graphical plots. To insert mathematics expression or graphical plot of a function can be simply done by typing, leaving the trouble of producing the equations and plots on the other software.

  • It is free on virtually every computer in the world and it is platform independent. One may produce a document in Windows but it can still be compiled and run in Linux or MAC.

  • It is portable, i.e. stick to the standard commands and everyone can read and exchange documents.

  • LaTeX has the reputation of being hard, but in fact it is effectively the same as HTML!

 

How to run LaTeX in Windows?

To run Latex, we need two things: one is the LaTeX compiler and two is the text-editor. There are many compilers and text-editors available for free. In this document, we show one example that we have tested in Windows Vista and XP. First, we need to install the compiler and the text-editor to proceed.

 

Compiler (using MiKTeX)

  • Download the compiler here. You may opt for either the basic or the complete versions. For the purpose of this article, we use the basic installation.

  • Make sure that you get basic-miktex-2.7.3248.exe for the installation.

  • Run the installer and follow the standard procedure of installation.

  • Once the installation is complete, the MiKTeX is ready for compiling.

 

Text-editor (using TeXnicCenter)

  • Download the installer here. For majority of the users, the option 1 ( TeXnicCenter installer) is sufficient. Please take note some errors that may occur if you use older version of windows and/or the internet explorer.

  • Make sure that you get TXCSetup_1StableRC1.exe.

  • Run the installer and complete the standard procedure.

  • Open the TeXnicCenter application and it will show the configuration window.

 

  • Click Next and specify the MiKTeX installation directory. Let say my installed MiKTeX is in “C:\Program Files (x86)\MiKTeX 2.7\”. Then, point to the directory of C:\Program Files (x86)\MiKTeX 2.7\miktex\bin” in the configuration window. It should look like this

 

  • Click Next and Finish.

  • Congratulations! Now, you can start to use LaTeX for typesetting and produce high quality documents.

  • To use it, just simply open the TeXnicCenter for inserting the codes.

 

To be continued: Producing simple document with LaTeX.

NB: Please report for the broken link!

Sep 3, 2009 Posted Under: Uncategorized   Read More

Embedded Fonts in Pdf

 

It is often that we need to submit paper or report electronically in portable document format (p.d.f.) that requires the fonts are embedded so that it is easily distributed or printed in various machines and p.d.f. version. It is usual for us to encounter the problem of emmbeded fonts simply because we use our own p.d.f converter. It may be from the pdf driver such as cutepdf, primopdf or our ghost script p.d.f converter.

Here is some "hacking techniques" to debug the problem of embedded fonts (in linux, taken from [1]).  Suppose your non-working p.d.f file is text.pdf

 

(1) convert to ps (postscript file):
pdftops text.pdf

(2) convert back to pdf using prepress settings:
ps2pdf14 -dPDFSETTINGS=/prepress text.ps

(3) check new text.pdf for horrendous formatting errors due to double conversion, and embedded fonts as above.

 

http://axiom.anu.edu.au/~luke/embedded_fonts.html

 

Sep 2, 2009 Posted Under: HOW TO, Portable Document, UNIX   Read More