C # Use the factory mode, reflection mechanism, and cache mechanism to design interfaces at the data access layer

Source: Internet
Author: User

In our C # tutorial, we will also talk aboutC #Design ModeBut the C # design model involves a wide range of knowledge. We will use the topic to explain in depth the design model in the C # language system. This article focuses on how to better create a data access layer (there are several related articles in the ASP. NET tutorial.Article).

 

Factory ModelIt means something similar to a factory. You only need to get the factory items, but you don't know how to make them. TakeProgramFor example, you define a class by yourself. There are many methods in this class, and these methods are used to create objects. People who use this class only need to know which method to call to obtain the specific object, you don't need to worry about how to get it. The factory mode is frequently used in ASP. NET tutorials. You can search for it on this site.

Reflection mechanismIn fact, it is also very simple. It is a class. You can use the reflection mechanism to obtain how many methods, attributes, and method names you have. At this time, you can determine whether this class has this attribute or this method. This method is called. For the reflection mechanism of C #, we will explain it in a special article. We put it in the C # tutorial and. Net 3.5 topic classification.

Cache MechanismThe main function is to cache some data. I don't know what effect he has. But the cache mechanism is to initiate a cache, and then directly retrieve data from the cache when the next call is made. This makes sense for your IE browser to browse the Web page.

Dynamically create data access layer objects, that is, create some interfaces for programming. More flexible. The implementation of specific interfaces is implemented in the factory mode..

 

The example project dalfactory is designed in the factory mode. There are many books on the design mode. I have also read "C # design mode" (the original English project is C # design patterns: A tutorial), which is not profound in understanding, but familiar with the factory mode. You can use it to return different instance objects as needed, and use the reflection mechanism in the dalfactory project to implement dependency injection, of course, its implementation can be more flexible and powerful, and interested friends can study it.

PartCodeAs follows:

// <Summary>
/// Create the Dal in the abstract factory mode.
/// Configure web. config: (use factory mode + reflection mechanism + cache mechanism to dynamically create different Data Layer object interface )
/// The datacache class is in the export code folder.
/// You can create all the Dal classes in this dataaccess class.
/// <Deleetask>
/// <Add key = "Dal" value = "smssystem. sqlserverdal"/> (The namespace here is changed to the namespace of your project according to the actual situation)
/// </Appsettings>
/// </Summary>
Public sealed class dataaccess
{
Private Static readonly string Path = configurationmanager. deleettings ["Dal"];
/// <Summary>
/// Create an object or obtain it from the cache
/// </Summary>
Public static object Createobject (string path, string cachekey)
{
Object objtype = datacache. getcache (cachekey); // read from the cache
If (objtype = NULL)
{
Try
{
// Assembly ass = new assembly ();
Objtype = assembly. Load (PATH). createinstance (cachekey); // create a reflection
Datacache. setcache (cachekey, objtype); // write Cache
}
Catch (system. Exception ex)
{
String STR = ex. Message ;//
Smssystem. Utility. savelog. saveinfotolog (STR, "errorlog", "exception ");
}
}
Return objtype;
}
/// <Summary>
/// Create an object without using the cache
/// </Summary>
Private Static object createobjectnocache (string path, string cachekey)
{
Try
{
Object objtype = assembly. Load (PATH). createinstance (cachekey );
Return objtype;
}
Catch // (system. Exception ex)
{
// String STR = ex. Message; // record the Error Log
Return NULL;
}
}
/// <Summary>
/// Create the custemployee data layer interface
/// </Summary>
Public static smssystem. idal. icustemployee createcustemployee ()
{
String cachekey = path + ". custemployee ";
Object objtype = Createobject (path, cachekey );
Return (icustemployee) objtype;
}
.................. (Other data layer interfaces)
}

This class can be used to obtain the data access layer instance. However, I always throw an exception during usage. [system. io. filenotfoundexception] = {"failed to Load file or assembly" smssystem. sqlserverdal "or one of its dependencies. The system cannot find the specified file. ":" Smssystem. sqlserverdal "} To be honest, I am still born with C # troubleshooting and debugging. Why is it abnormal when I use reflection? I went up to check the reflected IPA, I feel that my usage is correct. Besides, I think the petshop example is similar to this. Why can it be used? I have been debugging for a long time in the afternoon, I tried to compare it with petshop, and finally found that its DLL name is a bit strange. Unlike me, it is like petshop. sqlserverdal. DLL, and I am a sqlserverdal. DLL, I think it is very likely that there is a problem here, because in WB. I have configured

 

<Deleetask>
<Add key = "Dal" value = "smssystem. sqlserverdal"/>
............
</Appsettings>

After a long time for such an assembly, ask someone else and find out that it is necessary to set the Assembly name in the properties of the project to generate a DLL file such as smssystem. sqlserverdal. dll.


Setting and re-generating. Another problem occurs,

The type "smssystem. BLL. custemployee" also exists in "C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727 \ temporary ASP. NET files"
\ Smssystem \ 3358d 7f 2 \ 16ba9bb7 \ Assembly \ dl3 \ 7296d5df \ 40ded961_96d 8C 601 \ BLL. dll"
And "c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ temporary ASP. NET files \ smssystem \ 3358d 7f 2
\ 16ba9bb7 \ Assembly \ dl3 \ 644d 894c \ 4cf 8840a _ 9ad 8C 601 \ smssystem. BLL. dll"
Medium G: \ ASP. NET \ smssystem \ default. aspx. CS 22

I will take a closer look at the original sqlserverdal under the BIN OF THE WEB Project. the DLL is still present, and an smssystem is added. sqlserverdal. DLL, haha, another unused file that is not automatically deleted by vs2005. Yesterday it was a project file assemblyinfo. after CS is deleted, the corresponding deleted file is not deleted. However, I manually deleted it. OK, everything is normal, I would like to thank my friend who told me how to change the project's Assembly name. Otherwise, I have been looking for a long time and have no idea how to generate a DLL with a namespace.

 

Source: eefly's blog http://blog.sina.com.cn/eefly

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.