Practical .NET Data Access Layer - 12

zhaozj2021-02-16  52

It is easy to see from this Dalbase that the support of Framework Level is mainly concentrated.

This is almost the place in Cache Management and Distributed Process

There is a real issue that Data Access Logic has to consider (possibly in actual projects,

Data Access Logic Level's distributed process needs will not be a lot, most

All solved directly in Business Log)!

Let's take a look at how to make a real Data Access logic.

Convenient J

Code 10: Building your own Data Access Logic

// mydal: Provides data access support required for the current application, inheriting from DALBASE

Public Class Mydal: Dalbase

{

Public mydal () {}

}

// Customerdal_adox: Provides support for data access using traditional ADO.NET, inheriting from mydal

Class Customerdal_adox: Mydal

{

Public Customerdal_adox () {}

Protected Internal MyCustomETMER getCustomerbyid (String Strid)

{...}

Protected InterNal Void UpdateCustomer (MyCustomer Cust)

{...}

...

}

// Customerdal_orm: Provide support for data access using O / R mapping, inheriting from mydal

Class Customerdal_orm: Mydal

{

Public Customerdal_orm () {}

Protected Internal MycustomETAllCustomers ()

{...}

Protected InterNal Void UpdateCustomers (MyCustomer Cust)

{...}

...

}

Mydal in the above code did not really realize what operations, setting up for expansion, users can also be routed in this layer like DAF, directly let

Customerdal_adox or Customerdal_orm inherits from Dalbase, which will make

Our system structure is more simple and clear.

From the above code, we can easily discover all Data Access Logic

The method is all declared as protected internal, why?

Of course, because of "hateful" DAF! The cost of interface consistency is here

It is reflected in it (it's evil, the reason why so many days of spending, it is here.

What is the shortcomings of DAF behind?

Although it is "deprived" to directly order the Data Access Logic method directly in the code.

Feel, but if you really need this to do this (strongly do not recommend J), there is still the following 3

The way (of course, the author is very do not want you to put DAF into the cold palace.

After all, I also spent a lot of my heart and a big propaganda. J):

(1) Compile your access code to Data Access Logic into an Assembly, which, magical intencers will make you think (DAF, Data Access Logic with Data Access module, generally scaled an assembly compilation, so There is natural magic j)! The cost of paying is: You have to put business logic (or other call module) with Data Access in the same layer L, lost a well-designed system whose layer and flexibility! (2) INTERNAL is one of the three most commonly used .NET features (reminder: Do not abuse L), another weapon is of course our reflection!

OK, you don't have to shoot yourself, the system has already prepared Helper for you to enjoy:

Public Static Object InvokeMethod

Type Type, String Method, Object [] Paramsvalue

(3) If the method is not easy to use by itching or the suspicion, it only has his own horses. I believe that you have already been cooked to Reflection, and you can easily get it. (However, the author should remind One sentence: Don't abuse! Protected design is very clear, cautious L)!

Said so much, or a word: use Daf, it (also) will make you happy

J!

However, there is a problem that needs to be explained to you: here, why didn't the author?

Use Factory Pattern to construct different Data Access Logic implementations (not used

Factory's price is to provide a large number of configuration information to the Method level, it is indeed a bit

Trouble L)?

This is mainly based on 2 considerations:

(1) Data Access Logic does not necessarily abide by the principle of consistency, Data Entity is not the same (for legic Data Access Logic code, even the parameters may have differences), in this case, define a generic interface Something difficult;

(2) Not every Data Access Logic implements all the functions required by DAF (such as: The above code 10 is to construct all the functions of data access to Customer for data access by Customerdal_adox with Customerdal_orm. Imagine that if it adopts Factory, isn't it "with me" Dongdong to be forced to accept the whole, even if you write a space method, how to achieve true call to other Data Access Logic (write like this Factory is really difficult?

Next paragraph: http://www.9cbs.net/develop/read_article.asp? Id = 27555

转载请注明原文地址:https://www.9cbs.com/read-24096.html

New Post(0)