2010/7/31 josh robb <josh_r...@fastmail.fm>:
> There's nothing wrong with passing a LicenseRepository into a method
> on the Software class. This allows easy testing and also stops
> coupling your models to persistence.
>
> class Software{
>   bool IsValidLicenseCombination(LicensesRepository repo) {
>      var licenses = repo.FindValidLicenses(this,...);
>      ....
>   }
> }
>
> This doesn't end up with an anemic model or a transaction script.
> Although you still need to have the repo somewhere to pass in.

This works well in theory. Looking back at my example of license
management, I have not only Software and License, I also have
LicenseInstance, Installation and Computer as aggregate roots among
other entities.
I also modeled a lot of relations as unidirectional many-to-one
relations: License have thousands of LicenseInstances, but most of the
time I only need counting used and unused ones, so there isn't any
Instances collection in License. The situation between Computer and
Installation is similar: Computers might have hundreds of
installations, but the only thing I need to know most of the time is
whether an installation was required and done on a computer.
The method sketched above tests whether there are installation
packages of that software that are already installed on the computer,
it checks whether license instances are available and so on.
In reality, the method ended like converting this:
class Software
{
  public bool CanBeLicensedFor(Computer computer, User user)
  {
    //...
    computer.IsInstalled(this.InstallationPackage);
    //...
    foreach(var license in this.RequiredLicenses)
    {
      var free = LicenseInstance.IsInstanceAvailableOf(license);
      var assigned = computer.IsLicenseInstanceAssigned(license);
      //...
    }
  }
}

into:
class Software
{
  public bool CanBeLicensedFor(Computer computer, User user,
                               InstallationRepository instRepo,
                               LicenseInstanceRepository licInstRepo)
  {
    //...
    computer.IsInstalled(this.InstallationPackage,
Installationrepository instRepo);
    //...
    foreach(var license in this.RequiredLicenses)
    {
      var free = licInstRepo.IsInstanceAvailableOf(license);
      var assigned = computer.IsLicenseInstanceAssigned(license, licInstRepo);
      //...
    }
  }
}

I stopped the approach when I had four repositories in a single
method. That was when I first thought of ActiveRecord should use the
repositories for me, so I can use ActiveRecord with static methods. I
would tell AR that I'm testing now and that all data access should be
stubbed and only these and those methods should return some test
objects.

-Markus

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to castle-project-us...@googlegroups.com.
To unsubscribe from this group, send email to 
castle-project-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to