Application reflection + configuration file + Abstract Factory errors and Cause Analysis

Source: Internet
Author: User

Let's take a look at a program:

<PRE name = "code" class = "VB"> imports system. configuration 'configuration file namespace imports system. reflection 'reflection namespace imports idal' references the interface layer public class dataaccess' Assembly name (also representing the namespace name): Dal private shared readonly assemblyname as string = system. configuration. configurationmanager. appsettings ("assname") 'database type: SQL private shared readonly dB as string = system. configuration. configurationmanager. appsettings ("DB") 'creates an instance of the D-layer class sqluserinfodal public shared function createuserinfo () as idal. iuserinfo dim daluserinfoname as string = assemblyname &". "& dB &" userinfodal "'name of the D-layer class to be instantiated return ctype (assembly. load (assemblyname ). createinstance (daluserinfoname), iuserinfo) end Function


For those who have learned how to access data through reflection, configuration files, and abstract factories, this short program is easy to understand. I used this program in the personal Rebuild of the charging system of the IDC room. :

However, an error is reported during running:

Failed to Load file or assembly "Dal" or one of its dependencies. The system cannot find the specified file.

By checking the information, we know a solution:Copy the Dal. dll file of the Dal layer to the corresponding directory of the UI Layer.,.

Then, the program runs normally and the sqluserinfodal instance is created through createuserinfo.

With successful experience, I did the following:

  • In the Dal layer, a new class is added: sqlworklogdal
  • In dataaccess, a new method is added: createworklog ()

As follows:

'Create an instance of the D-layer class sqlworklogdal public shared function createworklog () as idal. iworklog dim dalworklogname as string = assemblyname &". "& dB &" worklogdal "'name of the D-layer class to be instantiated return ctype (assembly. load (assemblyname ). createinstance (dalworklogname), iworklog) end Function

However, an error is reported after running, but this error is different:

You have not set the object reference to the instance of the object.

After checking the information, I also found the answer:Modify the Dal layer -- properties -- compile -- generate the output path, and change the generated output path to the UI Layer \ bin \ debug folder.

Although the two solutions are different, they are essentiallyPlace the Dal. dll file in the Dal layer to the UI Layer..

So why is the first solution only "short-term effect", while the second solution can "cure?

At this time, I made an attempt. Instead of modifying the path, I copied the Dal. dll file in the Dal layer again and replaced the file I copied to the UI Layer.

The result is successful.

This shows that the two copied files are not the same. Although they are all named Dal. dll, they must have changed within the file.

Here we will talk about how the DLL file is generated.

Each assembly has two folders: Bin and obj,


The bin directory is used to save the Assembly generated by the project, and the OBJ directory is used to save the compilation result. After compilation, the file is merged into a. dllor. EXE file and saved to the bin directory. And useIncremental compilation.

Therefore, after I add a new sqlworklogdal class, the generated Dal. dll is no longer the original Dal. dll.

The replication method has the following defect: as long as the Dal layer changes, you must manually copy it to update the Dal. dll file of the U layer. But you don't need to worry about modifying the path. It will automatically generate the latest Dal. dll file to the UI Layer.

So why do we need to generate the. dll file on the u layer?

This requires you to know how assembly. Load () locates the assembly?

When you only specify the Assembly name, such as "Dal", the CLR will only find the application under the application directory, and our application is on the UI Layer, so we need to have Dal on the u layer. DLL.

The problem comes again:

On the U layer, there are entity. dll, BLL. dll, and dataaccess. dll ...... Why is there no Dal. dll? Why does it only need to modify the generated path?

Here we will look at the package diagram of the program:


----> Indicates dependency or reference. In the UI Layer, the UI Layer can directly or indirectly reference the facade layer, BLL layer, dataaccess layer, and idal layer, however, the Dal layer is not referenced. DLL files will appear in the U layer, while the Dal layer. the DLL file does not appear in the U layer.

In this article, if you understand something wrong, please criticize and correct it.

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.