macwier
macwier

Reputation: 1083

Should a factory return list of elements?

I am trying to use DDD, and i have a requirement to create some kind of entities. The interface for this method is something similiar to this:

public IEnumerable<Entity> CreateEntities(IEnumerable<Entity> entities, decimal someOtherParameter);

One thing of note is that inside this method there is a group by on the entities.

A simplified (by much) example of what i have now:

public IEnumerable<Entity> CreateEntities(IEnumerable<Entity> entities, decimal someOtherParameter)
{
    var grouped = entities.GroupBy(x=>x.SomeProperty);
    var result = new List<Entity>();
    foreach(var item in grouped)
    {
        result.Add(CreateEntity(item, someOtherParameter));
    }
    return result;
}

public Entity CreateSingleEntity(IEnumerable<Entity> entities, decimal something)
{
    return new Entity(){
        something = something,
        something2 = entities.Sum(x=>x.Amount),
        something3 = entities.First().something3
    };
}

Previously i had this methods as a static on the Entity class. (The one returning single Entity was private) Im thinking about moving this logic to a factory. What's the best way to accomplish this? My ideas:

Upvotes: 1

Views: 243

Answers (1)

JefClaes
JefClaes

Reputation: 3373

A factory doesn't necessarily have to be a new class. Often another aggregate is responsible for creating new entities or aggregates. What does your language say? For example var post = forum.NewPost(user).

Upvotes: 1

Related Questions