`
nihongye
  • 浏览: 102188 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

domain model sample

阅读更多
public class Department {

	public static void employee(String name, String department) {
		User.create(name,department);
	}
	
}


@Entity
public class Kind {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int id = -1;

	public String name;

	public Kind(String name) {
		this.name = name;
	}

	public static Kind create(String name) {
		Kind kind = new Kind(name);
		Context.em.persist(kind);
		return kind;
	}

	public void addBatchTaskToUsers(String task) {
		List<User> users = users();
		for (User user : users) {
			Task taskEntity = new Task(task, this);
			Context.em.persist(taskEntity);
			user.applyTask(taskEntity);
		}
	}

	public List<User> users() {
		Query query = Context.em
				.createQuery("select distinct u from User as u inner join u.kinds as kind where kind = ?1");
		query.setParameter(1, this);
		return query.getResultList();
	}

}


@Entity
public class Task {
	public Task(String task,Kind kind) {
		this.kind = kind;
		this.task = task;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int id = -1;
	
	public Date startTime;
	
	public Date endTime;
	
	public String task;
	
	@ManyToOne
	public User owner;
	
	@ManyToOne
	public Kind kind;

	public static Task create(String name, Kind kind) {
		Task task = new Task(name,kind);
		Context.em.persist(task);
		return task;
	}

	private static Date getCurrentMonthBegin()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH,0);
		Date begin = calendar.getTime();
		return begin;
	}
	
	public static Date getCurrentMonthEnd()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getMaximum(Calendar.DAY_OF_MONTH));
		Date end = calendar.getTime();
		return end;
	}
	
	public static List<Task> allTask_CurrentMonth() {
		Date begin = getCurrentMonthBegin();
		Date end = getCurrentMonthEnd();
		Query query = Context.em.createQuery("from Task as t where t.startTime >= ?1 and t.startTime <= ?2");
		query.setParameter(1,begin);
		query.setParameter(2,end);
		return query.getResultList();
	}

	public static List<Task> processingTasks_CurrentMonth() {
		Date begin = getCurrentMonthBegin();
		Date end = getCurrentMonthEnd();
		Query query = Context.em.createQuery("from Task as t where t.startTime >= ?1 and t.startTime <= ?2 and t.endTime is null");
		query.setParameter(1,begin);
		query.setParameter(2,end);
		return query.getResultList();
	}

	public static List<Task> processedTasks_CurrentMonth() {
		Date begin = getCurrentMonthBegin();
		Date end = getCurrentMonthEnd();
		Query query = Context.em.createQuery("from Task as t where t.endTime >= ?1 and t.endTime <= ?2 ");
		query.setParameter(1,begin);
		query.setParameter(2,end);
		return query.getResultList();
	}
}


@Entity
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int id = -1;

	public String name;

	public String department;

	@OneToMany
	public List<Kind> kinds = new ArrayList<Kind>();

	public User(String name, String department) {
		this.name = name;
		this.department = department;
	}

	public Tasks tasks = new Tasks(this);

	class Tasks {
		private User user;

		public Tasks(User user) {
			this.user = user;
		}

		public Task find_by_name(String task) {
			Query query = Context.em
					.createQuery("from Task t where t.owner = ?1 and t.task = ?2");
			query.setParameter(1, user);
			query.setParameter(2, task);
			return (Task) query.getSingleResult();
		}

		public List<Task> processing_tasks() {
			Query query = Context.em
					.createQuery("from Task t where t.startTime <= ?1 and t.owner = ?2 and t.endTime is null");
			query.setParameter(1, new Date());
			query.setParameter(2, user);
			return query.getResultList();
		}

		public boolean detectProcessingTask(String task) {
			for(Task taskEntity:processing_tasks())
			{
				if(task.equals(taskEntity.task))
				{
					return true;
				}
			}
			return false;
		}
	}

	public static User find_By_Name_And_Department(String name,
			String department) {
		Query query = Context.em
				.createQuery("from User u where u.name = ?1 and u.department = ?2");
		query.setParameter(1, name);
		query.setParameter(2, department);
		return (User) query.getSingleResult();
	}

	public static User create(String name, String department) {
		User user = new User(name, department);
		Context.em.persist(user);
		return user;
	}

	public void applyTask(Task task) {
		task.owner = this;
		task.startTime = new Date();
		Context.em.persist(task);
	}

	public static List<Task> all_processing_tasks() {
		Query query = Context.em
				.createQuery("from Task t where t.startTime <= ?1  and t.endTime is null");
		query.setParameter(1, new Date());
		return query.getResultList();
	}

	public void endTask(Task task) {
		task.endTime = new Date();
	}
}

分享到:
评论
2 楼 black_angle 2008-05-31  
Tasks 的设计真是经典啊 哈啊哈
1 楼 black_angle 2008-05-31  
我觉得User对象,不应该有创建自己的行为,面向对象就是把现实的东西映射到计算机中,一个人能创建自己吗?所以我觉得,创建这个行为,应该放到另外一个对象中,如DDD中的Repository,望赐教!1

相关推荐

    IDDD_Sample.zip

    8. **领域模型(Domain Model)**:整个DDD的核心,它反映了业务领域的知识和规则。Java代码中的领域模型类应尽可能地表达业务语言。 9. **策略(Strategy)**:将算法封装为可替换的策略对象,使得算法可以在不...

    ddd-sample.rar

    首先,领域驱动设计的核心在于“领域模型”(Domain Model),它是对业务领域的抽象和模拟。在船运系统中,领域模型可能包括船、货物、航线、港口等实体(Entity)和值对象(Value Object)。实体是具有唯一标识的...

    es-geo-sample

    5. **查询和过滤**:使用Elasticsearch提供的查询DSL(Domain Specific Language)来执行地理位置查询。例如,查找距离特定点一定范围内的地点: ```ruby Location.search(query: { geo_distance: { distance: '...

    毕业设计_论文正文_宋启迪1

    The research presented in this thesis contributes to the advancement of handwritten digit recognition within the broader domain of artificial intelligence. By comparing the performance of two ...

    CQRS.Sample:CQRS 示例

    3. 领域模型(Domain Model):包含业务逻辑和规则,是系统的核心部分。在CQRS中,领域模型可能会包含事件发布(Event Publishing)机制,当命令处理器完成对状态的修改后,会触发相应事件。 4. 事件处理器(Event ...

    ArcGIS_FlexView指南(中文)

    - **crossdomain.xml:** 为了允许跨域数据访问,需要正确配置`crossdomain.xml`文件。 - **网络资源代理:** 对于敏感数据的访问,可以通过网络资源代理来增加一层保护。 #### 七、附录A: Configuration XML 配置...

    4-7+全链路联动:面向最终目标的全链路一致性建模.pdf

    2. 粗排ESDM(Entire Space Domain Adaptation Model)模型:该模型可以解决召回模块的样本选择偏差问题。 3. 集合选择技术:该技术可以放弃对值的精准预估,以集合选择为目标,从而释放一部分算力。 4. 以学习后...

    pixeldragonsmvc .net 2.0 的一个mvc框架

    2. **PixelDragons.MVCSample.Domain** - 这个文件夹可能包含了示例项目的领域模型,即业务逻辑和实体类,这是MVC模式中的"Model"部分。 3. **PixelDragons.MVC** - 这可能包含了pixeldragonsmvc框架的核心组件,如...

    dm844-sample-project:DM844 课程的示例项目

    1. **MVC架构**:Grails遵循Model-View-Controller(MVC)设计模式,使得代码组织结构清晰,便于维护和扩展。模型负责业务逻辑,视图负责数据展示,控制器则协调两者之间的交互。 2. **GORM(Grails Object ...

    谱压缩感知压缩感知在谱估计中的应用

    Unfortunately, such signals are only sparse in the discrete Fourier transform (DFT) domain when the sinusoid frequencies live precisely at the center of the DFT bins. When this is not the case, CS ...

    Android代码-rosie-kotlin

    Rosie divides your application in three layers, view, domain and repository. View: It contains all your presentation logic, implemented through the [Model-View-Presenter pattern][mvp

    Pro JPA 2 in Java EE 8: An In-Depth Guide to Java Persistence APIs.pdf

    The examples use a common model from an overarching sample application, giving you a context from which to start and helping you to understand the examples within an already familiar domain....

    基于支持向量机的机器学习研究 Research of Machine-Learning Based Support Vector Machine

    This article summarize today state and application domain of machine-learning and Support Vector Machine,expound basic concept and basic model of machine-learning and Support Vector Machine and ...

    Time-resolved terahertz spectroscopy of charge carrier dynamics in the chalcogenide glass As30Se30Te40 [Invited]

    Broadband (1.6–18 THz) terahertz time-domain spectroscopy (THz-TDS) and time-resolved terahertz spectroscopy (TRTS) were performed on a 54 μm thick chalcogenide glass (As30Se30Te40) sample with a ...

    最实用MVC源代码pixeldragonsmvc

    在"PixelDragons.MVCSample.Domain"目录中,我们可以预见到包含实体类(Entity Class)和可能的数据访问层(Data Access Layer)。这些实体类通常对应数据库中的表,它们封装了数据和操作数据的方法。NHibernate,...

    basic-inventory:中小企业的基本库存网络应用程序

    蛋堆通过朱拉克·安蒂娜(Julac Ontina) 该框架基于Java Spring Boot中的Controller-Domain-Repository-Service(CDRS)框架或结构。 它旨在替换Softboiled-MVC,因为发现可以将Model-Controller-View(MVC)进一步...

    CVPR2018_Oral_论文合集_人工智能_机器学习

    Accurate and Diverse Sampling of Sequences based on a “Best of Many” Sample Objective .pdf Actor and Action Video Segmentation from a Sentence .pdf An Analysis of Scale Invariance in Object ...

    Physically Based Rendering from Theory to Implementation - part1

    The Frequency Domain and the Fourier Transform Ideal Sampling and Reconstruction Aliasing Antialiasing Techniques Application to Image Synthesis Sources of Aliasing in Rendering Understanding ...

    drools的Guvnor规则管理系统使用教程

    import com.sample.domain.Message; import com.sample.domain.Address; //声明一个Country类型Fact declare Country countryName: String countryCode: String createDate: Date end global List myGlobalList...

Global site tag (gtag.js) - Google Analytics