c# - How to Invoke the overloaded method in place of actual method -


i have mocking.sln has 2 projects: student (class library) , studentstat (console application)

student project has below details:

    private static dictionary<int, int> getstudentdata()         {             // key: student id value: total marks(marks in math + marks in physics)             dictionary<int, int> studentresultdict = new dictionary<int, int>();              using (sqlconnection con = new sqlconnection("data source=.;initial catalog=mydb;integrated security=true"))             {                 con.open();                 string cmdstr = "select * student";                 using (sqlcommand cmd = new sqlcommand(cmdstr, con))                 {                     using (sqldatareader reader = cmd.executereader())                     {                         while (reader.read())                         {                             studentresultdict.add((int)reader["id"], (int)reader["marksinmath"] + (int)reader["marksinphysics"]);                         }                     }                 }             }              return studentresultdict;         }       public static void calculatetotalmarks()         {             dictionary<int, int> studentresultdict = getstudentdata();             foreach (var item in studentresultdict)             {                 console.writeline("{0}\t{1}", item.key, item.value);             }         } 

studentstat project has below details:

class program     {         static void main(string[] args)         {             studentinfo.calculatetotalmarks();         }     } 

this getstudentdata() method read details db , output details. suppose production code , have no permission change anything.

i have overloaded version of getstudentdata() takes filepath parameter , read details text file. below method:

private static dictionary<int, int> getstudentdata(string filepath)     {         dictionary<int, int> studentresultdict = new dictionary<int, int>();         foreach (var item in file.readalllines(filepath))         {             string[] data = item.split(new string[] { "|" }, stringsplitoptions.removeemptyentries);             studentresultdict.add(convert.toint32(data[0]), convert.toint32(data[1]) + convert.toint32(data[2]));         }          return studentresultdict;     } 

now when call studentinfo.calculatetotalmarks() in studentstat.exe, want overloaded getstudentdata(string filepath) called instead of other method (which reads data db), , shows me output.

i heard possible using nmock3. dont have idea on that. can please share soem code samples..it me learn nmock3 also. appreciated...

you can use test driven developement need refactor code have there , implement interface segregation part of solid principle, recommend read roy osherov´s blog father of tdd, learned tdd reading book art of unit testing (it old now) anyway in nutshell need decouple code , separate implementations , define contract it.

for example can have interface going define methods of need do.

interface istudentservice{    dictionary<int, int> getstudentdata()  } 

and have 2 different implementations

to keep simple i'm going copy , paste sql code must have in mind sql commands need treated dependencies in tdd approach well.

public class dbstudentservice:istudentservice{      public dictionary<int, int> getstudentdata()     {     // key: student id value: total marks(marks in math + marks in physics)         dictionary<int, int> studentresultdict = new dictionary<int, int>();         using (sqlconnection con = new sqlconnection("data source=.;initial catalog=mydb;integrated security=true"))             {                 con.open();                 string cmdstr = "select * student";                 using (sqlcommand cmd = new sqlcommand(cmdstr, con))                 {                     using (sqldatareader reader = cmd.executereader())                     {                         while (reader.read())                         {                             studentresultdict.add((int)reader["id"], (int)reader["marksinmath"] + (int)reader["marksinphysics"]);                         }                     }                 }             }              return studentresultdict;         } } 

and create different implementation let´s say

   public class filestudentservice:istudentservice{         steing _filepath;         public filestudentservice(string filepath){           _filepath = filepath;         }          public dictionary<int, int> getstudentdata()         {             dictionary<int, int> studentresultdict = new dictionary<int, int>();             foreach (var item in file.readalllines(_filepath))             {                 string[] data = item.split(new string[] { "|" }, stringsplitoptions.removeemptyentries);                 studentresultdict.add(convert.toint32(data[0]), convert.toint32(data[1]) + convert.toint32(data[2]));             }          return studentresultdict;         }      } 

just notice methods not static anymore because not testable of test framework out there , pass filepath dependency in constructor of class.

so need choose dependency injection container there several projects can find take this benchmark , choose conclusions.

personally structuremap , simple injector registering implementation simple (using si)

var container = new container(); configure container (register) container.register<istudentservice, filestudentservice>(); 

and can inject service using constructor injection program , should trick

i know not full working code @ least want give idea of how can start!

cheers!


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -