petermichaux / maria

The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.
BSD 2-Clause "Simplified" License
764 stars 51 forks source link

Keeping Domain Models Separate From maria.Models? #54

Closed jonahx closed 11 years ago

jonahx commented 11 years ago

Peter,

Quick question:

I like to keep my domain models "raw," and unit test them that way. That is, without any dependencies on a framework. I notice that maria models look like:

maria.Model.subclass(checkit, 'TodoModel', {
    properties: {
        _content: '',
        _isDone: false,
        getContent: function() {
            return this._content;
        },
        // etc 
    }
});

Is it fair to say that the object which is the value of properties could be considered such a raw domain object? So that we could then consider the step of subclassing maria.Model to be the wiring that sets up event dispatching and ties us to the maria framework? If so, what do you think about writing and testing your domain object in completely separate files, and then doing something like:

maria.Model.subclass(checkit, 'TodoModel', {
    properties: new MyDomainObject()
    }
});

Since the domain object itself really has nothing to do with any framework, I like to keep it completely clean.

Thanks for you thoughts.

petermichaux commented 11 years ago

Is it fair to say that the object which is the value of properties could be considered such a raw domain object?

I wouldn't say that. The reason is that setters will call dispatch events...

https://github.com/petermichaux/maria/blob/master/eg/checkit/src/js/models/TodoModel.js#L12

So the object that is the value of properties is not actually independent of Maria. It may appear close to independent but this connection about dispatching events is a strong and important connection.

jonahx commented 11 years ago

Ah, right. And really it's not a limitation of Maria, but just a consequence of MVC models needing an event subscription and dispatch mechanism.

Since there is no need to test the framework in our domain tests, what do you think about doing unit testing on the object in properties without subclassing it as a maria.Model object, but instead, in the "beforeEach" section of the tests, adding a stub method for "dispatchEvent" to the raw domain object so that no errors are thrown? I suppose you could even test to make sure it was being called....

Or do you have a better testing strategy?

One other option would be to build the domain objects without the "dispathEvent" lines, but instead have any mutator methods return true if mutated, false otherwise. Then you create a maria.Model version of each domain object that was essentially just a wrapper which added the event dispatching to the mutator methods, and delegated everything else to the raw domain object. But since javascipt has no equivalent of method_missing, I think the delegation would be an annoying, verbose, boilerplate chore so I don't like this idea....

petermichaux commented 11 years ago

All of your ideas are viable. Personally, I don't have the need for separating the "raw domain object" you refer to from the maria.Model subclass. I'm very happy with just subclassing maria.Model because it is a very simple base class which only adds some event-related methods.