Hi everyone,
For a library I'm writing I'm trying to set up a base "user" class which could form a basis for, for example, a user registered on a website.
1 2 3 4 5
|
struct base_user
{
int id;
std::string username, password;
};
|
I use an adapter-pattern for mapping the users data to a datastore, to separate the type of datastore from the user-implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
struct base_user_mapper_interface
{
virtual void read(base_user& u)=0;
virtual void create(base_user& u)=0;
//etc.
};
struct base_user_sql_mapper : public base_mapper_interface
{
void read(base_user& u)
{
fetch("SELECT * FROM users WHERE id=?", u.id);
//etc..
}
void create(base_user& u)
{
execute("INSERT INTO users(name, password) VALUES(?,?)", u.name, u.password);
//etc.
}
};
|
So here is my problem: This is supposed to be part of a library, making it easy for me to create new types of users, for example:
1 2 3 4
|
struct my_user : public base_user
{
std::string email, address;
};
|
Now, when I want to create a new user in the datastore I would have to make a new mapper class as such:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
struct my_user_sql_mapper : public base_user_sql_mapper
{
void read(my_user& u)
{
base_user_sql_mapper::read(u);
fetch("SELECT email, address FROM my_users WHERE id=?", u.id);
//etc.
}
void create(my_user& u)
{
base_user_sql_mapper::create(u);
execute("INSERT INTO my_users(email, password) VALUES(?,?)", u.email, u.password);
}
};
|
However, that means that for every operation on the datastore, I need to connect twice to the datastore and perform actions on it. From an OO standpoint, to me, this seems the most logical solution, but from a datastore standpoint, it seems terribly inefficient.
Any advice on alternative solutions / design patterns would be much appreciated (even if it's just that this performance hit is something to live with).
Sorry for the long post, I like to be thorough.
Thanks in advance,
NwN