Experiences in hierarchical development of VB. NET [reprinted]

Source: Internet
Author: User

With Microsoft. the launch of the NET development platform has undoubtedly brought great news to countless VB developers, including no inheritance, ugly error capturing, no object pool, and no underlying development, it is followed by a complete OO-oriented feature, a wide range of class libraries that support XML. When someone prefers C #, C ++ developers are dismissive about it, but what is more important in software development is an idea and principle. Language is just a tool.

I have the honor to participate in an ASP. NET project based on a three-tier architecture and have some experiences. I will share some of my views on some issues encountered during the development process. The architecture diagram is as follows:

 

  I. Implementation of the data Entity layer (Entity)

We need to solve the problem of data representation first. In VB development, I believe everyone has encountered the problem of how to effectively construct data entities, a single class, a collection class, how to operate a single class in the Collection class and how to fill data entities are not easy to solve. There are also many methods to implement these problems. For the implementation of the Collection class, you can use arrays, you can use a collection object or a dictionary object. Of course, there are also many ways to instantiate a data object, such as the most common factory mode, which will not be discussed here, I use TYPED-DATASET objects as data entities in projects. I personally think the following benefits:

1. Data Binding. You can bind controls directly, especially grid controls. This is very difficult in VB. Unless you add a property to store Recordset, you need to add more methods to initialize Recordset, in addition, it increases the cost of Instantiation and sending.

2. The code is automatically generated. Write less code, especially when there are many fields in the table, and the serialization function is automatically provided.

3. Collection class. Typed-Dataset is a collection class. It provides methods for adding, deleting, modifying, and searching for a single class.

Of course, there are also disadvantages. Nothing can be perfect. For example, it is difficult to assign child classes from them, and the instantiation cost is high.

When Typed-Dataset is automatically generated, you need to make some changes to make Entity better meet the OO features.

A. automatically generated table objects and Row objects end with DataTable and Row objects. This can be changed by adding the following code: add the <XML: schema node in the xml file:

Xmlns: msdata = "urn: schemas-microsoft-com: xml-msdata" xmlns: codegen = "urn: schemas-microsoft-com: xml-mspdrop" namespace

B. Rewrite the <xs: choice maxOccurs = "unbounded"> <xs: element name = "Tables"> node <xs: element name = "Tables" codegen: typedName = "Table" codegen: typedPlural = "Tables">

C. Because the automatically generated code reports an error when a field is NULL by default. If you do not want to report an error, add: nillable = "true" codegen: to the definition of each Element node: nullValue = "_ null" or nillable = "true" codegen: nullValue = "false" or nillable = "true" codegen: nullValue = "1900-01-01T00: 00: 00"

D. the granularity of Entity. When considering the design of object objects, "Object granularity" is a matter that needs to be carefully considered. I personally like coarse-grained Entity, which can reduce the number of round trips with the database, of course, it does not contain all the tables, which is inefficient. For example, the Order only needs the Order and OrderDetail tables, Product and SalesPerson, of course, you can also use the custom classes inherited from dataset as the Entity, but I personally think it is not necessary in general. After all, MS has more typed-dataset functions, it is also complicated to implement.

 

 2. Data Service)

You can download the Data Access Application Block of MS at the MS site.

  3. Data Access)

All data operations are implemented using the traditional Stored Procedure (SP). However, since Entity is a set and contains multiple rows, it is necessary to call SP cyclically for batch operations, you are advised to provide two methods: one is to operate one row (commonly used) and the other is to operate multiple rows (not commonly used ).

  4. Business Logics)

The functions to be completed are the implementation of various business rules and logic, inherited from SevicedComponent. This is the only layer on the interface layer that needs to be clearly understood, and also the areas for system expansion and frequent changes in the future, therefore, each module in the business layer has a base class, Module1BaseClass, such as the document module. The structure is as follows:

Abstract Class OrderBase

Public Function AddNew ()

Public Function Delete

Public Function GetOrderByID

Public Function Update

End class

 

Public Class SaleOrder Inherits OrderBase

...............

End Class

 

Public Class PurchaseOrder Inherits OrderBase

.........

End Class

 

Public Class MorePurchaseOrder Inherits PurchaseOrder

.........

End Class

In this way, the scalability of the business layer is strong, which is the benefit of OO. If it is in VB, it is very difficult to implement. Of course, it can be indirectly implemented through interfaces, but it is not convenient, after all, VB6 is not an OO-oriented language, but an Interface-oriented language.

  V. Facade layer

Provides high-level business logic processing. Generally, when an operation on the presentation layer needs to be linked to multiple modules, this layer provides a unified interface, however, the presentation layer is not required to call this layer to process the business logic.

 

  Vi. Transaction Processing)

Manual transaction processing is adopted. Because all business layers are Serviced component, we can directly use the transaction function in COM +, although Serviced component has a performance loss, however, the distributed structure still brings a lot of convenience. I set the transaction attribute of all business layers to Required. The specific code is as follows:

<Description ("Contains methods for inserting and updating product records in the berp system. "), Guid (" 4FE02C4F-C8FA-497c-9A9E-EDA0877B772C "), Transaction (TransactionOption. supported), Synchronization (SynchronizationOption. required), JustInTimeActivation (True)>

Public Class Product

Public Function Add () As Boolean

'............

If ContextUtil. IsInTransaction Then

ContextUtil. SetComplete ()

Else

ContextUtil. SetAbort ()

End If

'............

End Function

End Class

Other layers in the system do not need to be Serviced component

 

  VII. Exception Handling

It completely changed the error handling method in VB6, which I like most! There are two types of project exceptions: Business exceptions and system exceptions:

1. Business Exception:

Inherited from ApplicationException, there is a total Exception, and the Exception of each module is inherited from the total Exception, thus forming a hierarchy:

ApplicationException

ProjectException

Module1Exception

Module1Concret1Exception

Module2Concret2Exception

...............

Module2Exception

...............

2. system exceptions and errors reported, including database errors:

The exception capture principle is to capture only the expected errors, because the material does not need to Catch all the errors at the data access layer and business layer, as shown below:

Public Function Methoda ()

IF ...... THEN

If ContextUtil. IsInTransaction Then

ContextUtil. etAbort ()

End If

Throw Module1Concret1Exception

End If

......

Catch the specific error Module2Concret1Exception

'Catch ProjectException is not required here.

End Function

To facilitate debugging, You need to record system exceptions in the log file. Here, the Exception Management Application Block provided by MS is used for implementation. For specific implementation methods, see MSDN, which is:

Http: www. icrosoft. om/downloads/details. spx? FamilyId = 8CA8EB6E-6F4A-43DF-ADEB-8F22CA173E02 & displaylang = en

There are two display methods for displaying error information on the interface. One is to display service exceptions, and the other is to display system exceptions. The Code is as follows:

Public Sub Button_OnClick ()

Dim objblModule As business layer

Try

ObjblModul. osomething ()

Catch ProjectExcption

'Display a custom page ()

Catch Exception

'Call Exception Management. Publish method to log the exception

'Display another custom page. The custom page can send specific error messages to the administrator, just like the windows Error Page.

End Try

End Sub

  8. Permission Control

This is an issue worth further discussion. I use FORM authentication. The specific user information and permissions are stored in the database, and WINDOWS domain authentication is not integrated, when implemented, a special layer is used to execute permission judgment and GeneralPrincipal and GeneralIdentity objects. The code snippets are as follows:

 

Public Function CheckRole (ByVal strRole As String) As Boolean

Return privateUserPrincipal. IsInRole (strRole)

End Function

 

Private Sub InitPrincipal ()

Try

PrivateUserIdentity = New GenericIdentity (privateUserName)

PrivateUserPrincipal = New GenericPrincipal (privateUserIdentity, privateUserRoles)

Catch e As Exception

Throw New Exception ("an error occurred setting credentials ")

End Try

End Sub

 

Private Sub SavePrincipal ()

Try

If Not IsNothing (_ context) Then

Context. Session ("UserName") = privateUserIdentity. Name

Context. Session ("Roles") = privateUserRoles

Context. User = privateUserPrincipal

End If

Catch e As Exception

Throw e

End Try

End Sub

In this way, when the interface is changed to a windows form, you do not need to change a lot of code. In addition, to solve the latency problem caused by putting permissions in the SESSION, I put the user's permission information in the XML file of the server, and then directly retrieve data from the XML file. Any modification to the user information will change the corresponding XML file, this is more efficient than obtaining from the database.

Of course, you will encounter many other problems in actual development, such as reports, printing, and concurrency. If you are interested, please contact me. If you have any personal level limitations, please include them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.