Exemple #1
0
        /// <summary>
        /// Instantiate and return a class conforming to the IDependentClass interface:
        /// which class gets instantiated depends on the ClassName setting in
        /// the configuration file
        /// </summary>
        /// <returns>Class conforming to the IDependentClass interface</returns>
        static IDependentClass GetCorrectDependency()
        {
            string          classToCreate = System.Configuration.ConfigurationManager.AppSettings["ClassName"];
            Type            type          = System.Type.GetType(classToCreate);
            IDependentClass dependency    = (IDependentClass)Activator.CreateInstance(type);

            return(dependency);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // Get the correct dependency based on configuration file
            IDependentClass dependency = GetCorrectDependency();

            // Create our main class and inject the dependency
            MainClass mainClass = new MainClass(dependency);

            // Use the main class, the method references the dependency
            // so behaviour depends on the configuration file
            mainClass.DoSomething();

            Console.ReadLine();
        }
Exemple #3
0
 public void InjectDependent(IDependentClass dependentClass)
 {
     this.dependentClass = dependentClass;
 }
Exemple #4
0
 public DependencyClass(IDependentClass client)
 {
 }
Exemple #5
0
 public MainClass(IDependentClass dependentClass)
 {
     this.dependentClass = dependentClass;
 }