CakePHP: Why entity validation is on Table class intead of Entity class? - validation

If the CakePHP validations valids entities, why the rules is set on table class intead Entity class?

Because it's the table class that consumes the entities, hence its the tables responsibility to provide the contract that defines what's valid and what's not.

Related

Using DataAnnonations constraints on a Data Transfer Object(dto) Model to update a Domain Model

i have a question.
i have a form which returns a CustomerDto model when submitted, i want to add some constraints like required, minlength etc to perform validation on the form, but the domain model class(Customer) that the customerDto model would map the form data to when saving does not have these data annotations constraints, is it possible for me to
add these constraints in the CustomerDto model only and perform server side validation with them and then map the CustomerDto model to the Customer model(domain) which do not have these constraints?
I do not want to alter the existing database structure by adding constraints to the Domain model customer class, but i still want to use CustomerDto model class to perform server side validation

Combine DAO and Entity in Spring and Hibernate

I was wondering if it is possible to combine DAO and Entity in a single class. e.g.
In rails
If I have table named user then there will be one ActiveRecord User and by using that class I can access access Methods related to DB and user i.e. it has both methods user.name (accessing object properties) and user.save / User.get_all methods (managing DB interactions) in the same class
In Spring/Hibernate Configuration
I have two things: DAO and Entity
Entity: I have User class that is an entity and is mapping Table as POJO, so that I can access methods related to a single user e.g. user.getName()
DAO: I have a DAO in which there are DB interactions e.g. userDAO.save(user) and userDAO.get(id).
Question:
I was wondering if I can create single User class and define User properties and getter/setter inside along with DB interactions so that I can single class as both, i.e. user.getName() (as POJO) and User.get(id)/user.save() (as DAO).
Is this method possible, and why are the complications I might run into, if I start with this approach?
it's called Active Record Pattern . Here is article about topic for JPA . Active Record Pattern . and example https://github.com/ActiveJpa/activejpa
Is this method possible, and why are the complications I might run into, if I start with this approach?
it's :
Cohesion & Coupling
if it's real project , it might become problem to support it
when you have 20 entities it's difficult to decide where to put method into what entity , and also find method that you need as it might be in many places
when you don't use active record pattern you can share entity with web layer , with active record entity can't be Serializable.
code become bigger and bigger

Can i write two separate pojo classes one for validation and another for hibernate mapping

If yes, how to map them. I am using jsr bean validation and hibernate mapping annotations.
You don't have to do this by mapping two classes to one table.
You can just define one class A which map to a table as a PO (Persistent Object), and define another class B which transfer values amgonst View, Controler and Service Layer as a DTO/VO (Data Transfer Object/Value Object) for validation or data transfering. use BeanUtils to copy properties between PO and DTO.

how to modify collection classes provided by EF after associating one class to another?

I'm new to MVC & EF. I'm developing the MVC project with model first approach. In my project i have different entities like-customer,employee,product,etc. and i created association between them like 1 to many in customer-employee like this. and after creating this association; it is generating navigation property in customer entity i.e, Employees (collection object) for employee entity.
I want to modify this collection class and i want to add some more methods on it. Is it possible? How to do this if possible?
thanks.
The property is generated with ICollection<Employee> type. You can in theory create your own class implementing this interface and initialize the property for example in Employee constructor but the property will still expose the interface. Changing return type of the property requires change in class generator (you should use T4 template which would make this easy task). By changing property's return type to your collection you can lose some EF functionality.

DDD model to EF

I have two questions.
1: At the moment I have two model classes that are mapped to two entities in the EF diagram.
How does DDD work with EF because if I decide to split my model classes into smaller specific classes e.g. from 2 to 4. How will EF relate to them?
Will I have to create a seperate DTO to map these four model classes to the two entity models that EF will understand?
Just wondering how other people have managed to get around this issue.
2: EF only recognises models that have public properties. If I change my model class to have behavioural methods like GetName(), SetName(), GetAddress() etc and remove the public properties to be private members then EF throws a wobbly and complains it can not find any properties on my model.
How do I solve this issue? Or would the answer be the same as the first question in that I need to create a DTO that has public properties which map from my model class which EF will use?
I'm just thinking if this is the correct path to take because it seems like a bit of redundant work having to map my DDD model classes to another set of DTO or EF model classes that EF understand.
If I do have to map to the EF classes will they be in the model layer or repository layer?
There are possibilities to map multiple entities to the same table (TPH inheritance, table splitting) but these possibilities must follow strict rules. Otherwise you could end up with scenario where you cannot insert entity to database because it doesn't contain all required columns for the record.
EF (with EDMX) recognizes non public properties as well - EF code fist requires at least property with accessible getter or setter. Moreover those behavioral methods can be redundant because writing your own getter and setter in property has the same meaning.

Resources