Injecting other dependencies to data objects

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Injecting other dependencies to data objects

Christian Grobmeier
Hi,

I am not sure if there is a better solution or not, so maybe you guys have an idea how I can solve this.

When I call something like that:
 

User user = userMapper.findUser(id);

I get an user object which is a plain and nice POJO populated by MyBatis.
I would like to use this object with some additional logic and would need to inject something to the user object, like:

User user = userMapper.findUser(TimeZone timeZone, id);

In the user object:

class User {
    TimeZone timeZone;

    // Injection done by constructor

    public String formattedDate() {
      return this.createdAt.format(timeZone);
    }
}

This is just pseudo code, but I hope it makes clear what I want to achieve. What I do now is to iterate through all objects, create new "wrapper" objects containing the additional dependencies and return the wrappers instead.

Of course, this is a lot of overhead and costly with performance. I would love to know if MyBatis could inject these dependencies as well or if there is a better approach I coudl use.

You may have guessed it, I use myBatis with Spring and Annotations.

Thanks!

Christian

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Tim
Reply | Threaded
Open this post in threaded view
|

Re: Injecting other dependencies to data objects

Tim
Do you mean you want to give it additional properties that don't come from the database?
There are several ways that mybatis allows you to "control" an object as it's being created but a simple one for your use case might be the ObjectFactory?


On Sat, Nov 4, 2017 at 3:58 PM, Christian Grobmeier <[hidden email]> wrote:
Hi,

I am not sure if there is a better solution or not, so maybe you guys have an idea how I can solve this.

When I call something like that:
 

User user = userMapper.findUser(id);

I get an user object which is a plain and nice POJO populated by MyBatis.
I would like to use this object with some additional logic and would need to inject something to the user object, like:

User user = userMapper.findUser(TimeZone timeZone, id);

In the user object:

class User {
    TimeZone timeZone;

    // Injection done by constructor

    public String formattedDate() {
      return this.createdAt.format(timeZone);
    }
}

This is just pseudo code, but I hope it makes clear what I want to achieve. What I do now is to iterate through all objects, create new "wrapper" objects containing the additional dependencies and return the wrappers instead.

Of course, this is a lot of overhead and costly with performance. I would love to know if MyBatis could inject these dependencies as well or if there is a better approach I coudl use.

You may have guessed it, I use myBatis with Spring and Annotations.

Thanks!

Christian

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Injecting other dependencies to data objects

Christian Grobmeier-2
ObjectFactory looks pretty interesting, thank you. 
Can I only configure it via XML or is Spring/Annotations supported as well? I haven't found anything in the docs.


On Sun, Nov 5, 2017, at 03:08, Tim wrote:
Do you mean you want to give it additional properties that don't come from the database?
There are several ways that mybatis allows you to "control" an object as it's being created but a simple one for your use case might be the ObjectFactory?


On Sat, Nov 4, 2017 at 3:58 PM, Christian Grobmeier <[hidden email]> wrote:
Hi,

I am not sure if there is a better solution or not, so maybe you guys have an idea how I can solve this.

When I call something like that:
 

User user = userMapper.findUser(id);

I get an user object which is a plain and nice POJO populated by MyBatis.
I would like to use this object with some additional logic and would need to inject something to the user object, like:

User user = userMapper.findUser(TimeZone timeZone, id);

In the user object:

class User {
    TimeZone timeZone;

    // Injection done by constructor

    public String formattedDate() {
      return this.createdAt.format(timeZone);
    }
}

This is just pseudo code, but I hope it makes clear what I want to achieve. What I do now is to iterate through all objects, create new "wrapper" objects containing the additional dependencies and return the wrappers instead.

Of course, this is a lot of overhead and costly with performance. I would love to know if MyBatis could inject these dependencies as well or if there is a better approach I coudl use.

You may have guessed it, I use myBatis with Spring and Annotations.

Thanks!


Christian



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to a topic in the Google Groups "mybatis-user" group.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Tim
Reply | Threaded
Open this post in threaded view
|

Re: Injecting other dependencies to data objects

Tim
It’s on the configuration level.
Look at setObjectFactory in Configuration.
I looked and realized that we don’t have any tests that set this outside of XML but it’s the same idea.
There’s currently no annotations method of setting it.

On Sun, Nov 5, 2017 at 04:12 Christian Grobmeier <[hidden email]> wrote:
ObjectFactory looks pretty interesting, thank you. 
Can I only configure it via XML or is Spring/Annotations supported as well? I haven't found anything in the docs.


On Sun, Nov 5, 2017, at 03:08, Tim wrote:
Do you mean you want to give it additional properties that don't come from the database?
There are several ways that mybatis allows you to "control" an object as it's being created but a simple one for your use case might be the ObjectFactory?


On Sat, Nov 4, 2017 at 3:58 PM, Christian Grobmeier <[hidden email]> wrote:
Hi,

I am not sure if there is a better solution or not, so maybe you guys have an idea how I can solve this.

When I call something like that:
 

User user = userMapper.findUser(id);

I get an user object which is a plain and nice POJO populated by MyBatis.
I would like to use this object with some additional logic and would need to inject something to the user object, like:

User user = userMapper.findUser(TimeZone timeZone, id);

In the user object:

class User {
    TimeZone timeZone;

    // Injection done by constructor

    public String formattedDate() {
      return this.createdAt.format(timeZone);
    }
}

This is just pseudo code, but I hope it makes clear what I want to achieve. What I do now is to iterate through all objects, create new "wrapper" objects containing the additional dependencies and return the wrappers instead.

Of course, this is a lot of overhead and costly with performance. I would love to know if MyBatis could inject these dependencies as well or if there is a better approach I coudl use.

You may have guessed it, I use myBatis with Spring and Annotations.

Thanks!


Christian



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to a topic in the Google Groups "mybatis-user" group.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
--


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.