WeBeliveInTigerman Posted January 22, 2016 Report Posted January 22, 2016 --- Basically u need to outsource the connection creation, read and write methods to another class and in Copy method u jus call these methods write interface and class to implement connection class outside copy method. IConnection { getConnetion(); Read(SqlConnection); Write(SqlConnection); } Class CreateConn:IConnection { getConnetion(){--Connection logic here--} Read(SqlConnection){--Read logic here--} Write(SqlConnection){--Write logic here--} Close(SqlConnection){--logic--} } Class A ,Class B, Class C unnayi CreateCon con = new CreateConn() ClassA ca= new ClassA(con); ca.Copy(); ClassB cb= new ClassB(con); cb.Copy(); ClassC cc= new Classc(con); cc.Copy() in class A/B/C create IConnection object and assign it using constructor Similarly implement read/write methods. Your copy method would be like this Copy() { SqlConnection con = a.GetConnection(); a.Read(con); a.Write(con); a.Close(con); } You can read in MSDN for Dependency Injection design pattern and Inversion of Control Principles
tejak123 Posted January 22, 2016 Author Report Posted January 22, 2016 Donniee.. yes.. this is what i exactly need. I used your code.. kaani i have small problem ..what if i have to pass output of "read" method as "input" to Write method.. how should i do..? i can change the return type in interface... but Class A , Class B, Class C have different return types in their respective READ methods just outline and naku telisindi rasa..hope this helps i dont have a strong command on this so get a second opinion public interface IJambalHeart { void Read(string conn); void Write(string conn); } public class ClassA: IJambalHeart { public void Read(string conn) { //ne istam } public void Write(string conn) { //ne istam } } alage classB, class ClassA public class SomeClass { private readonly IJambalHeart jambal; public SomeClass(IJambalHeart jambal) { this.jambal = jambal; } //read method calling public void ReadMethod(string conn) { //here jambal is class jambal.Read(conn); } public void WriteMethod(string conn) { //here jambal is class jambal.Write(conn); } } from where you are calling just inst interface IJambalHeart jambal = new ClassA(); //classc whatever you want SomeClass cl = new SomeClass(jambal); cl.ReadMethod(connection parameter); c1.WriteMethod(connection para);
tejak123 Posted January 22, 2016 Author Report Posted January 22, 2016 Tigerman.. Thanks for your time one more question.. what if i have to pass output of "read" method as "input" to Write method.. how should i do..? i can change the return type in interface... but Class A , Class B, Class C have different return types in their respective READ methods --- Basically u need to outsource the connection creation, read and write methods to another class and in Copy method u jus call these methods write interface and class to implement connection class outside copy method. IConnection { getConnetion(); Read(SqlConnection); Write(SqlConnection); } Class CreateConn:IConnection { getConnetion(){--Connection logic here--} Read(SqlConnection){--Read logic here--} Write(SqlConnection){--Write logic here--} Close(SqlConnection){--logic--} } Class A ,Class B, Class C unnayi CreateCon con = new CreateConn() ClassA ca= new ClassA(con); ca.Copy(); ClassB cb= new ClassB(con); cb.Copy(); ClassC cc= new Classc(con); cc.Copy() in class A/B/C create IConnection object and assign it using constructor Similarly implement read/write methods. Your copy method would be like this Copy() { SqlConnection con = a.GetConnection(); a.Read(con); a.Write(con); a.Close(con); } You can read in MSDN for Dependency Injection design pattern and Inversion of Control Principles
rikki Posted January 22, 2016 Report Posted January 22, 2016 just make it generic use T Donniee.. yes.. this is what i exactly need. I used your code.. kaani i have small problem ..what if i have to pass output of "read" method as "input" to Write method.. how should i do..? i can change the return type in interface... but Class A , Class B, Class C have different return types in their respective READ methods
LostAlone Posted January 22, 2016 Report Posted January 22, 2016 moq objects use chesi unit testing cheyi antunnadu vayya, see moq framework to get some idea. Use delegates..he might get impression on you. atb
Recommended Posts