论坛首页 Java企业应用论坛

一个简单的菜单管理,我却迷茫了,求解惑

浏览 9587 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (3)
作者 正文
   发表时间:2011-09-07  
我觉得 在树形结构下,如果要使用充血模型,就得必须使用缓存,否则的话还不如使用贫血来的简单。把一些对某个节点的增,删,改的业务逻辑都写在Menu这个对象上 ,我是这么实现的,然后具体的持久化操作时委托给Repository来实现的。我觉得service之间是可以互相依赖的,层依赖与下一层和自己本层得对象,我记得DDD里面 有这么写过,
这里只是个例子:
public void addChild(MenuNode child) throws CanNotSaveException  {
		if(this.isLeaf()){
			throw new CanNotSaveException("不能往叶子菜单下增加子菜单");
		}
		if(child.getParent()!=null){
			child.getParent().getChildren().remove(child);
		}
		child.parent=this;
		this.children.add(child);
	}

	public void setParent(MenuNode parent) throws CanNotSaveException {
		if(this.parent!=null) {
			this.parent.getChildren().remove(this);
			//return;
		}
		if(parent!=null){
			parent.addChild(this);
		}

		this.parent=parent;
	}


service的方法:
	public void create(MenuNode menuNode,String parentId,String menuId,boolean ignoleDefault) throws CanNotSaveException, CanNotDeleteException{
		
		if( GlobalVariable.DEFAULT_MENU_ID.equals(menuId) && !ignoleDefault) {
			throw new CanNotSaveException("默认菜单不能添加节点");
		}
		
		if(StringUtils.hasText(parentId)){
			MenuNode parent=menuNodeRepository.get(parentId);
			if(parent==null){
				throw new CanNotSaveException("父菜单不存在!");
			}
//			
			menuNode.setParent(parent);
		} else {
			menuNode.setParent(null);
		}
		
		if(!StringUtils.hasText(parentId)){
			menuNode.setMenu(menuManager.get(GlobalVariable.DEFAULT_MENU_ID));
		} else {
			menuNode.setMenu(menuManager.get(menuId));
		}
		
		menuNodeRepository.save(menuNode);
	}

public void updateParent(String id,String oldParentId,String newParentId) throws CanNotUpdateException, CanNotSaveException, CanNotDeleteException {
		this.updateParent(id, oldParentId, newParentId, false);
	}
	
	public void updateParent(String id,String oldParentId,String newParentId,boolean ignoleDefault) throws CanNotUpdateException, CanNotSaveException {
		MenuNode menuNode=menuNodeRepository.get(id);
		if(menuNode.isDefaultMenu()&& !ignoleDefault) {
			throw new CanNotUpdateException("默认菜单不能更新菜单节点的父子关系");
		}
		MenuNode newParent=menuNodeRepository.get(newParentId);
		
		//newParent.addChild(menuNode);
		menuNode.setParent(newParent);
		
		menuNodeRepository.update(menuNode);
		if(newParent!=null){
			menuNodeRepository.update(newParent);
		}	
	}
0 请登录后投票
   发表时间:2011-09-07  
160649888 写道
我觉得 在树形结构下,如果要使用充血模型,就得必须使用缓存,否则的话还不如使用贫血来的简单。把一些对某个节点的增,删,改的业务逻辑都写在Menu这个对象上 ,我是这么实现的,然后具体的持久化操作时委托给Repository来实现的。我觉得service之间是可以互相依赖的,层依赖与下一层和自己本层得对象,我记得DDD里面 有这么写过,
这里只是个例子:
public void addChild(MenuNode child) throws CanNotSaveException  {
		if(this.isLeaf()){
			throw new CanNotSaveException("不能往叶子菜单下增加子菜单");
		}
		if(child.getParent()!=null){
			child.getParent().getChildren().remove(child);
		}
		child.parent=this;
		this.children.add(child);
	}

	public void setParent(MenuNode parent) throws CanNotSaveException {
		if(this.parent!=null) {
			this.parent.getChildren().remove(this);
			//return;
		}
		if(parent!=null){
			parent.addChild(this);
		}

		this.parent=parent;
	}


service的方法:
	public void create(MenuNode menuNode,String parentId,String menuId,boolean ignoleDefault) throws CanNotSaveException, CanNotDeleteException{
		
		if( GlobalVariable.DEFAULT_MENU_ID.equals(menuId) && !ignoleDefault) {
			throw new CanNotSaveException("默认菜单不能添加节点");
		}
		
		if(StringUtils.hasText(parentId)){
			MenuNode parent=menuNodeRepository.get(parentId);
			if(parent==null){
				throw new CanNotSaveException("父菜单不存在!");
			}
//			
			menuNode.setParent(parent);
		} else {
			menuNode.setParent(null);
		}
		
		if(!StringUtils.hasText(parentId)){
			menuNode.setMenu(menuManager.get(GlobalVariable.DEFAULT_MENU_ID));
		} else {
			menuNode.setMenu(menuManager.get(menuId));
		}
		
		menuNodeRepository.save(menuNode);
	}

public void updateParent(String id,String oldParentId,String newParentId) throws CanNotUpdateException, CanNotSaveException, CanNotDeleteException {
		this.updateParent(id, oldParentId, newParentId, false);
	}
	
	public void updateParent(String id,String oldParentId,String newParentId,boolean ignoleDefault) throws CanNotUpdateException, CanNotSaveException {
		MenuNode menuNode=menuNodeRepository.get(id);
		if(menuNode.isDefaultMenu()&& !ignoleDefault) {
			throw new CanNotUpdateException("默认菜单不能更新菜单节点的父子关系");
		}
		MenuNode newParent=menuNodeRepository.get(newParentId);
		
		//newParent.addChild(menuNode);
		menuNode.setParent(newParent);
		
		menuNodeRepository.update(menuNode);
		if(newParent!=null){
			menuNodeRepository.update(newParent);
		}	
	}


首先,很感动你能够认真的参与讨论。
很感谢你回答了我的疑问
1.对贫血模型和充血模型做了一个建议。
2.使用充血模型时,将菜单自身的操作逻辑封装在其自身的领域对象上,其他持久化操作委托给仓储。
3.Service层里可以互相调用依赖。因为DDD中说过可以依赖同层和下层。
4.不仅理论,还将实现代码贴出来。

基于以上四点,我倍感激动。谢谢你。我继续会努力学习DDD。
0 请登录后投票
   发表时间:2011-09-07  
KimHo 写道
生搬硬套设计模式,所谓的XXX驱动,只能带来负面效果
我个人还是推崇简洁设计风格,simple is powerful……


统一简洁设计风格一说。
但是不应该以对立的态度去对待设计模式、XXX驱动和简洁设计。
所以,不赞同你的第一句话。

最后,谢谢你的回帖。
0 请登录后投票
   发表时间:2012-05-29  
净扯些没用的概念...多简单一个问题 网上多少js类库 随便一个都能解决 用户看的是你最终完成的东西 整这么多虚的有啥用
0 请登录后投票
   发表时间:2012-05-29  
复杂问题简单化 该深究的时候再深究,非要把自己弄昏。
0 请登录后投票
   发表时间:2012-05-29  
呵呵,看来LS和LSS都是比较本分的职场人员。
0 请登录后投票
   发表时间:2012-05-30  
没那么复杂,用jquery树形插件解决,比如ztree
0 请登录后投票
   发表时间:2012-05-30  
实际上横向导航菜单也是有层级关系、父子递归关系的,只是你这个需求没有罢了。
所以横向菜单、树形菜单可以统一为菜单对象。
如果真的细分,就随便LZ了
0 请登录后投票
   发表时间:2012-05-30  
感觉你的需求中导航菜单和树形菜单可以用一个菜单来表示,树形菜单的顶端就是你说的导航菜单的内容,那么你只需要一个树形菜单的管理就够了啊,需要新增菜单时新增一个树形菜单,这个树形菜单的名字即可作为导航菜单的内容。
至于说你的程序是否是DDD,如果用贫血模型的话,我觉得就不是DDD了,DDD的一大特点就是领域类是一个相对独立的、能够处理自身关联业务的领域对象,用贫血模型的话领域类内部只有getter、setter,不符合DDD的思想。
0 请登录后投票
   发表时间:2012-05-30  
额.......刚看到是去年的帖子,不知楼主目前是否对DDD有更深的理解,回头看自己当初的问题会不会有一些心得
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics