.NET分层架构中的一个问题

acqy 2007-05-21 09:51:58
通常情况下,数据访问层只管数据的CRUD操作,而业务逻辑层使用数据访问层提供的类和方法实现业务逻辑。我的数据持久层采用NHibernate,例如,某个用户tester创建并post了一个blog,我是这样做的:

public void PostBlog()
{
using (ISession session = NHibernateHelper.GetCurrentSession())
{
User user = new User();
user.Name = "tester";

Blog blog = new Blog();
blog.Title = "blogTitle";
blog.Content = "blogContent";
blog.User = user;

user.Blogs.Add(blog);

session.Save(user);
session.Save(blog);
session.Flush();
}
}

从表面上看,上面的业务逻辑很简单,但是,这种做法明显的把数据库操作以及数据库事务处理放在了业务层,明显不符合分层必须解耦的思想。那么,这个数据访问层和数据持久层应该如何定义呢?他们的关系是怎样的呢?
...全文
870 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolinpeng 2007-06-05
  • 打赏
  • 举报
回复
本想再多说几句,想想还是算了!
我只想请问: layer architecture, dependency injection, spring.net 干什么用?
acqy 2007-06-02
  • 打赏
  • 举报
回复
amandag(高歌) 兄弟,您是两颗星的高手,能否给点建议啊?
多谢了!
Jinglecat 2007-06-02
  • 打赏
  • 举报
回复
都是高论,各有建娣,我同样有 LZ 的困惑
amandag 2007-06-02
  • 打赏
  • 举报
回复
mark
bonhomme 2007-06-01
  • 打赏
  • 举报
回复
to gg_zg(),

你说的意思我很赞同。

从另一个角度看,楼主希望把操作NHibernate的代码进一步分离出来也有他的道理,尽管NHibernate已经是数据库中立的了,这里已经没有赤裸裸的SQL语句了。分离出来,业务逻辑层甚至可以独立于NHibernate,例如可以使用其他的ORM引擎,或者再回到直接使用SQL的路子上,而业务逻辑层都可以保持不变。不过这样切换ORM引擎的必要性应该是微乎其微。

分层过多或过少,如何权衡,是一个需要综合考虑的问题。
acqy 2007-06-01
  • 打赏
  • 举报
回复
大家讨论得非常激烈,但也希望各位能够心平气和的讨论问题,毕竟我们都是做学术的。
对于是否使用remoting,我开始打算使用,毕竟可以在应用服务器负载增大的时候,能够通过remoting将某些业务逻辑分布出去,分摊服务器压力。但我目前可能会暂时放弃这个想法,因为只要业务层能够做到解耦,remoting只不过是一种形式,到时候改成remoting就是了。
另外我认为,数据持久层可以看成是一种系统与数据库打交道的facade,这个facade提供给数据访问层使用。因此,NHibernate是数据持久层,当然,我们也可以直接使用那些Connection, Command对象和SQL语句来实现自己的数据持久层;而数据访问层则是使用这个facade来实现数据对象的添加、删除、修改、查询等等基本操作,这些操作是业务无关的,当然数据访问层更不会去考虑,现在使用的是sql server数据库,还是oracle数据库,因为这些东西都被数据持久层封装了。立于数据访问层之上的,就是业务逻辑了,业务逻辑应该体现具体的业务流程和事务处理,并使用数据访问层来处理数据。
现在的问题是,在.net中用一种什么样的结构,或者模式,来组织这些对象,使得层与层的耦合最小呢?
以上只是本人的考虑,请高人指教!
gg_zg 2007-06-01
  • 打赏
  • 举报
回复
那个 xiaolinpeng() 搞笑的很,加个接口,用了个所谓设计模式。

哈哈。其实是多此一举!

回楼主:
数据操作和数据事务处理允许放在逻辑层。况且你并无针对数据库的操作。

逻辑层不操作数据操作什么???
bonhomme 2007-05-31
  • 打赏
  • 举报
回复
To Xiaolipeng, who claims to be a software architect in so-called US companies:

I don't think you're talking about anything relevant to the point people discusing here in this thread, by bragging about your soundbite-style "theories" regarding interfaces, which everyone knows, understands, and uses, perhaps even better :) Pardon my straightforwardness.

No further responses from you will be replied.
还是说国语吧,哥们。
bonhomme 2007-05-31
  • 打赏
  • 举报
回复
to gameboy766(古巴):
对不起,别关注了,我上一帖已经说了,不继续回复那个xiaolinpeng()了。因为,就技术层面,已经没有什么可以争论或讨论的了。如果有人有兴趣,可以邀请他把他的帖子如实翻译成国语(或如他所坚称,中文),继续和他从interface的定义开始讨论下去,我技术太浅,就不奉陪了。
gameboy766 2007-05-31
  • 打赏
  • 举报
回复
关注
xiaolinpeng 2007-05-31
  • 打赏
  • 举报
回复
Bonhomme, "国语"是台湾叫的. 我们叫"中文".
没有写中文, 是因为输入太慢. 还有一些技术术语, 本人用的不准确.
如果我们想真正做技术讨论, 我们可以继续. 如果是"抬杠""掐架", 恕不奉陪.
xiaolinpeng 2007-05-30
  • 打赏
  • 举报
回复
to bonhomme:
Your comments "换了汤,并未换药!尽管你使用了接口,但没有任何实质意义" obviously showed that you did not understand me.

I thought you would understand when I hinted you the use of interface.

It looks like I, as a software architect in US companies, need to explain a little more to you.

the interface is just a binding contract. The implementation of the interface can be in a separate DLL, and this DLL can be loaded even in a remote machine if client calls it through remoting.

In this way, the client is completely de-coupled with the service "BlogSvc".

If you still don't get me, then sorry, you have to read more.

bonhomme 2007-05-26
  • 打赏
  • 举报
回复
我有Java Persistence with Hibernate的英文电子版,如有需要,可以致信cbxia@sina.com索取。
bonhomme 2007-05-26
  • 打赏
  • 举报
回复
to Xiao LinPeng:
发贴人提出的问题很有意义,但你给出的方案有明显缺陷。看上去,你实现了业务逻辑层和数据访问层的分离,其实没有!因为你的PostBlog()方法本身没有实现任何业务逻辑,只是把调用原封不动地重定向到“你的”数据访问层BlogSvc,从而使得BlogSvc成了包含了具体数据库访问逻辑的业务逻辑层,换了汤,并未换药!尽管你使用了接口,但没有任何实质意义。

一个值得考虑的方案是DAO模式。可以参考Java Persistence with Hibernate一书第十六章关于DAO的论述。
liubin911 2007-05-25
  • 打赏
  • 举报
回复
up
xiaolinpeng 2007-05-22
  • 打赏
  • 举报
回复
这样如何?这里只给想法,用到了design pattern!

public interface IBlogSvc
{
void CreateBlog(whatever argument);
}

//implement this service in another class, it could be in another DLL
public class BlogSvc : IBlgSvc
{
public CreateBlog(whatever argument)
{
using (ISession session = NHibernateHelper.GetCurrentSession())
{
User user = new User();
user.Name = "tester";

Blog blog = new Blog();
blog.Title = "blogTitle";
blog.Content = "blogContent";
blog.User = user;

user.Blogs.Add(blog);

session.Save(user);
session.Save(blog);
session.Flush();
}

}
}

public class WhateverYourClass
{
//you can use spring.net here if you want IBlogSvc to be pluggable
pulbic IBlogSvc BlogSvc
{
get;
set;
}

public void PostBlog()
{
BlogSvc.CreateBlog(argument);
}

}

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧