6

In my project we are using MongoTemplate, which is injected by spring

private final MongoTemplate mongoTemplate;

I know mongo template implements MongoOperations, ApplicationContextAware, so we get context aware method with template, which we do not get if I use the mongo operation type object like below (spring will inject mongo template object in it)

private final MongoOperations mongoOperations;

My doubt is 1. Are we not violating "programming to interface" paradigm doing that 2. Which pattern should we use and why?

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
Pradeep
  • 615
  • 8
  • 14
  • 2
    Check out the below post https://stackoverflow.com/questions/17008947/whats-the-difference-between-spring-datas-mongotemplate-and-mongorepository – Ronnie Horo Jul 24 '19 at 06:10
  • 1
    Mongo operations - an interface that specifies a basic set of MongoDB operations. Implemented by MongoTemplate. Not often used but a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK proxy). https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoOperations.html – RandomCode Sep 26 '19 at 09:44

0 Answers0