1

Topic: DDD + REST + Unit Testing

It would be desirable to explain some questions on the specified sheaf and on architectural questions as a whole. 1) At first DDD + REST, how they generally ? Something of anything normal not , trivial examples get/post/put/delete where user/id=5 and any examples from real life are resulted. How to make in REST something from  GetAllUsersWhoWasBornOnMonday (), ActivateUser (int id), SelectCardsWithZeroBalance (), LoadUserDetailsPage (), AssignUserToGroup (int userID, int groupID) 2) As testing of layer DDD and services is produced. We tell is at us  class Person Person {public string Name {get; set} public string Surname {get; set} public Date DOB {get; set}; public CalculateAge ()} also is service which twitches from REST UserWorkflow {public bool SendUserPromotion (int userID) {if (userID <0) throw new InvalidUserID (); User user = UserRepository. GetUser (userID); if (user == null) throw new InvalidUser (); if (user. CalculateAge ()> 18) {EmailService. SendPromotionEmail (userID); LogService. LogCommunition (userID, "Promotion was send"); UserRepository.UpdateUserLastPromotionDate (userID); return true;} else {LogService. LogError (userID, "Promotion was not send, user is too young"); return false;} } It is clear that here for unit-testing follows  email service and repository, however there is a pair of questions: - How to check up that EmailService, LogService and UserRepository have been caused with the necessary parameters (the user is higher 18 years) or are not caused? - What to do actually with  class Person if I directly use it in the test of service it like not so unit the test will be, and integration, and  Person through the interface there will be a frank horror and  - it is necessary-whether to test Person. CalculateAge () a method in this case - Obedinjaja with a question number 1 - as will be SendUserPromotion () operation  through interface REST? 3) the Question on the Repository. Obviously its interface quickly acquires methods UpdateUserLastPromotionDate (), GetAllUsersWhoWasBornOnMonday () and AssignUserToGroup (). What methods on structurization of all of it exist, what its class did not turn to a dump from tens such ill-matched methods? I will tell at once I know about existence of idea not to use a repository generally, and directly in a service method to write corresponding EF requests, however such ideas are good in the theory, and in practice lead to noodles from the code of business logic, requests to the data and data model conversion to business models, and the most important thing for writing of units-tests of all it is transited  itself EF a context that takes away a terrible amount of time, and allows  only the elementary requests

2

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> It would be desirable to explain some questions on the specified sheaf and on  S> - How to check up that EmailService, LogService and UserRepository have been caused with the necessary parameters (the user is higher 18 years) or are not caused? Normal  for  give the chance  arguments  services from a box. S> - what to do actually with  class Person if I directly use it in the test of service it like not so unit the test will be, and integration, and  Person through the interface there will be a frank horror and  Until it will be an anemic domain class,  as a matter of fact there will be nothing (change of fields it is possible to trace on calls in other methods) S> - it is necessary-whether to test Person. CalculateAge () a method in this case it is necessary to carry out all business the logic in services and to test it. However I want to mark that Person. CalculateAge (), in the presence of Person. BirthDate is logic of representation and it is not necessary to carry out it. The test () for this purpose it is possible to write a separate unit without everyones . S> - Obedinjaja with a question number 1 - as will be SendUserPromotion () operation  through interface REST? For example/workflow/user/id/promotion S> 3) the Question on the Repository. Obviously its interface quickly acquires methods UpdateUserLastPromotionDate (), GetAllUsersWhoWasBornOnMonday () and AssignUserToGroup (). What methods on structurization of all of it exist, what its class did not turn to a dump from tens such ill-matched methods? In normal programming languages is linq. Though its unit to test - not the most pleasant piece.

3

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> It would be desirable to explain some questions on the specified sheaf and on architectural questions as a whole. S> 1) At first DDD + REST, how they generally ? Something of anything normal not , trivial examples get/post/put/delete where user/id=5 and any examples from real life are resulted. How to make in REST something from  GetAllUsersWhoWasBornOnMonday (), ActivateUser (int id), SelectCardsWithZeroBalance (), LoadUserDetailsPage (), AssignUserToGroup (int userID, int groupID) Google CQRS. Separately model for commands, separately for requests. S> 2) as testing of layer DDD and services is produced. We tell is at us  class Person S> and there is a service which twitches from REST S> Clearly that here for unit-testing follows  email service and repository, however there is a pair of questions: S> - How to check up that EmailService, LogService and UserRepository have been caused with the necessary parameters (the user is higher 18 years) or are not caused? Read docks to library . For example in  is verify methods.

4

Re: DDD + REST + Unit Testing

Hello, itslave, you wrote: I> it is necessary to carry out all business the logic in services and to test it. However I want to mark that Person. CalculateAge (), in the presence of Person. BirthDate is logic of representation and it is not necessary to carry out it. A separate unit the test (for this purpose it is possible to write without everyones . Person. CalculateAge () it is an example, there there will be more difficult methods. About existence of the anemic approach I in course, but at first all am necessary to us normal DDD, secondly even in case of the anemic approach the problem remains on a place - Person turns in any PeopleService and somehow should junit-testitsja together with mine PersonService from an example. On-essence a question it is possible to reduce to that if I will write units-tests for logic Person testing PersonService is possible only in the form of integration test since Person it is necessary  (to select DDD model in interfaces for this purpose). Normal-whether it I> In normal programming languages is linq. I> Though its unit to test - not the most pleasant piece. We use EF, from here and a question. Even if to forget about noodles in the code, a unit-testing of such classes the present nightmare, at me now on absolutely basic methods of type to add or delete the user forms moq setup a unit of the test in the size of times in 10 exceeding code actually a method and the favor of these tests is equal on-essence zero since it is possible  only absolutely basic requests.

5

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> Person. CalculateAge () it is an example, there there will be more difficult methods. About existence of the anemic approach I in course, but at first all am necessary to us normal DDD, secondly even in case of the anemic approach the problem remains on a place - Person turns in any PeopleService and somehow should junit-testitsja together with mine PersonService from an example. S> On-essence a question it is possible to reduce to that if I will write units-tests for logic Person testing PersonService is possible only in the form of integration test since Person it is necessary  (to select DDD model in interfaces for this purpose). It will be normal-whether In case of the anemic approach at you class Person in which there will be only data without logic (there will not be any CalculateAge, Validate etc.) And PersonService about business by logic. In the given approach it is necessary  for dependence only PersonService if they are. If they are not present, and  it is necessary nothing.> but at first to us all it is necessary normal DDD What for it is necessary?

6

Re: DDD + REST + Unit Testing

Hello, AndrewJD, you wrote: AJD> In case of the anemic approach you will have class Person in which there will be only data without logic (there will not be any CalculateAge, Validate etc.) And PersonService about business by logic. In the given approach it is necessary  for dependence only PersonService if they are. If they are not present, and  it is necessary nothing. I now renamed a class in an example in UserWorkflow what not to confuse with UserService the anemic approach. My question consisted in how method UserWorkflow will be tested. SendUserPromotion () without dependence anemic it is the approach or not. In case of DDD I have dependence Person. CalculateAge (), in the anemic approach will be PersonService. CalculateAge (), i.e. testing becomes integration since  these classes it is impossible, they are not carried out in interfaces, accordingly who as approaches to it? I will pay attention, on a difference between UserWorkflow and business logic in Person since It probably confuses someone, class UserWorkflow does not contain business the logic, it contains workflow logic on manipulation with different services, type of a repository, service of mailing of mail, any cryptography,  and other. All should be tested it too, and all these services can be , except for most Person since it part DDD and accordingly not , whence and question DDD is used since all though know that this such, the anemic approach is known only at this forum

7

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> I now renamed a class in an example in UserWorkflow what not to confuse with UserService the anemic approach. My question consisted in how method UserWorkflow will be tested. SendUserPromotion () without dependence anemic it is the approach or not. In case of DDD I have dependence Person. CalculateAge (), in the anemic approach will be PersonService. CalculateAge (), i.e. testing becomes integration since  these classes it is impossible, they are not carried out in interfaces, accordingly who as approaches to it? What difference is called it formally as integration or a unit testing? An essence one, on an input the input data of the scenario moves, on an output the result is checked, and also that   services have been caused (email, DB, etc).

8

Re: DDD + REST + Unit Testing

Hello, AndrewJD, you wrote: AJD> What difference is called it formally as integration or a unit testing? An essence one, on an input the input data of the scenario moves, on an output the result is checked, and also that   services have been caused (email, DB, etc). A difference important since integration test also will be fastened on logic of class DDD which has units-tests. But , your point is clear - the test becomes integration

9

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> Person. CalculateAge () it is an example, there there will be more difficult methods. About existence of the anemic approach I in course, but at first all was necessary to us normal DDD Normal DDD is invented years  for 10 to a testing unit, and as consequence - it not the unit is tested generally. S> secondly even in case of the anemic approach the problem remains on a place - Person turns in any PeopleService and somehow should junit-testitsja together with mine PersonService from an example. S> On-essence a question it is possible to reduce to that if I will write units-tests for logic Person testing PersonService is possible only in the form of integration test since Person it is necessary  (to select DDD model in interfaces for this purpose). Normal-whether it At you remains anemic Person, which not , and 2 services - PeopleService + PersonService which are tested separately. And if it is necessary - integration test on all economy together. S> We use EF, from here and a question. Even if to forget about noodles in the code, a unit-testing of such classes the present nightmare, at me now on absolutely basic methods of type to add or delete the user forms moq setup a unit of the test in the size of times in 10 exceeding code actually a method and the favor of these tests is equal on-essence zero since it is possible  only absolutely basic requests. With CUD generally problems are not present, and here with  - yes, it is necessary to suffer. On a site  there is a step-by-step tutorial. The alternative -   in  , on one method on  - too not to tell that the super.

10

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> My question consisted in how method UserWorkflow will be tested. SendUserPromotion () without dependence anemic it is the approach or not.  prompts that testing strategy depends from .

11

Re: DDD + REST + Unit Testing

Hello, itslave, you wrote: I> At you remains anemic Person, which not , and 2 services - PeopleService + PersonService which are tested separately. And if it is necessary - integration test on all economy together.  PersonService? As a result to transfer in UserWorkflow an armful of interfaces of all services servicing anemic model? Them can is real appear much, I already represent than all it ends - instead of set of small services developers start to mold motley crews from methods since  to contact creation of new service for any case,  and its transmission to a heap of units-tests. Or  will simply mold a new method in wrong service simply because that already  what not  with transmission of new service in workflow. In general I so understand in case of DDD a unit tests for classes using domejn-models do not write, and do integration tests.  I> With CUD generally problems are not present, and here with  - yes, it is necessary to suffer. On a site  there is a step-by-step tutorial. The alternative -   in  , on one method on  - too not to tell that the super. For CUD one record still anything, but if a little (we tell at removal of the user to delete the list of its contacts from other table) the code already becomes , in Moq for check of such things it is necessary to use them  where manually to search for specific deleted record in a collection and to delete all of them on-turn since Moq does not allow to delete a line from a context, this operation should be feigned in  on an own collection. Generally the code of a layer of a repository by this reason nobody  and if strongly it would be desirable - that integration tests from the present DB which can test at the same time much more interesting things than simple  or , for example are simply written so it is possible  check of uniqueness of record in the table, a transactional coordination and a heap of all other, and expended time will be less than if the code  for EF was written

12

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S>  PersonService? As a result to transfer in UserWorkflow an armful of interfaces of all services servicing anemic model? Them can is real appear much, I already represent than all it ends - instead of set of small services developers start to mold motley crews from methods since  to contact creation of new service for any case,  and its transmission to a heap of units-tests. Yes, will be what kol-in services,  in  or where there still. In the most triggered cases saw 20 + in parameters of the designer. For such it is necessary to beat a ruler on hands on the code  and . Also I want to mark that  it is possible to write anyway, and I also repeatedly saw transformations aggregation roots in  monsters. S> in general I so understand in case of DDD a unit tests for classes using domejn-models do not write, and do integration tests.  Yes. After all in classic DDD even methods Load/Save should be  a part business , where here a unit tests. With integration tests all is good except 3 things - necessities of presence configured  for their start (especially actually in case of integrations with any other services) - enough decent (as a rule - several hours) time of running of the full set of tests. - in times big  on writing-refaktoring of tests All it inevitably leads - to less full covering business of logic by tests - to later detection of bugs and as consequence - unstable  If these risks do not guard you - that why are not present. S> For CUD one record still anything, but if a little (we tell at removal of the user to delete the list of its contacts from other table) the code already becomes , in Moq for check of such things it is necessary to use them  where manually to search for specific deleted record in a collection and to delete all of them on-turn since Moq does not allow to delete a line from a context, this operation should be feigned in  on an own collection. Generally the code of a layer of a repository by this reason nobody  and if strongly it would be desirable - that integration tests from the present DB which can test at the same time much more interesting things than simple  or , for example are simply written so it is possible  check of uniqueness of record in the table, a transactional coordination and a heap of all other, and expended time will be recommend to cover exceptional business the logic less than if the code  for EF was written to Voobshcheto a unit by tests,  it does not concern - to check up enough that there was a call of the necessary method to the necessary parameters. And yes. Tests do not replace a unit integration, and add.

13

Re: DDD + REST + Unit Testing

Hello, itslave, you wrote: I> Yes, will be what kol-in services,  in  or where there still. In the most triggered cases saw 20 + in parameters of the designer. For such it is necessary to beat a ruler on hands on the code  and . Also I want to mark that  it is possible to write anyway, and I also repeatedly saw transformations aggregation roots in  monsters. So will be or 20 + parameters on difficult operations, or motley crews, anywhere at such approach from it not to get to I> With integration tests all well except 3 things I> - necessities of presence configured  for their start (especially actually in case of integrations with any other services) I> - enough decent (as a rule - several hours) time of running of the full set of tests. I> - in times big  on writing-refaktoring of tests in my case the test turns out "integration" only because the class workflow is fastened on model DDD, all remaining services (type of a repository or mail service) there will be  so all will be quickly. The logic covering there too will be sufficient, after all model DDD will be covered by the a unit tests. Tests of the code of a repository will be rather "integration only, and that if we generally want to communicate with them since there tests will be pursue with the present database.

14

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> Hello, itslave, you wrote: I>> Yes, will be what kol-in services,  in  or where there still. In the most triggered cases saw 20 + in parameters of the designer. For such it is necessary to beat a ruler on hands on the code  and . Also I want to mark that  it is possible to write anyway, and I also repeatedly saw transformations aggregation roots in  monsters. S> so will be or 20 + parameters on difficult operations, or motley crews, anywhere at such approach from it not to get to the Anemic model gravitates to set small to services, orthodox "rich DDD model" - to god aggregation root. Life - pain S> in my case the test turns out "integration" only because the class workflow is fastened on model DDD, all remaining services (type of a repository or mail service) there will be  so all will be quickly. The logic covering there too will be sufficient, after all model DDD will be covered by the a unit tests. Tests of the code of a repository will be rather "integration only, and that if we generally want to communicate with them since there tests will be pursue with the present database. Again , tests invented a unit such because so more conveniently. Generally more conveniently  dependences than to configure the whole trees  and if they also statefull, to deduce them in the necessary state before test start. Though Probably in your case it is possible to make easier or after the first pack of tests you will reflect and you will make" as recommend ".

15

Re: DDD + REST + Unit Testing

Hello, Stalker., you wrote: S> so will be or 20 + parameters on difficult operations, or motley crews, anywhere at such approach from it not to get to If you workflow uses these 20 + services - that it not to avoid. But we receive the full covering of the scenario the test. As practice shows the main errors arise at component interaction among themselves.

16

Re: DDD + REST + Unit Testing

Hello, itslave, you wrote: I> - necessities of presence configured  for their start (it is especially actual in case of integrations with any other services) I> - enough decent (as a rule - several hours) time of running of the full set of tests. It if high-grade integration test. If under integration to understand only testing some components and usage  and unit test  - this problem is not present. I> - in times big  writing-refaktoring of tests it do not agree. If to use blackbox the approach - that just time it is reduced at refactoring since exterior interfaces normally much are more stable than their implementation. In certain cases it is possible to store  scenarios and the data in exterior XML/JSON files. Time of preparation of the test data probably will be more. But such test covers all scenario + there is a possibility somehow  implementation - the test does not change.

17

Re: DDD + REST + Unit Testing

Hello, AndrewJD, you wrote: AJD> It if high-grade integration test. If under integration to understand only testing some components and usage  and unit test  - this problem is not present. Remains necessity very accurately to work with services which work with exterior resources, first of all - with a database. I>> - in times big  writing-refaktoring of tests AJD> it do not agree. If to use blackbox the approach - that just time it is reduced at refactoring since exterior interfaces normally much are more stable than their implementation. In certain cases it is possible to store  scenarios and the data in exterior XML/JSON files. Time of preparation of the test data probably will be more. But such test covers all scenario + there is a possibility somehow  implementation - the test does not change. Even with blackbox, all very strongly depends on, how much services stateful and how much a tree difficult. State storage in exterior files leads  c .