简体   繁体   English

将我的公司登录名放在vaadin应用程序中的位置

[英]Where to put my business login in vaadin application

I have a small vaadin application which would allow users to input some data in a form (employee details like name, surname, address etc) and store each employee object in a List. 我有一个小的vaadin应用程序,该应用程序允许用户以某种形式输入一些数据(员工详细信息,例如姓名,姓氏,地址等),并将每个员工对象存储在一个列表中。 This is the structure so far: 到目前为止的结构是:

EmployeeForm
    src/main/java
        com.vaadin.project.EmployeeForm
            MyUI.java
        employee
            Address.java
            Employee.java

So, for testing purposes I created an Employee object directly inside MyUI.java, like so: 因此,出于测试目的,我直接在MyUI.java中创建了一个Employee对象,如下所示:

Address address1 = new Address(34, "Flinch Street","KT25AG");
Employee employee1 = new Employee("John","Smith", "Male", 39, address1, "Permanent", 8765);
List<Employee> employee = new ArrayList<Employee>();
employee.add(employee1);
for(Employee currentEmployee : employee ){
    System.out.println(employee);
}

just to make sure things works, but it seems wrong to do that there, so I'd like to create another class that deals with all the business logic, and I was wondering what the best way would be. 只是为了确保一切正常,但是在那做似乎是错误的,所以我想创建另一个处理所有业务逻辑的类,我想知道最好的方法是什么。 By business login I mean that What I need to make sure it happens is that after users fill in the form on the MyUI.java file and click the submit button a new Employee object is created and stored in a List. 通过企业登录,我的意思是我需要确保发生的情况是,在用户填写MyUI.java文件上的表单并单击“提交”按钮之后,将创建一个新的Employee对象并将其存储在List中。 So I'm thinking, can I have a function call in MyUI.java, something like createNewEmployee() and then have that function defined inside another class CreateEmployee.java , get all the data I need (name, surname, address etc) from MyUI.java and construct the new Employee object in CreateEmployee.java ? 所以我在想,我可以在MyUI.java中调用一个函数,例如createNewEmployee() ,然后在另一个类CreateEmployee.java定义该函数,从中获取我需要的所有数据(名称,姓氏,地址等) MyUI.java并在CreateEmployee.java构造新的Employee对象? Does that sound reasonable? 听起来合理吗? It's easy enough to do that inside the MyUI.java but I reckon that should only deal with the GUI, what do you guys think? 在MyUI.java中执行此操作很容易,但是我认为那应该只处理GUI,你们认为呢?

I like to use a service design pattern where all business logic is encapsulated in services. 我喜欢使用一种服务设计模式,其中所有业务逻辑都封装在服务中。

You would have an EmployeeService class that in the simplest form could be instantiated as a member variable on your MyUI. 您将拥有一个EmployeeService类,该类可以以最简单的形式实例化为MyUI上的成员变量。 My services are usually stateless and have business methods. 我的服务通常是无状态的,并且具有业务方法。 In your example it would have the method called createNewEmployee(). 在您的示例中,它将具有名为createNewEmployee()的方法。

I typically like to have an interface for my services. 我通常喜欢为我的服务提供接口。 Some people use an additional layer for any data source interaction (search for DAOs). 有些人为任何数据源交互(搜索DAO)使用附加层。 I like to keep it simple and access the data sources directly from my service. 我喜欢保持简单,并直接从我的服务访问数据源。

If I think I'm going to have a multiple implementations or have need to swap out data sources in the future then I would have an AbstractEmployeeService where most of the true business logic would go and then have a data source specific implementation of that to do data source interaction (for example a JdbcEmployeeService.) 如果我认为我将来将有多种实现或需要交换数据源,那么我将拥有AbstractEmployeeService,其中大多数真正的业务逻辑都将运用于此,然后有一个特定于数据源的实现来执行数据源交互(例如JdbcEmployeeService。)

If you want to get more advanced, I'd recommend using Spring to inject your services. 如果您想获得更高级的建议,我建议使用Spring注入您的服务。 See http://vaadin.github.io/spring-tutorial/ to get a start. 请参阅http://vaadin.github.io/spring-tutorial/以开始使用。

Different developers will have different opinions. 不同的开发人员会有不同的意见。 There is no one right answer, but you are on the right path to separate our your business logic! 没有一个正确的答案,但是您正沿着正确的道路分离我们的业务逻辑!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM