question about source code reading: why not use facoty pattern to product MapperMethod?

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

question about source code reading: why not use facoty pattern to product MapperMethod?

yue yan
Hi~

I read mybatis-3.2.8.jar source code, I just don't understand, why we need factory to product MapperProxy, but we don't use factory to product MapperMethod.

notice: mybatis-bind.png descript the class diagram of binding package.

for convenience, here is the part of source code of MapperRegistry.java and MapperProxy.java.
// MapperRegistry.java
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<T>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}

// MapperProxy.java 
private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
}

Thank you for help!

--
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.

mybatis-bind.png (48K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: question about source code reading: why not use facoty pattern to product MapperMethod?

yue yan
there is an answer about it, my friend tell me the reason why use factory on MapperProxy is do cache of the MapperProxyFactory rather than MapperProxy, because cache MapperProxy will meet a trouble that there isn't  more than two connection at different sqlsession in a same transation.

在 2018年9月18日星期二 UTC+8下午3:38:35,yue yan写道:
Hi~

I read mybatis-3.2.8.jar source code, I just don't understand, why we need factory to product MapperProxy, but we don't use factory to product MapperMethod.

notice: mybatis-bind.png descript the class diagram of binding package.

for convenience, here is the part of source code of MapperRegistry.java and MapperProxy.java.
// MapperRegistry.java
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<T>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}

// MapperProxy.java 
private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
}

Thank you for help!

--
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.