Talking from Grails service layer

| |
[阴 2009/04/13 09:51 | by xeric ]
One day, some friend ask me, why grails so looks like the rails, but there's a service layer in this framework.
It's a common question asked by buddy from rails framework.
I'm confused also when just got stating in grails.
I decide to talk about this topic in article, I think most of the idea is just came from myself, may be it's not alright, I just hope it can resolve some confuse and question from newer.
The most different between grails and rails, I think it's focus on grails is domain-Driven, this is came from some special area such as UML and BA. Programmers always not care about the UML and BA in most project. but they are always received some information about domain, entity, model, role, behavior, wow, it's so much nouns, hard to understand, no problem. I talk about them just since they are relative with the DDD.
Some buddies from rails framework is familiar with rich model object, right? That's important concept in rails model layer, we are always like witting model-related logic in model, it's different from java or ejb, an anemia model object language, But are you confused when you see thousands lines of code in the models? Are you feeling why it so far to get ending of the code when you have read the model's code for 1 hour?
Grails choose an standpoint between grails and ejb model, they choose DDD.
It's so familiar with the rails framework when you just get starting with grails, model properties, model constraints and model mapping such as belongsTo and hasMany.
But when you want to add some business logic, problem came out.
Where to add logic? In rails user opinion, coding in domain model is conversion, but why there's a service layer in grails, if we write logic into service layer, how to explain the constraints and validation in domain objects?
It looks like some important  concept in UML, domain-entity-role-behavior.
Example: in a pet store. We have three domain, store, pets, customers.
store has-may pets, customer can buy pets and also has-many pets.
the constraints is pets can not have no store but can null in owner(Customer) object.
the pets' price can not little than zero.
The customer must have non-null name and they must have enough money to buy pets.
the constraints is the meta information of domain, so this logic add to domain class.
when this domain get a instance in back-end, we can call them entity. entity can be described with the relation between each others, and entity has validation them self.
exclude entity concept, the customer, pets and store are also roles, why?
They have behavior, customer have behavior to buy, to pay and to complain.
The pets can shout, and the store can publish pet information.
Ok, where to put behavior code? Not controller layer, not domain layer or not script SRC in grails also. just in the service layer.
More advantage? Yes, service layer can inject into everywhere in the grails framework, so ,if someone want to reuse logic in other place, go ahead.
Service layer can be as an open service API, in web service.
Example, store and customer must login when store want to publish information and customer want buy pet, It's the same logic when auth them, username + password [+ verification code].
You can just write a LoginService in application and inject into store and customer domain(controller) to use.
And when you just want to open a auth API for user from another system, you can use this service as an open API(webservice).
After all, we have some idea when we choosing where to put the different logic, right?
After all, believe and trust DRY concept when you use this Agile Web Framework, that's the most important you must to remember!
Tags: , ,
开发[DEV] | 评论(1) | 引用(0) | 阅读(669)
阳朔住宿 Email Homepage
2009/12/02 15:52
象眼岩 位于象鼻山山腰南北两侧, 互相贯通, 高约2米, 宽5~10米, 长52.8米, 呈扁长形, 面积约274平方米。象眼岩原是一段不长的古地下河道, 形成年代比水月洞早, 因部位恰巧在象眼处而得名。从象山南麓南极洞阳朔酒店口, 沿石级可登上南眼。出象眼岩, 盘山道可达山顶。北眼悬崖峭壁, 无磴可通。象眼岩南北两洞, 是一对眺望风景的绝妙窗口。从左眼张望, 高耸的漓江饭店近在咫尺, 城区楼宇街市鳞次栉比, 半城山芭历历在目; 从右眼远眺, 江波帆影, 闪烁隐现于訾洲、塔山、穿山之间。
分页: 1/1 第一页 [1] 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 请输入左侧的字母,不区分大小写