Archive for the ‘Programming’ Category
Google Page Speed: Web Performance Best Practices
When you profile a web page with Page Speed, it evaluates the page’s conformance to a number of different rules. These rules are general front-end best practices you can apply at any stage of web development. Google provides documentation of each of the rules, so whether or not you run the Page Speed tool, you can refer to these pages at any time.
The best practices are grouped into five categories that cover different aspects of page load optimization:
- Optimizing caching: Keeping your application’s data and logic off the network altogether
- Minimizing round-trip times: Reducing the number of serial request-response cycles
- Minimizing request size: Reducing upload size
- Minimizing payload size: Reducing the size of responses, downloads, and cached pages
- Optimizing browser rendering: Improving the browser’s layout of a page
The Little Manual of API Design
This manual gathers together the key insights into API design that were discovered through many years of software development on the Qt application development framework at Trolltech (now part of Nokia). When designing and implementing a library, you should also keep other factors in mind, such as efficiency and ease of implementation, in addition to pure API considerations. And although the focus is on public APIs, there is no harm in applying the principles described here when writing application code or internal library code.
E-Books Directory: More than 300 free programming books
Here is a categorized list of online programming books available for free download. The books cover all major programming languages: Ada, Assembly, Basic, C, C#, C++, CGI, JavaScript, Perl, Delphi, Pascal, Haskell, Java, Lisp, PHP, Prolog, Python, Ruby, as well as some other languages, game programming, and software engineering. The books are in various formats for online reading or downloading.
Yahoo Open Hack 2009 London
Come and join Yahoo! on the 9th and 10th of May in London for 24 hours of learning, hacking, networking and fun.
It’s a two-day event on Saturday 9th of May with a morning of Tech Talks covering a wide range of topics led by some of the Web’s most respected developers. Yahoo will showcase the newest platforms and developer tools for you to play around with, and there will be plenty of experts on hand to answer questions.
Write, share and sell your own books
As commercial book publishing crashes, personal book publishing is booming. Blurb is an online application which can be used to design and print your books in professional looking formats. Blurb makes it easier for you to write, share, promote and sell your own books.
Blurb BookSmart software is the most straightforward and easy to use software available. Multiple demos and tutorials are available, showcasing the potential that each Blurb book offers. Some of the books you buy on Amazon are manufactured with this same technology. You just can’t tell the difference!
From their site:
Holding a finished book with your name on the cover is a truly amazing feeling; it’s one of those experiences everyone should have. As software people, designers and publishing professionals at the top of our game, we realized something both incredible and obvious: there’s no good reason why it should take tons of time, technical skills, big bucks or friends in high places to publish a book. Or a zillion books, for that matter.
Blurb Features
- Design your book with free software
- Print your book by ordering online (as few as 1 book needs ordering)
- Books created are of bookstore quality
- Free to register and design books
- Use the site to promote your books
- Print your books with or without the Blurb Logo
Time to write some books :)
Links
Zend Framework Debug Bar
The Scienta ZF Debug Bar is a plugin for the Zend Framework. It provides useful debug information displayed in a small bar at the bottom of every page. Time spent, memory usage and number of database queries are presented at a glance. Additionally, included files, a listing of available view variables and the complete SQL command of all queries are shown in separate panels.
Great plugin Joakim. Thanks for sharing!
Is Groovy the next programming language in your toolbox?
Groovy offers the flexibility of PHP and the power and capabilities of Java. Groovy meets all the requirements: optional static typing, familiar syntax, runs on a VM, performs as well as Java and supports lots of hot buzzwords like closures and builders. If you are a PHP and/or Ruby developer, I’m sure you are going to love Groovy.
If you are interested in learning more about Groovy and Grails and what it has to offer to your developer toolbox, keep an eye on my blog for some groovy posts :)
Domain-Driven Design: Sample Application
Last updated: 15 Feb, 2010
Part 1: Domain-Driven Design and MVC Architectures
Part 2: Domain-Driven Design: Data Access Strategies
Part 3: Domain-Driven Design: The Repository
Some of the Domain-driven design concepts explained above are applied in this sample application.
Directory Structure
app/
config/
controllers/
UserController.php
domain/
entities/
User.php
UserProfile.php
repositories/
UserRepository.php
views/
lib/
public/
The domain layer should be well separated from the other layers and it should have few dependencies on the framework you are using.
User Entity
The User and UserProfile objects have a one-to-one relationship and form an Aggregate. An Aggregate is as a collection of related objects that have references between each other. Within an Aggregate there’s always an Aggregate Root (parent Entity), in this case User:
class User
{
private $id;
private $name;
/* @var UserProfile */
private $profile;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function setProfile(UserProfile $profile)
{
$this->profile = $profile;
}
public function getProfile()
{
return $this->profile;
}
}
class UserProfile
{
private $id;
public function __construct($id)
{
$this->id = $id;
}
}
Users Collection
A collection is simply an object that groups multiple elements into a single unit.
class Users
{
private $elements = array();
public function __construct(array $users)
{
foreach ($users as $user) {
if (!($user instanceof User)) {
throw new Exception();
}
$this->elements[] = $user;
}
}
public function toArray()
{
return $this->elements;
}
}
User DAO
The UserDAO class allows data access mechanisms to change independently of the code that uses the data:
interface UserDao
{
public function find($id);
public function findAll();
}
class UserDatabaseDao implements UserDao
{
public function find($id)
{
$dataSource = Zf_Orm_Manager::getInstance()->getDataSource()
$db = $dataSource->getConnection('slave');
$query = $db->select()->from('user')->where('id = ?', $id);
return $db->fetchRow($query);
}
public function findAll()
{
...
return $db->fetchAll($query);
}
}
interface UserProfileDao
{
public function find($id);
public function findByUserId($id);
}
class UserProfileDatabaseDao implements UserProfileDao
{
public function find($id)
{
...
}
public function findByUserId($id)
{
$dataSource = Zf_Orm_Manager::getInstance()->getDataSource()
$db = $dataSource->getConnection('slave');
$query = $db->select()->from('user_profile')->where('user_id = ?', $id);
return $db->fetchRow($query);
}
}
User Repository
A Repository is basically a collection of Aggregate Roots. Collections are used to store, retrieve and manipulate Entities and Value objects, however, object management is beyond the scope of this post. The Repository object can inject dependencies on demand, making the instantiation process inexpensive.
class UserRepository
{
/* @var UserDatabaseDao */
private $userDao;
/* @var UserProfileDatabaseDao */
private $userProfileDao;
public function __construct()
{
$this->userDao = new UserDatabaseDao();
$this->userProfileDao = new UserProfileDatabaseDao();
}
public function find($id)
{
$row = $this->userDao->find($id);
$user = new User($row['id'], $row['name']);
$row = $this->userProfileDao->findByUserId($id);
if (isset($row['id'])) {
$profile = new UserProfile($row['id']);
$user->setProfile($profile);
}
return $user;
}
public function findAll()
{
$users = array();
$rows = $this->userDao->findAll();
foreach ($rows as $row) {
$users[] = new User($row['id'], $row['name']);
}
return new Users($users);
}
}
Usage:
$repository = new UserRepository(); $user = $repository->find(1); $profile = $user->getProfile(); $users = $repository->findAll();
Source Code
http://svn.fedecarg.com/repo/Zf/Orm
Links
If you’re interested in learning more about Domain-driven design, I recommend the following articles:
Domain Driven Design and Development In Practice
Domain-Driven Design in an Evolving Architecture
Domain-Driven Design: The Repository
Part 2: Domain-Driven Design: Data Access Strategies
The Ubiquitous Language
The ubiquitous language is the foundation of Domain-driven design. The concept is simple, developers and domain experts share a common language that both understand. This language is set in business terminology, not technical terminology. This ubiquitous language allows the technical team become part of the business.
The Repository
Repositories play an important part in DDD, they speak the language of the domain and act as mediators between the domain and data mapping layers. They provide a common language to all team members by translating technical terminology into business terminology.
In a nutshell, a Repository:
- Is not a data access layer
- Provides a higher level of data manipulation
- Is persistence ignorance
- Is a collection of aggregate roots
- Offers a mechanism to manage entities
Data Access Objects
In the absence of an ORM framework, the Data Access Object (DAO) handles the impedance mismatch that a relational database has with object-oriented techniques. In DDD, you inject Repositories, not DAO’s in domain entities. For example, imagine you have an entity named User that needs to access the database to retrieve the User details:
class User
{
private $id;
private $name;
public function __construct(UserDao $dao, $id)
{
$row = $dao->find($id);
$this->setId($row['id']);
$this->setName($row['name']);
}
public function setId($id) {}
public function getId() {}
public function setName($name) {}
public function getName() {}
}
The User DAO class will look something like this:
interface UserDao
{
public function fetchRow($id);
}
class UserDatabaseDaoImpl implements UserDao
{
public function fetchRow($id)
{
...
$query = $db->select();
$query->from('user');
$query->where('id = ?', $id);
return $db->fetchRow($query);
}
}
$dao = new UserDatabaseDaoImpl();
$user = new User($dao, 1);
$userId = $user->getId();
But, what about separation of concerns? DAO’s are related to persistence, and persistence is infrastructure, not domain. The main problem with the example above is that we have lots of different concerns polluting the domain. According to DDD, an object should be distilled until nothing remains that does not relate to its meaning or support its role in interactions. And that’s exactly the problem the Repository pattern tries to solve.
Injecting Repositories
Lets create a UserRepository class to isolate the domain object from details of the UserDatabaseDaoImpl class:
class User
{
private $id;
private $name;
public function __construct(UserRepository $repository, $id)
{
$row = $repository->find($id);
$this->setId($row['id']);
$this->setName($row['name']);
}
public function setId($id) {}
public function getId() {}
public function setName($name) {}
public function getName() {}
}
It’s the responsibility of the UserRepository to work with all necessary DAO’s and provide all data access services to the domain model in the language which the domain understands.
interface UserRepository
{
public function find($id);
}
class UserRepositoryImpl implements UserRepository
{
private $databaseDao;
public function setDatabaseDao(UserDao $dao) {}
public function getDatabaseDao() {}
public function find($id)
{
return $this->getDatabaseDao()->find($id);
}
}
$userRepository = new UserRepositoryImpl();
$userRepository->setDatabaseDao(new UserDatabaseDaoImpl());
$user = new User($userRepository, 1);
$userId = $user->getId();
$userName = $user->getName();
The main difference between the Repository and the DAO is that the DAO is at a lower level of abstraction and doesn’t speak the ubiquitous language of the domain.
So, what’s next?
The process of creating an entity is complex, because an entity always has relationship with other objects in your domain model. When creating an entity, we have to initialize its relationships as well. Therefore, it’s a good practice to delegate this task to another object.
We’ve seen how to write a persistence-ignorant domain model. Next, I’ll explain how to automate the creation and injection of dependencies.
Domain-Driven Design: Data Access Strategies
Part 1: Domain-Driven Design and MVC Architectures
The Domain Model
Here are some of the features a Domain-driven design framework should support:
- A domain model that is independent and decoupled from the application.
- A reusable library that can be used in many different domain-specific applications.
- Dependency Injection in order to inject Repositories and Services into Domain Objects.
- Integration with unit testing frameworks, such as PHPUnit.
- Good integration with other frameworks, such as Zend, Symfony, Doctrine, etc.
The Zend Framework, for example, is part of the infrastructure layer and acts as a supporting library for all the other layers. However, the domain layer should be well isolated from the other layers of the application and should not be dependent on the framework you are using.
Data Access Strategies
When accessing data from a data source, you have to decide how your application will communicate with a data source. Each data access strategy has its own advantages and disadvantages. Here are some design patterns that support DDD:
Generic DAO’s
Data Access Objects (DAO’s) and Repositories play an important role in DDD. The goal of a DAO is to abstract and encapsulate all access to the data and provide an interface. The DAO always connects, reads and saves data to a data source. From the applications point of view, it makes no difference when it accesses a database, XML file or Web service:
$user = new UserDbDao(); $row = $user->findUserById(456); echo $row->name; $user = new UserXmlDao(); $row = $user->findUserById(456); echo $row->name;
Table Data Gateway
An object that acts as a Gateway to a database table. One instance handles all the rows in the table. The Zend_Db_Table solution is an implementation of the Table Data Gateway pattern:
$user = new User(); $rows = $user->find(456); $row = $rows->current(); echo $row->name;
More info: Table Data Gateway
Row Data Gateway
An object that acts as a Gateway to a single record in a data source. There is one instance per row. Zend_Db_Table_Row is an implementation of the Row Data Gateway pattern:
$user = new User();
$row = $user->fetchRow($user->select()->where('user_id = ?', 1));
echo $row->name;
More info: Row Data Gateway
Active Record
An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. The Mad_Model component is an ORM layer that follows the Active Record pattern where tables map to classes, rows map to objects, and columns to object attributes:
$user = new User(); $userFinder = new UserFinder(); $row = $userFinder->find(456);
More info: Active Record
Data Mapper
A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself:
$user = new User(); $userMapper = new UserMapper(); $row = $userMapper->findByEmail($email);
More info: Data Mapper
Data Transfer Objects
A Data Transfer Object (DTO) in Domain-driven design is not the same as a Value Object. A DTO is often used in combination with a DAO to encapsulate data. It allows you to reduce communication effort when dealing with a lot of small data entities. For example:
class UserData
{
public function setId($id) {}
public function getId() {}
public function setFirstName($firstName) {}
public function getFirstName() {}
public function setLastName($lastName) {}
public function getLastName() {}
}
$user = new UserData();
$user->setFirstName('Jim');
$user->setLastName('Morrison');
$userDao = new UserDbDao();
$userDao->insert($user);
A DTO is a simple container for a set of aggregated data. It should contain no business logic and limit its behaviour to activities such as internal consistency checking and basic validation. Be careful not to make the DTO depend on any new classes as a result of implementing these methods.