Injecting other dependencies to data objects

12 views
Skip to first unread message

Christian Grobmeier

unread,
Nov 4, 2017, 3:58:07 PM11/4/17
to mybatis-user
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

Tim

unread,
Nov 4, 2017, 10:09:18 PM11/4/17
to mybati...@googlegroups.com
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?


--
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 mybatis-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Grobmeier

unread,
Nov 5, 2017, 4:12:44 AM11/5/17
to mybati...@googlegroups.com
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.
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 mybatis-user...@googlegroups.com.

Tim

unread,
Nov 6, 2017, 10:48:19 AM11/6/17
to mybati...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.

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 mybatis-user...@googlegroups.com.
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 mybatis-user...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--


Reply all
Reply to author
Forward
0 new messages