3

Possible Duplicate:
Why not use an IoC container to resolve dependencies for entities/business objects?

I asked a very similar question in the past. However, I believe this is not a self-duplicate: the (good) answer to my original question was very specific to this domain problem, and does not discuss the problem in general.

Let's take a new example I came accross:

  • I have a Zone entity, with an associated boundary;
  • I have a Store entity, which has a location as well as a zone property;
  • A ZoneRepository can find which Zone contains a specific location.

The zone property of the Store should never be set directly, but instead deducted from the location when this property is assigned. Therefore, it sounds logical to me to do this:

class Store
{
    public void setLocation(Point location, ZoneRepository repo)
    {
        this.location = location;
        this.zone = repo.findByLocation(location);
    }
}

Are there drawbacks, caveats to this approach? If so, can you suggest realistic alternatives?

Community
  • 1
  • 1
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • This is not a duplicate question. This question is about DDD perspective and the supposed duplicated question has nothing to do with DDD – fabien7474 May 28 '13 at 11:38
  • @fabien7474 Welcome to StackOverflow, where a vote breeds another. You can vote to reopen this question if you think it's worth it! – BenMorel May 28 '13 at 12:29

1 Answers1

-1

What is the relationship between Zone and Location? Zone must have a Location property, correct? Or otherwise you wouldn't be able to search zone by location. Could this be a bi-directional relationship? What I'm getting at, is why can't you do this?

class Store
{
    public function setLocation(Point $location)
    {
        $this->location = $location;
        $this->zone = $location->zone;
    }
}

alternatively, you cold use the repository in the layer above, and then just pass in what you need:

class Store
{
    public function setLocation(Point $location, Zone $zone)
    {
        $this->location = $location;
        $this->zone = $zone;
    }
}

Generally, I would not inject the repository in unless absolutely necessary, and in this situation it is not. Even then, I would not pass it into the method, I would pass it in using an IOC container.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Paul T Davies
  • 2,527
  • 2
  • 22
  • 39
  • It's probably unclear in the question, but location is a `Point`, and the boundary of the Zone is a `Polygon`. When the Store's location is pinpointed on a map, I need to find which Zone contains that point. – BenMorel Dec 23 '11 at 23:20
  • In that case I would go for my second suggestion. – Paul T Davies Dec 23 '11 at 23:21
  • That's indeed a solution, but it doesn't offer a guarantee that this method will be called with consistent `location` <-> `zone` data! – BenMorel Dec 23 '11 at 23:26