Skip to main content
added 655 characters in body
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72

Is it a good practice to always decouple the entity classes from the presentation layer by implementing a domain object (or "view" object)?

Decoupling rolesLoose coupling and responsibilities is often a good practicehigh cohesion are usually two fundamental principles that worth implementing. That saidHowever, decoupling is preceded by an increasewe have to bear in mind that implementing this kind of best practices have a cost. A remarkable increase of the complexity. So, decouplingwhich is good upoften translated into more components to a pointimplement and maintain. WhenA source code harder to envision and to understand at the first sight.

You'll have to weigth if the benefits of these practices offset the costs of implementation and maintenance. For example, if the source code is unlikely to change or if it over-complicatechanges very now and then, you might not appreciate the designbenefits any time soon. If your code is already testeable, becomes counterproductiveprobably either.

Which approach would you take for that?

You may be interested in the followingI would give a chance to some well-known design patterns:. As for instance MVC, MVP and  MVVM. Or in Onion Architectecture too, depending on the size of the application.

Decoupling roles and responsibilities is often a good practice. That said, decoupling is preceded by an increase in the complexity. So, decoupling is good up to a point. When it over-complicate the design, becomes counterproductive.

You may be interested in the following patterns: MVC, MVP and  MVVM. Or in Onion Architectecture, depending on the size of the application.

Is it a good practice to always decouple the entity classes from the presentation layer by implementing a domain object (or "view" object)?

Loose coupling and high cohesion are usually two fundamental principles that worth implementing. However, we have to bear in mind that implementing this kind of best practices have a cost. A remarkable increase of the complexity, which is often translated into more components to implement and maintain. A source code harder to envision and to understand at the first sight.

You'll have to weigth if the benefits of these practices offset the costs of implementation and maintenance. For example, if the source code is unlikely to change or if it changes very now and then, you might not appreciate the benefits any time soon. If your code is already testeable, probably either.

Which approach would you take for that?

I would give a chance to some well-known design patterns. As for instance MVC, MVP and MVVM. Onion Architectecture too, depending on the size of the application.

spellcheck revision
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72

Usually, decouplingDecoupling roles and responsibilities is often a good practice. That being said, decoupling is often preceded by an increase in the complexity of the code base which could, in some cases, outweigh the benefits. NeverthelessSo, let's assume that this's notdecoupling is good up to a point. When it over-complicate the casedesign, becomes counterproductive.

You may be interested in the following patterns: MVC and, MVP and MVVM. Or in Onion Architectecture, depending on the size of the project. According to comments, looks like you already did choose MVVMapplication.

Now, the question boils down to

I was thinking about first rules and best practices and conventions at the beginning of a project. I think it makes sense, don't use the entities of Entity Framework in the view models.Instead of the EF classes, I can use a "view" object. I want to define asa rule that in presentation layer is better don't use EF classes

Law of Demeter may help you with the guidelines of yourthe code conventions. It's often introduced as a principle to design interoperatibilityinteroperability of components at low levels ofof abstraction. But It alsoit works for higher levels of abstraction.

  • Persistence Layer. Persistent entities live in and only in this layer. No domain business liveis implemented here. The componentsComponents at this level are in charge of retrieving and storing data into the different data storages. The persistent model here can be as simple as row mappers, Maps or arrays. The persistence data model is accessible only by upper layers through the proper abstractions. For example DAOs (DAOs, Entity Managers, etc.)

  • *Domain Layer *. The domain data model and its services live here. Theall the business rules happens in this layerlive here. Any need of persistencePersistence is delegated to the Persistence LayerPersistence Layer. The domain model never exposes persistence entities to upper layers. UpperUpper layers do have access to the domain model through the proper abstractions (repositories and domain services). For example Repositories or Domain Services. Usually, for simplicity, no DTO is needed yet. The domain is agnostic to the outer layers' needs of external layers. The mappings to DTOs and mappers should be carried bylocated in those who need itlayers that need'em. The domain entities don'tmodel doesn't necessarily match 1-to-1 the Persistence data model. Both can be different modelsThey have different concerns, hence they are different things.

  • Control Layer:. Controllers liveslive here and they are the bridge between UI and the domain model. TheThe access to the domain model is carrieddone through the proper abstractions (repositories, and services, etc). In order to prevent coupling between our domainUI and the consumer's domain model, controllers do map domain entities to another model. DTOs, POJOS, etc. DTOs are not View models don't live here. The reason is simple, howView Models belong to the view model should look like is UI concern.

Postel's law may help you too at taking decisions regarding interoperatibilityinteroperability among layers and tiers.

My advice is, don't you try a heavy decouplingdecouplings. There's going to be always a little coupling somewhere. Be flexible and KISS.

Following these and other principles (comventionsconventions, best practices, etc) should serve a real pruposepurpose. Apply them while they keep providing value to the application. Going to far in their implementations can be counterproductive. As I mentioned, the further you go, higher is the overall complexity. A solution

Remember that solutions should never be more complex than the problem it solvesproblems they solve (ideally).

KISS KISS and YAGNI are good "mantras"should help you out to keep always in mindremember this premise.

Usually, decoupling roles and responsibilities is a good practice. That being said, decoupling is often preceded by an increase in the complexity of the code base which could, in some cases, outweigh the benefits. Nevertheless, let's assume that this's not the case.

You may be interested in the following patterns: MVC and MVP. Or in Onion Architectecture, depending on the size of the project. According to comments, looks like you already did choose MVVM.

Now, the question boils down to

I was thinking about first rules and best practices and conventions at the beginning of a project. I think it makes sense, don't use the entities of Entity Framework in the view models.Instead of the EF classes, I can use a "view" object. I want to define as rule that in presentation layer is better don't use EF classes

Law of Demeter may help you with the guidelines of your code conventions. It's often introduced as a principle to design interoperatibility of components at low levels of abstraction. But It also works for higher levels of abstraction.

  • Persistence Layer. Persistent entities live in and only in this layer. No business live here. The components at this level are in charge of retrieving and storing data into the different data storages. The model here can be as simple as row mappers. The persistence data model is accessible only by upper layers through the proper abstractions. For example DAOs, Entity Managers, etc.

  • *Domain Layer *. The domain data model and its services live here. The business rules happens in this layer. Any need of persistence is delegated to the Persistence Layer. The domain model never exposes persistence entities to upper layers. Upper layers do have access to the domain model through the proper abstractions. For example Repositories or Domain Services. Usually, for simplicity, no DTO is needed yet. The domain is agnostic to the needs of external layers. The mappings to DTOs should be carried by those who need it. The domain entities don't necessarily match 1-to-1 the Persistence data model. Both can be different models.

  • Control Layer: Controllers lives here and they are the bridge between UI and the domain model. The access to the domain model is carried through the proper abstractions (repositories, services, etc). In order to prevent coupling between our domain and the consumer's domain, controllers do map domain entities to another model. DTOs, POJOS, etc. View models don't live here. The reason is simple, how the view model should look like is UI concern.

Postel's law may help you too at taking decisions regarding interoperatibility among layers and tiers.

My advice is, don't you try a heavy decoupling. There's going to be always a little coupling somewhere. Be flexible and KISS.

Following these and other principles (comventions, best practices, etc) should serve a real prupose. Apply them while they keep providing value to the application. Going to far in their implementations can be counterproductive. As I mentioned, the further you go, higher is the overall complexity. A solution should never be more complex than the problem it solves (ideally).

KISS and YAGNI are good "mantras" to keep always in mind.

Decoupling roles and responsibilities is often a good practice. That said, decoupling is preceded by an increase in the complexity. So, decoupling is good up to a point. When it over-complicate the design, becomes counterproductive.

You may be interested in the following patterns: MVC, MVP and MVVM. Or in Onion Architectecture, depending on the size of the application.

I was thinking about first rules and best practices and conventions at the beginning of a project. I think it makes sense, don't use the entities of Entity Framework in the view models.Instead of the EF classes, I can use a "view" object. I want to define a rule that in presentation layer is better don't use EF classes

Law of Demeter may help you with the guidelines of the code conventions. It's often introduced as a principle to design interoperability of components at low levels of abstraction. But it works for higher levels of abstraction.

  • Persistence Layer. Persistent entities live in and only in this layer. No domain business is implemented here. Components at this level are in charge of retrieving and storing data. The persistent model can be as simple as row mappers, Maps or arrays. The persistence data model is accessible only by upper layers through the proper abstractions (DAOs, Entity Managers, etc)

  • *Domain Layer *. The domain model and all the business rules live here. Persistence is delegated to the Persistence Layer. The domain model never exposes persistence entities to upper layers. Upper layers have access to the domain model through the proper abstractions (repositories and domain services). For simplicity, no DTO is needed. The domain is agnostic to the outer layers' needs. DTOs and mappers should be located in those layers that need'em. The domain model doesn't necessarily match 1-to-1 the Persistence model. They have different concerns, hence they are different things.

  • Control Layer. Controllers live here and they are the bridge between UI and the domain model. The access to the domain model is done through the proper abstractions (repositories and services, etc). In order to prevent coupling between UI and the domain model, controllers do map domain entities to another model. DTOs. DTOs are not View models. View Models belong to the UI.

Postel's law may help you too at taking decisions regarding interoperability among layers and tiers.

My advice is, don't you try heavy decouplings. There's going to be always a little coupling somewhere. Be flexible.

Following these and other principles (conventions, best practices, etc) should serve a real purpose. Apply them while they keep providing value to the application.

Remember that solutions should never be more complex than the problems they solve (ideally). KISS and YAGNI should help you out to remember this premise.

deleted 340 characters in body
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72

You mightmay be interested in the following patterns: MVC and MVP. Or in Onion Architectecture, depending on the size of the project. According to comments, looks like you already did choose MVVM. So far so good.

SoNow, the question spins around the next commentboils down to

Law of Demeter may help you with the guidelines of your code conventions. We usually mention this law in the contextIt's often introduced as a principle to design interoperatibility of the interoperability among components at a low level, but it'slevels of abstraction. But It also validworks for higher levels of abstractions such as layers, tiers, etcabstraction.

  • Persistence Layer or DAL. Persistent entities classes live in and only in this layer. No business live here. The components at this level are in charge of retrieving and storing data into the different data storages. The model here can be as simple as a row mappingmappers. The persistence data model is accessible only by upper layers through the proper abstractions. For example DAOs, Entity Managers, etc.

  • Domain Layer or BL *Domain Layer *. The domain data model and its services live here. The business rules (logic) happens in this layer. Any need of persistence is delegated to the DALPersistence Layer. The domain model never exposes persistence entities to upper layers. Upper layers do have access to the domain model through the proper abstractions. For example Repositories, or Domain Services, Managers, etc. Usually, for simplicity, no DTO is needed yet. The domain is agnostic to the needs of external layers. The mappings to DTOs should be carried by those who need it. Finally, entities of theThe domain model no necessarily match entities of the DAL. There's not aentities don't necessarily match 1-to-1 relationship between components at this levelthe Persistence data model. Both can be different models.

  • Control Layer: Controllers lives here and they are the bridge between UI and the domain model. They shape the facade to access to our domain (or data). The access to the domain model is carried through the proper abstractions (repositories, services, etc). In order to prevent coupling between our domain and the consumer's domain, controllers do map domain entities to another model. DTOs, POJOS, etc. View models don't live here. The reason is simple, how the view model should look like is a UI concern.

Postel's law may help you too for designing the abstractions betweenat taking decisions regarding interoperatibility among layers and tiers. 

My advice is, don't you try a heavy decoupling. There's going to be always a little coupling somewhere between the layers. So beBe flexible and follow KISS.

The rulesFollowing these and conventionsother principles (comventions, best practices, etc) should server toserve a real proposeprupose. Don't state conventions for the state ofApply them while they keep providing value to the artapplication. You haveGoing to identify first what kind of application you havefar in their implementations can be counterproductive. OtherwiseAs I mentioned, you might strive to stick with principles that applied to the project may result in an unnecessary increase infurther you go, higher is the overall complexity. A solution should never be more complex than the problem it solves (as I introduced at the beginning of the answerideally). For example, implementing LocalDTOs for layers inter-communication. You probably don't need them, so don't strive to use DTOs everywhere. Following SOLID principles should enough to achieve loose coupled layers

KISS and componentsYAGNI are good "mantras" to keep always in mind.

You might be interested in the following patterns: MVC and MVP. Or in Onion Architectecture, depending on the size of the project. According to comments, looks like you already did choose MVVM. So far so good.

So, the question spins around the next comment

Law of Demeter may help you with the guidelines of your code conventions. We usually mention this law in the context of the interoperability among components at a low level, but it's also valid for higher levels of abstractions such as layers, tiers, etc.

  • Persistence Layer or DAL. Persistent entities classes live in and only in this layer. No business live here. The components at this level are in charge of retrieving and storing data into the different data storages. The model here can be as simple as a row mapping. The persistence data model is accessible only by upper layers through the proper abstractions. For example DAOs, Entity Managers, etc.

  • Domain Layer or BL. The domain data model and its services live here. The business rules (logic) happens in this layer. Any need of persistence is delegated to the DAL. The domain model never exposes persistence entities to upper layers. Upper layers do have access to the domain model through the proper abstractions. For example Repositories, Domain Services, Managers, etc. Usually, for simplicity, no DTO is needed yet. The domain is agnostic to the needs of external layers. The mappings to DTOs should be carried by those who need it. Finally, entities of the domain model no necessarily match entities of the DAL. There's not a 1-to-1 relationship between components at this level.

  • Control Layer: Controllers lives here and they are the bridge between UI and the domain model. They shape the facade to access to our domain (or data). The access to the model is carried through the proper abstractions (repositories, services, etc). In order to prevent coupling between our domain and the consumer's domain, controllers do map domain entities to another model. DTOs, POJOS, etc. View models don't live here. The reason is simple, how the view model should look like is a UI concern.

Postel's law may help you too for designing the abstractions between layers. My advice is, don't you try a heavy decoupling. There's going to be always a little coupling somewhere between the layers. So be flexible and follow KISS.

The rules and conventions should server to a real propose. Don't state conventions for the state of the art. You have to identify first what kind of application you have. Otherwise, you might strive to stick with principles that applied to the project may result in an unnecessary increase in the complexity (as I introduced at the beginning of the answer). For example, implementing LocalDTOs for layers inter-communication. You probably don't need them, so don't strive to use DTOs everywhere. Following SOLID principles should enough to achieve loose coupled layers and components.

You may be interested in the following patterns: MVC and MVP. Or in Onion Architectecture, depending on the size of the project. According to comments, looks like you already did choose MVVM.

Now, the question boils down to

Law of Demeter may help you with the guidelines of your code conventions. It's often introduced as a principle to design interoperatibility of components at low levels of abstraction. But It also works for higher levels of abstraction.

  • Persistence Layer. Persistent entities live in and only in this layer. No business live here. The components at this level are in charge of retrieving and storing data into the different data storages. The model here can be as simple as row mappers. The persistence data model is accessible only by upper layers through the proper abstractions. For example DAOs, Entity Managers, etc.

  • *Domain Layer *. The domain data model and its services live here. The business rules happens in this layer. Any need of persistence is delegated to the Persistence Layer. The domain model never exposes persistence entities to upper layers. Upper layers do have access to the domain model through the proper abstractions. For example Repositories or Domain Services. Usually, for simplicity, no DTO is needed yet. The domain is agnostic to the needs of external layers. The mappings to DTOs should be carried by those who need it. The domain entities don't necessarily match 1-to-1 the Persistence data model. Both can be different models.

  • Control Layer: Controllers lives here and they are the bridge between UI and the domain model. The access to the domain model is carried through the proper abstractions (repositories, services, etc). In order to prevent coupling between our domain and the consumer's domain, controllers do map domain entities to another model. DTOs, POJOS, etc. View models don't live here. The reason is simple, how the view model should look like is UI concern.

Postel's law may help you too at taking decisions regarding interoperatibility among layers and tiers. 

My advice is, don't you try a heavy decoupling. There's going to be always a little coupling somewhere. Be flexible and KISS.

Following these and other principles (comventions, best practices, etc) should serve a real prupose. Apply them while they keep providing value to the application. Going to far in their implementations can be counterproductive. As I mentioned, the further you go, higher is the overall complexity. A solution should never be more complex than the problem it solves (ideally).

KISS and YAGNI are good "mantras" to keep always in mind.

Edited accordingly with the OP's comments
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72
Loading
edited body; added 11 characters in body
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72
Loading
added 5 characters in body; deleted 6 characters in body
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72
Loading
Source Link
Laiv
  • 15k
  • 2
  • 34
  • 72
Loading