110 likes | 332 Views
重构. 减少重复、提高效率、引进新的编程模式. B2B 平台技术部应用平台一部. Round 1. GenericDao 泛型数据访问对象 DRM – Domain Relational model 领域关系模型. Svn 代码地址 : http://svn.alibaba-inc.com/repos/ali_platform/roma/sample/trunk/quickstart. 如何增强现有类的行为. 动态代理 通过 JDK 接口增强的方式为指定接口提供 Proxy 实现 如:
E N D
重构 减少重复、提高效率、引进新的编程模式 B2B 平台技术部应用平台一部
Round 1 • GenericDao 泛型数据访问对象 • DRM – Domain Relational model 领域关系模型 Svn 代码地址 : http://svn.alibaba-inc.com/repos/ali_platform/roma/sample/trunk/quickstart
如何增强现有类的行为 • 动态代理 通过JDK接口增强的方式为指定接口提供Proxy实现 如: Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) • 字节码增强 通过动态修改类的字节码方式来增强或者改变类的行为 如: ASM、CGLIB以及Javassit等
GenericDao (介绍) • 目的 • 通过统一的数据访问接口来实现对实体对象的CRUD • 通过可扩展的持久层来无缝接入Ibatis、Hibernate、Toplink、JPA等。 • 开发人员只需要继承这个接口,不用写重复的实现代码。 • 实现 • 通过泛型的方式来约束DAO的操作对象 • 通过弱类型的方式来实现接口的通用性 • 通过Annotation的方式来补充对强类型接口的支持
GenericDao (表现形式) Public interface GenericDao<PK, E> { public PK create(E entity) throws DataAccessException; public boolean update(E entity) throws DataAccessException; public boolean delete(PK pk) throws DataAccessException; public E load(PK pk) throws DataAccessException; public List<E> find(Object parameters) throws DataAccessException; public int count(Object parameters) throws DataAccessException; public boolean executeBatch(List<BatchTask> tasks) throws DataAccessException; …………. } Public interfaceUserDao extends GenericDao<Integer, User> { } Public interface RoleDaoextends GenericDao<Integer, Role> { }
GenericDao (使用) @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; public Integer createUser(User user) { return this.userDao.create(user); } ....... } @Transactional public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDao; public Integer updateRole(Role role) { return this.roleDao.update(role); } ....... }
GenericDao (强类型支持) Public interface UserDaoextends GenericDao<Integer, User> { @Load public User loadContact(Integer contactId); @Find(sqlName = "byNameOrMbile") public List<E> findUsersByNameOrMobile(@Param("name") String name, @Param("mobile") String mobile); @Load(sqlName = "loadMyDto") public MyDto loadDto(@Param("contactId") Integer contactId); …………. }
DRM (介绍) • 目的 • 增强现有持久对象(Entity)的行为,让其具备生老病死的自我控制能力 • 引入新的编程模式,DDD (Domain Driven Development) • 舍弃DAO • 局限 • 只支持字节码增强方式 • 不提供远程调用模式的支持
DRM (表现形式-1) Public class User extends DomainAdapter<Integer, User> implements Serializable { private static final long serialVersionUID = -3483023166493420830L; @Id public Integer userId; public String name; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) public Address address; @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REMOVE }) public List<Role> roles = new ArrayList<Role>(0); }
DRM (表现形式-2) Public class Address extends DomainAdapter<Integer, Address> implements Serializable { private static final long serialVersionUID = 5366024266900174273L; @Id public Integer addressId; public String street; public String zipcode; } Public class Role extends DomainAdapter<Integer, Role> implements Serializable { private static final long serialVersionUID = -3679372698163243616L; @Id public Integer roleId; public String name; public String description; }
DRM (使用) User user = user(); user.name = "roma"; user.address = address(); user.address.street = "长沙"; user.address.zipcode = "35131"; user.addRole(role().get(102)).addRole(role().get(101)); user.save(); //查找刚才新添加的用户 User newUser = user().get(user.userId); newUser.address.street = “北京”; newUser.update();