11.07.2015 Views

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12 ■ LEARNING ABOUT APPLICATION CONFIGURATION AND DYNAMIC LOADING 341_availableTypes. If the identifier exists in _availableTypes, the ConfigurationInfo instance isretrieved and used <strong>to</strong> instantiate the type. If the identifier does not exist, an exception is thrown.The bolded code is the special code because it is unlike what you have encountered thusfar in the book. Built in<strong>to</strong> .NET is the ability <strong>to</strong> dynamically execute code, as illustrated bythese three very powerful lines of code. The first bolded line makes the reflection ability available,and the second is used <strong>to</strong> load the assembly. So, for example, if the parameter identifierequaled Impl1, the first bolded line would reference and dynamically load the assemblyImplementations1.dll. However, and here is the catch, the assembly can be loaded dynamicallyonly if Implementations1.dll exists in the local direc<strong>to</strong>ry or the GAC. As an alternative,you could specify the entire path in the definition of the assembly. The downside <strong>to</strong> this strategyis that your assembly must always be located at the same place on different machines.When the second bolded line assigns the variable assembly, it is <strong>to</strong> a reference of a loaded.NET assembly. The assembly will be parsed, and <strong>from</strong> there, it is possible <strong>to</strong> instantiate a type.The third bolded line calls the method CreateInstance(), with the name of the type that youwant <strong>to</strong> instantiate, including the namespace. So, for example, if Implementations1.dll hasbeen loaded, you could instantiate the type Implementations1.Implementation. The instantiationwill work even though Implementations1.Implementation is a private class, because youare using dynamic programming principles.However, being able <strong>to</strong> instantiate the type does not imply being able <strong>to</strong> use the type. Tobe able <strong>to</strong> use the instantiated type, you need <strong>to</strong> be able <strong>to</strong> type cast <strong>to</strong> a type that has beendeclared publicly.We will continue the Implementations1.Implementation example and figure out how <strong>to</strong>instantiate and manipulate the instantiated object. But before we get <strong>to</strong> that code, we need <strong>to</strong>talk about an additional programming feature: single<strong>to</strong>ns.Using Single<strong>to</strong>nsThe class ConfigurationManager is a static class, which means that you cannot instantiate theclass. A static class and static methods, and static data members, result in a single-purpose class.Another way <strong>to</strong> get the same effect is <strong>to</strong> create a single<strong>to</strong>n. A single<strong>to</strong>n behaves like a staticclass, except that the class is instantiated. An advantage of using the single<strong>to</strong>n approach is thatyou can have multiple single<strong>to</strong>ns of the same type, whereas with the static class, there can be onlya single static class.Consider that locks have keys, and sometimes a lock has a single key. Think of the singlekey as a static class. If that key is <strong>to</strong> open a vault, you will most likely want only one person <strong>to</strong>have a key. But what if the key is <strong>to</strong> your house door? You would probably want multiplecopies, but you will want <strong>to</strong> control who has those keys. So, maybe you will have a singlehouse key, or maybe you will have multiple house keys, but you will always be in control. Thehouse key example is analogous <strong>to</strong> the use of a single<strong>to</strong>n. You could argue that a single<strong>to</strong>ncould just as well be used <strong>to</strong> open a vault, since you can create a single key. But the problemwith a single<strong>to</strong>n is that using properly written source code, you could instantiate a second orthird instance of the single<strong>to</strong>n. If you use a static class, then source code cannot instantiatea second instance, thus enforcing a certain programming style.Let’s define the class ConfigurationLoader as a single<strong>to</strong>n, which implies two things:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!