如何在3层解决方案中使用抽象类?

时间:2012-02-01 15:49:12

标签: asp.net vb.net .net-3.5 abstract-class 3-tier

我的当前问题是目前我有一个3层解决方案(Presentation.aspx.vb调用BusinessLayer.vb调用DataAccessLayer.vb)。但是,我想制作BusinessLayer和DataAccessLayer.vb抽象类,因为几个Webforms将使用具有相同的功能。

所以我目前正在做这个(没有抽象类):

'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

1 个答案:

答案 0 :(得分:1)

如果使用.Net远程处理作为层之间的通信层,那么这根本不是问题(就像调用当前层中另一个类中的方法一样)。

如果你使用WCF,那么这也会起作用,买你需要多做一点工作。必须使用您希望传递的所有继承类的KnownType属性来修饰基类/抽象类。

如果您使用网络服务,那么如果不玩一些游戏就无法做到这一点。例如,您需要使用二进制序列化器将对象序列化为字符串,然后在另一端对其进行反序列化。