//Depedency injection using constructor public void SavePerson(Person person) { dbDal.SavePersonalDetails(person); }
public void SavePersonalDetails(Person person) { Console.WriteLine("Personal details saved to oracle " + person.PersonName); }
//Depedency Injection using function parameter public void SavePerson(Person person,IDatabase DB) { DB.SavePersonalDetails(person); }
static void Main(string[] args) { Person person = new Person() { PersonID = 101, PersonName = "karan" }; IDatabase db; //saving in SQL db = new SQLServerDB(); person.SavePerson(person, db); db = new OracleDB(); person.SavePerson(person, db); //Depedency injection using constructor Person personSQL = new Person(new SQLServerDB()) { PersonID = 102, PersonName = "shukla" }; Person personOracle = new Person(new OracleDB()) { PersonID = 103, PersonName = "John" }; personSQL.SavePerson(personSQL); personOracle.SavePerson(personOracle); //depdency injection by unity IUnityContainer containerObj = new UnityContainer(); containerObj.RegisterType<Person>(); containerObj.RegisterType<IDatabase, SQLServerDB>(); //save into sql Person _person = containerObj.Resolve<Person>(); _person.PersonID = 103; _person.PersonName = "SQL Iunity"; _person.SavePerson(_person); containerObj.RegisterType<IDatabase, OracleDB>(); _person = containerObj.Resolve<Person>(); _person.PersonID = 104; _person.PersonName = "Orcale Iunity"; _person.SavePerson(_person); Console.Read(); }