Where should the behavior live when the user’s story mentions the parent, but the action takes place on the child?

I have a StoreOwner object. StoreOwner has a Store property.

public class Product { }

public class Store { 
    public IEnumerable<Product> Products { get; private set; }
}

public class StoreOwner { 
    public Store Store { get; private set }
}

I have the following user history:

As a store owner, I can add a product to my store.

Where should the "add to store" behavior occur? In the StoreOwner store or in the store?

If it is a repository, what will be the name of the method?

+5
source share
4 answers

I would put it in Store, since I assume the product collection is a child of Store. Something like Store.AddProduct(), not for exhibiting a collection.

, , , , Store, Store , , AddProduct.

, , , , , .

+4

" "? StoreOwner ?

,

, Store. - store.AddProduct(product). , - .

+1

One of the best approaches I've taken is to always look at it, who belongs to whom.

So:

StoreOwner owns a store. The store contains a product.

So, based on your story and the above use case, you will say Store.AddProduct (newProduct);

+1
source

If you only need to be able to add a product, if you have access to the instance StoreOwner, put the method there, otherwise in the class Store.

The name AddProductseems obvious ...

0
source

All Articles