Re: Avoid injecting Service/Repository in an Entity

João Bragança <[email protected]>
Newsgroups gmane.comp.programming.domain-driven-design
Message-ID <CACxseT6TrxaT3rymDjpw1-WJ-8MiqpvhTD4tDH3X8CS-UtYL6Q@mail.gmail.com>
You don't want to pull in every student with a repository, if you had a
decent amount of students  your performance will suck.

// you can implement this delegate with a simple sql statement
public delegate bool IsCurrentlyEnrolled(Student student);

public class Class {
  public void Enroll(Student student, IsCurrentlyEnrolled
isCurrentlyEnrolled) {
    Guard.Against(isCurrentlyEnrolled(student), "blah");
    ...
  }
}

Of course if you want this to scale there will be a chance that one could
'get through;' you will need to detect this and prompt for administrative
action down the line.


On Tue, Mar 25, 2014 at 10:15 AM, Jorge Branco
<[email protected]>wrote:

>
>
> From my (limited) understanding, you'd have to implement that kind of
> logic on a command that knows all the relevant services/repositories.
>
>
> On Tue, Mar 25, 2014 at 7:23 AM, <[email protected]> wrote:
>
>>
>>
>> Hi Everyone,
>>
>> From what I've read, this is really bad. But I have the following use
>> case so wondering how to do this in the proper DDD way. The business rule
>> is contrived but it is very similar to what i have in real life. I just
>> cannot disclose the use case due to NDAs.
>>
>> I have a class named Class. A class can have students in it. There is a
>> business rule such that if a student with the same surname belongs to any
>> current of any other class, they cannot be added to the class.
>>
>> So on Class, i have a method named AddStudent. Just wondering how would
>> the algorithm be implemented in a proper DDD sense
>>
>> public class Class
>> {
>>
>> public void AddStudent(Student student)
>> {
>>
>> // Call a repository to get all the students enrolled in all classes
>> // If any students surname matches the surname of the student passed in,
>> do not thing
>> // otherwise add the student
>>
>> }
>>
>> }
>>
>>
>>
>>
>>
>
>
> 
>



-- 
Sent from my regular computer
http://twitter.com/thefringeninja
http://www.thefringeninja.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.