如何在Orika 1.4.2中用BoundMapperFacade替代MapperFacade?
Great question! You’re right that BoundMapperFacade delivers better performance than the generic MapperFacade—the trick is that in Orika 1.4.2, the method to retrieve it isn’t named getBoundedMapperFacade (that’s a naming change in newer versions). Let’s walk through how to adapt your code step by step:
Step 1: Understand BoundMapperFacade’s Value
Unlike the generic MapperFacade, BoundMapperFacade is type-specific—you create one instance per source/destination class pair you’ve mapped. This cuts out runtime type checks entirely, which is why it’s the faster option. For non-cyclic use cases (no circular references between your mapped classes), this is the optimal choice.
Step 2: Update Your Code to Fetch BoundMapperFacade Instances
Instead of grabbing a single generic MapperFacade, you’ll fetch a dedicated BoundMapperFacade for each registered class pair:
System.setProperty(OrikaSystemProperties.WRITE_SOURCE_FILES, "false"); System.setProperty(OrikaSystemProperties.WRITE_CLASS_FILES, "false"); MapperFactory factory = new DefaultMapperFactory.Builder().build(); // Keep your existing class map registrations factory.classMap(Folder.class, FolderUI.class).byDefault().register(); factory.classMap(VCManager.class, VCManagerUI.class).byDefault().register(); factory.classMap(NSXManager.class, NSXManagerUI.class).byDefault().register(); factory.classMap(L3SwitchManager.class, BaseManagerUI.class).byDefault().register(); // ... add other mappings as needed // Get type-specific BoundMapperFacade instances BoundMapperFacade<Folder, FolderUI> folderMapper = factory.getMapperFacade(Folder.class, FolderUI.class); BoundMapperFacade<VCManager, VCManagerUI> vcManagerMapper = factory.getMapperFacade(VCManager.class, VCManagerUI.class); BoundMapperFacade<NSXManager, NSXManagerUI> nsxManagerMapper = factory.getMapperFacade(NSXManager.class, NSXManagerUI.class); BoundMapperFacade<L3SwitchManager, BaseManagerUI> l3SwitchMapper = factory.getMapperFacade(L3SwitchManager.class, BaseManagerUI.class); // ... create mappers for your other class pairs
Step 3: Use the BoundMapperFacade in Your Application
When mapping objects, use the dedicated mapper for the corresponding type pair instead of the generic one:
// Example: Map a Folder to FolderUI Folder inputFolder = new Folder(); FolderUI outputFolder = folderMapper.map(inputFolder); // Or map to an existing instance folderMapper.map(inputFolder, outputFolder);
Key Notes for Non-Cyclic Usage
- Double-check that there are no circular references between your source and destination classes (e.g., Folder contains a reference to FolderUI, which references back to Folder).
BoundMapperFacadeis non-cyclic by default in Orika 1.4.2—if you do have cycles, you’ll need to usefactory.getCyclicMapperFacade(Class<S>, Class<D>)instead.
Why This Works
In Orika 1.4.2, the getMapperFacade(Class<S>, Class<D>) method returns a BoundMapperFacade<S, D> under the hood. The generic MapperFacade you were using before delegates to these type-specific mappers but adds extra runtime checks, which is why the bound version is more efficient.
内容的提问来源于stack exchange,提问作者tuk

