I received an assignment to implement a small system using the data mapper pattern for the data tier. I did not want to do the repetitive task of writing every single mapper for every single entity and I was restricted to not use any libraries or frameworks to do the mapping, so I implemented a generic mapper using reflection to generate SQL scripts with the following interface (pseudocode-like):
Mapper {
create (Class) {
create table in database using Class as reference;
}
create (object) {
insert object into database;
}
read (Class, filters...) {
return a list of objects that match the class specification in the database using the provided filters;
}
update(object) {
update row of the database that has object's primary key with the values in object;
}
delete(Class) {
drop table in the database that has the Class specification;
}
delete(object) {
remove row that has the object's primary key;
}
}
I would then use Java annotations to specify primary keys and foreign keys as well as String size constraints inside the classes I wanted to use it with. Upon giving the completed assignment to my teacher he told me I had implemented an active record instead of a data mapper and now I'm confused as to whether that's correct or not. Is that really not a mapper? He didn't give me a reason, just said that I essentially implemented Hibernate and that it wasn't a mapper.
Aucun commentaire:
Enregistrer un commentaire