在这种情况下我该如何决定我的领域模型?

发布于 2024-12-22 23:54:34 字数 923 浏览 2 评论 0原文

我需要开发一个应用程序用于IT项目中的用户管理。这样做是为了学习 Grails。我有一些问题要开始:

在我的示例应用程序中,用户有很多任务,属于一个项目,有很多假期状态,属于资源规划,仅此而已。 (某种要求!)

现在......说到领域建模,我实际上是如何建模的?我想出了这样的建模解决方案:

class User { 
        //relationships. . . . 
        static belongsTo = [ company : Company, role : Role, resource : Resource] 
        static hasMany = [ holidays : Holiday, tasks : Tasks ] 
        Profile profile 
        Project project 
        String login 
        String password 
        static constraints = { 
                login(unique:true,size:6..15) 
                profile(nullable:true) 
        } 
        String toString() { 
                this.login 
        } 
} 

现在利用 Grails 脚手架。我生成了视图,嗯,这就是我震惊的地方!

有了这个模型,显然在创建新用户时,我需要放弃项目详细信息、资源详细信息。即使我可以根据需要更改视图,例如,我只需要两个字段(即新用户的登录名和密码)即可注册我的网站。但根据数据建模,我如何处理其他字段,如配置文件、项目和其他关系。这听起来很复杂!我知道我是网络开发的新手,我想要您的建议以及我该如何继续?

提前致谢。

I need to develop a application for a user's management in a IT Project. This is done in order to learn Grails. I have some problem to start with :

In my sample app, a user has many tasks, belongs to a project, have many holiday status, belongs to the Resource planning and thats it. (kind of requirements!)

Now..... When it comes to domain modeling, how I actually model this? I came up the solution of modeling something like this :

class User { 
        //relationships. . . . 
        static belongsTo = [ company : Company, role : Role, resource : Resource] 
        static hasMany = [ holidays : Holiday, tasks : Tasks ] 
        Profile profile 
        Project project 
        String login 
        String password 
        static constraints = { 
                login(unique:true,size:6..15) 
                profile(nullable:true) 
        } 
        String toString() { 
                this.login 
        } 
} 

Now taking advantage of Grails scaffolding. I generated the view, hmmm well thats where I got struck!

With this model in place, clearly when creating a new User., I need to give away project details, resource details. Even though I can change the view as I need, for example I need only two fields say login and password for the new User to register my site. But as per data modeling, how I can handle other fields like Profile, Project and other relationships. This sounds complicated! I know i'm a newbie to both web development and I want your suggestions and how do I proceed with this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-12-29 23:54:34

您需要覆盖控制器中的 save 操作并填写其中的其他字段。

尝试在 UserController 中添加以下代码:

def save = {
    def userInstance = User.get(params.id)
    if (!userInstance) {
        userInstance = new User()

        // here you can fill in additional fields:
        userInstance.profile = myMethodToGetTheProfile()
        userInstance.project = myMethodToGetTheProject()
        ...
    }
    userInstance.properties = params
    if (userInstance.save(flush: true)) {
        flash.message = message(code: 'default.created.message', args: [message(code: 'User.propertyName.label', default: 'User'), userInstance.id])}
        redirect(action: session.lastActionName ?: "list", controller: session.lastControllerName ?: controllerName)
    } else {
        render(view: "form", model: [userInstance: userInstance, mode: 'edit'])
    }
}

获取默认项目的方法的实现取决于您的应用程序的逻辑。

You need to override the save action in your controller and fill those additional fields there.

Try adding the following code in UserController:

def save = {
    def userInstance = User.get(params.id)
    if (!userInstance) {
        userInstance = new User()

        // here you can fill in additional fields:
        userInstance.profile = myMethodToGetTheProfile()
        userInstance.project = myMethodToGetTheProject()
        ...
    }
    userInstance.properties = params
    if (userInstance.save(flush: true)) {
        flash.message = message(code: 'default.created.message', args: [message(code: 'User.propertyName.label', default: 'User'), userInstance.id])}
        redirect(action: session.lastActionName ?: "list", controller: session.lastControllerName ?: controllerName)
    } else {
        render(view: "form", model: [userInstance: userInstance, mode: 'edit'])
    }
}

The implementation of methods to get the default project depends on your app's logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文