public static void Main(string[] args) { // injection par instanciation statique //CreateHostBuilder(args).Build().Run(); //DaoIpml dao = new DaoIpml(); //MetierImpl metier = new MetierImpl(dao); //ou methode 2 via le ctroc par default //metier.dao = dao; //Console.WriteLine(metier.calcul()); //Console.ReadKey(); //injection par instanciation dynamique string path = "config.txt"; string[] data = File.ReadAllLines(path); string daoClassName = data[0]; string metierClassName = data[1]; IDao dao = (IDao)Activator.CreateInstance(Type.GetType(daoClassName)); IMetier metier = (IMetier)Activator.CreateInstance(Type.GetType(metierClassName), dao); // c'est l'équivalent de [ MetierImpl metier = new MetierImpl(dao); ] Console.WriteLine(metier.calcul()); Console.WriteLine(daoClassName); Console.WriteLine(metierClassName); Console.ReadKey(); }
static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); IMetier dao = ctx["MyMetier"] as IMetier; //ImplDao1 dao1 = (ImplDao1)ctx.GetObject("MyDao"); Console.WriteLine(dao.Calcul()); Console.ReadKey(); }
public void SetMetier(IMetier metier) //conserve l'instance de métier identique à celle du form parent { try { _metier = metier; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
static void Main(string[] args) { //instanciation statique /*DaoImpl daoImpl = new DaoImpl(); * MetierImpl metierImpl = new MetierImpl(daoImpl); * Console.WriteLine(metierImpl.Calcul()); * // * MetierImpl metier = new MetierImpl(); * metier.Dao = daoImpl; * Console.WriteLine("________________"); * Console.WriteLine(metierImpl.Calcul());*/ Console.WriteLine("INSTANCIATION DYNMAIQUE "); //instanciation dynamique utilisation lire line par line String[] data = File.ReadAllLines("config.txt"); String daoClassName1 = data[0]; String MetierClassName1 = data[1]; /* StreamReader streamReader = new StreamReader("config.txt"); * String daoClassName = streamReader.ReadLine(); * String MetierClassName = streamReader.ReadLine(); * Console.WriteLine("CASS NAME DAO "+daoClassName); * Console.WriteLine("CASS NAME Metier "+ MetierClassName);*/ //instancier dynmaquement les class objet type equivant de Class en java Type typeDao = Type.GetType(daoClassName1); Console.WriteLine("type dao " + typeDao); IDao dao = (IDao)Activator.CreateInstance(typeDao); Type typeMetier = Type.GetType(MetierClassName1); IMetier metierDynamic = (IMetier)Activator.CreateInstance(typeMetier, dao); Console.WriteLine("resultat avec instanciaition dynmaique" + metierDynamic.Calcul()); // creation du conainer //var services = new ServiceCollection(); //ajout des instances au container //services.addSingelto<IDao, DaoImpl>() //services.addSingelto<IMetier,MetierImpl>() //Recuperer une instance ISERVICEPRODIVDER //IServiceProvider provider=services.BuildServiceProvider() //recuperation de l'objet qui implemente l'interface //IMetier metier=provider.getServic<IMetier>() //console.WriteLine(metier.calcul() Console.ReadKey(); }