如何在三层解决方案中使用抽象类?
我面临的直接问题是,目前我有一个 3 层解决方案(Presentation.aspx.vb 调用 BusinessLayer.vb,BusinessLayer.vb 调用 DataAccessLayer.vb)。但是,我想创建 BusinessLayer 和 DataAccessLayer.vb 抽象类,因为多个 Webform 将使用具有相同的功能。
所以我目前正在这样做(没有抽象类):
'Presentation Layer (pseudocode)
public sub checkUser(byval userName as string, byval dept as string)
dim isGood as boolean = BL.checkUserAccess(userName, dept)
'some more code
'change properties of webcontrols, etc
end sub
'Business Layer (pseudocode)
public function checkUserAccess(byval name as string, byval group as string) as boolean
dim accessObject as dataObject = DAL.checkPermissions(name, group)
if accessObject.isHighAccess then
'some code
else
'some other code
end if
end function
'Data Access Layer (pseudocode)
public function checkPermissions(byval userid as string, byval section as string) as dataObject
'some code
end function
但是,如果我添加抽象类,我仍然可以拥有这个结构吗?
例如:
'Presentation Layer (pseudocode)
public sub checkUser(byval userName as string, byval dept as string)
dim isGood as boolean = instOne_BL.checkUserAccess(userName, dept)
'some more code
'change properties of webcontrols, etc
end sub
'Business Layer (pseudocode)
public class instOne_BL inhertis BL
public function checkUserAccess(byval name as string, byval group as string) as boolean
base.checkUserAccess(name, group)
instOne_DAL.checkPermissions(name, group)
end function
end class
'Data Access Layer (pseudocode)
public class instOne_DAL inherits DAL
public function checkPermissions(byval userid as string, byval section as string) as dataObject
base.checkPermissions(userid, section)
end function
end class
My immediate issue is that currently I have a 3 tier solution (Presentation.aspx.vb calls BusinessLayer.vb which calls DataAccessLayer.vb). However, I want to make BusinessLayer and DataAccessLayer.vb abstract classes because several Webforms will use have the same functionality.
So I currently am doing this (no abstract classes):
'Presentation Layer (pseudocode)
public sub checkUser(byval userName as string, byval dept as string)
dim isGood as boolean = BL.checkUserAccess(userName, dept)
'some more code
'change properties of webcontrols, etc
end sub
'Business Layer (pseudocode)
public function checkUserAccess(byval name as string, byval group as string) as boolean
dim accessObject as dataObject = DAL.checkPermissions(name, group)
if accessObject.isHighAccess then
'some code
else
'some other code
end if
end function
'Data Access Layer (pseudocode)
public function checkPermissions(byval userid as string, byval section as string) as dataObject
'some code
end function
However can I still have this structure if I add abstract classes?
For instance:
'Presentation Layer (pseudocode)
public sub checkUser(byval userName as string, byval dept as string)
dim isGood as boolean = instOne_BL.checkUserAccess(userName, dept)
'some more code
'change properties of webcontrols, etc
end sub
'Business Layer (pseudocode)
public class instOne_BL inhertis BL
public function checkUserAccess(byval name as string, byval group as string) as boolean
base.checkUserAccess(name, group)
instOne_DAL.checkPermissions(name, group)
end function
end class
'Data Access Layer (pseudocode)
public class instOne_DAL inherits DAL
public function checkPermissions(byval userid as string, byval section as string) as dataObject
base.checkPermissions(userid, section)
end function
end class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用.Net remoting作为各层之间的通信层,那么这根本没有问题(就像调用当前层中另一个类中的方法一样)。
如果你使用WCF,那么这也可以工作,购买时你需要多做一点工作。基/抽象类必须使用您希望传递的所有继承类的 KnownType 属性进行修饰。
如果您使用网络服务,那么如果不玩一些游戏,这是不可能的。例如,您需要使用二进制序列化器将对象序列化为字符串,然后在另一端将其反序列化。
If you use .Net remoting as the communication layer between the layers, then this is no problem at all (it's just like calling a method in another class within the current layer).
If you use WCF, then this will also work, buy you need to do a little more work. The base/abstract class must be decorated with KnownType attributes of all inheriting classes that you expect to pass.
If you use web services, then this is not possible without playing some games. For example, you would need to serialize the object into a string using the binary serializer and then deserialize it on the other end.