Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js and DAO pattern

First of all I must admit I am quite new to Angular.js and I haven't used any new generation js framework like Backbone or Knockout before. I'm creating an application which communicates with server using RESTful API. I dug a lot into angular documentation and blog notes so as I could do it right.

I found examples mostly with $resource. It looks pretty good: many built-in methods, when you design your REST interface properly you don't even have to write anything more.

But I (and my whole team too) are more used to JavaEE way of thinking about model layer: lightweight model classes (POJO, etc), DAO classes persisting and fetching model and optionally service layer between DAO and controllers. On the other hand in Angular $resource creates something more resembling active record.

I've already come up with two ways how to implement DAO pattern in Angular:

  1. Writing everything from scratch, going down to the $http abstraction level. I'd implement every DAO method as $http call, take care about errors.
  2. Using $resource objects normally like lightweight model classes and passing them to DAOs which are the only unit responsible for calling actions like .$save() on them. Of course we cannot prevent calling it in different place, but solution with such convention is good enough for me.

Second way looks better for me because of reusing existing code. $resource has nice behaviour of promise object and I will be happy if I don't have to implement it myself.

So finally the main question: is active record approach is the only way of doing data access right in Angular, Backbone and other tools like that? Maybe somebody have already tried to incorporate similar solution more resembling DAO in his code and can share his thoughts about it?

And the second question: is $resource object sufficient when it comes to dealing with errors, connection losses and different problems? Is it worth to use $resource having this in mind or it's better to start with lower level $http.

I am at the beginning of the project and I know that this decision may well affect the whole project life later, so I want to choose the best.

like image 865
Mequrel Avatar asked Apr 25 '13 15:04

Mequrel


People also ask

What is the DAO design pattern?

DAO stands for Data Access Object. DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.

What is DAO in typescript?

Data Access Object Concrete Class — implements the above interface [1] Model Object or Value Object — A simple POJO (Plain Old Java Object) containing get/set methods to store data retrieved using the DAO class. [ 1]

Is DAO and model same?

These are very different things. DAO is a CRUD oriented data service (read/create/update/delete data) and model are objects representing data.

What is DAO in Javascript?

In software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database.


1 Answers

I totally agree. Here is the way I do it:

bankApp.factory("CustomerRepository", function ($resource) {
    var customerRepository = $resource("rest/customers/:id", {id:'@id'}, {'update': {method:'PUT'}});
    // You can also add addition repository like $http calls onto the
    // customerRepository like getting the count and other stuff.
    return customerRepository;
});

Then you can inject the CustomerRepository where ever you need it. For example:

bankApp.controller("BankController", function ($scope, CustomerRepository) {

    $scope.customers = CustomerRepository.query();

    $scope.createCustomer = function () {
        CustomerRepository.save($scope.customer, function (customer) {
            ...
        });
    };

    ...

    $scope.saveCustomer = function () {
        CustomerRepository.update($scope.customer, function (customer) {
            ...
        });
    };
});
like image 143
testing123 Avatar answered Sep 19 '22 19:09

testing123