Skip to main content
added 279 characters in body
Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

I think there is a misconception here. The repository pattern is a persistence pattern. What you are describing is business logic, not persistence logic.

There is a business process you need to model, and the first step in doing this is to create a proper domain model, not an anemic domain model like you currently have (related search: anemic vs rich domain model).

The ReassignLineItems method belongs on one of the entities where they set the Order and IsDeleted properties instead of stuffing this logic in a repository. You shouldn't need to test for an entity's state in the ORM. If properly configured, your ORM should pick up the changes automatically and issue the proper UPDATE statements to the database.

This new method belongs on one of the existing entities, but this process might have a representation in both entities. A concrete recommendation relies on a myriad details which you don't have included in your question. Instead, take this as an opportunity to reconsider the design of this part of your system. You have found non-trivial business logic, and it needs a place to live. You've also discovered that mixing business logic with persistence logic can be troublesome. The solution is separation of concerns and encapsulation; two fundamental tenets of object-oriented design that an anemic domain model flippantly and maliciously throws out the window.


If I remember correctly, Entity Framework 6 is an older version, and I don't remember if it can map private properties. If it can't, I would still recommend adding proper methods to your entities so this logic has a place to live even if properties have public setters and getters. Encapsulation should be enforced by code review to ensure nobody is bypassing the new methods. Not ideal, but it's still better than what you've got now.

An alternative that I've seen many times (though it isn't my preference) is to put this logic in a service class.

This is a decision that forces you to make trade-offs due to a constraint imposed on you by a tool. A tool which may not be possible or practical to upgrade.

I think there is a misconception here. The repository pattern is a persistence pattern. What you are describing is business logic, not persistence logic.

There is a business process you need to model, and the first step in doing this is to create a proper domain model, not an anemic domain model like you currently have (related search: anemic vs rich domain model).

The ReassignLineItems method belongs on one of the entities where they set the Order and IsDeleted properties instead of stuffing this logic in a repository. You shouldn't need to test for an entity's state in the ORM. If properly configured, your ORM should pick up the changes automatically and issue the proper UPDATE statements to the database.

This new method belongs on one of the existing entities, but this process might have a representation in both entities. A concrete recommendation relies on a myriad details which you don't have included in your question. Instead, take this as an opportunity to reconsider the design of this part of your system. You have found non-trivial business logic, and it needs a place to live. You've also discovered that mixing business logic with persistence logic can be troublesome. The solution is separation of concerns and encapsulation; two fundamental tenets of object-oriented design that an anemic domain model flippantly and maliciously throws out the window.


If I remember correctly, Entity Framework 6 is an older version, and I don't remember if it can map private properties. If it can't, I would still recommend adding proper methods to your entities so this logic has a place to live even if properties have public setters and getters. Encapsulation should be enforced by code review to ensure nobody is bypassing the new methods. Not ideal, but it's still better than what you've got now.

I think there is a misconception here. The repository pattern is a persistence pattern. What you are describing is business logic, not persistence logic.

There is a business process you need to model, and the first step in doing this is to create a proper domain model, not an anemic domain model like you currently have (related search: anemic vs rich domain model).

The ReassignLineItems method belongs on one of the entities where they set the Order and IsDeleted properties instead of stuffing this logic in a repository. You shouldn't need to test for an entity's state in the ORM. If properly configured, your ORM should pick up the changes automatically and issue the proper UPDATE statements to the database.

This new method belongs on one of the existing entities, but this process might have a representation in both entities. A concrete recommendation relies on a myriad details which you don't have included in your question. Instead, take this as an opportunity to reconsider the design of this part of your system. You have found non-trivial business logic, and it needs a place to live. You've also discovered that mixing business logic with persistence logic can be troublesome. The solution is separation of concerns and encapsulation; two fundamental tenets of object-oriented design that an anemic domain model flippantly and maliciously throws out the window.


If I remember correctly, Entity Framework 6 is an older version, and I don't remember if it can map private properties. If it can't, I would still recommend adding proper methods to your entities so this logic has a place to live even if properties have public setters and getters. Encapsulation should be enforced by code review to ensure nobody is bypassing the new methods. Not ideal, but it's still better than what you've got now.

An alternative that I've seen many times (though it isn't my preference) is to put this logic in a service class.

This is a decision that forces you to make trade-offs due to a constraint imposed on you by a tool. A tool which may not be possible or practical to upgrade.

Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

I think there is a misconception here. The repository pattern is a persistence pattern. What you are describing is business logic, not persistence logic.

There is a business process you need to model, and the first step in doing this is to create a proper domain model, not an anemic domain model like you currently have (related search: anemic vs rich domain model).

The ReassignLineItems method belongs on one of the entities where they set the Order and IsDeleted properties instead of stuffing this logic in a repository. You shouldn't need to test for an entity's state in the ORM. If properly configured, your ORM should pick up the changes automatically and issue the proper UPDATE statements to the database.

This new method belongs on one of the existing entities, but this process might have a representation in both entities. A concrete recommendation relies on a myriad details which you don't have included in your question. Instead, take this as an opportunity to reconsider the design of this part of your system. You have found non-trivial business logic, and it needs a place to live. You've also discovered that mixing business logic with persistence logic can be troublesome. The solution is separation of concerns and encapsulation; two fundamental tenets of object-oriented design that an anemic domain model flippantly and maliciously throws out the window.


If I remember correctly, Entity Framework 6 is an older version, and I don't remember if it can map private properties. If it can't, I would still recommend adding proper methods to your entities so this logic has a place to live even if properties have public setters and getters. Encapsulation should be enforced by code review to ensure nobody is bypassing the new methods. Not ideal, but it's still better than what you've got now.