Extension methods help you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method.
For example, let us assume Car.dll is the 3rd party DLL and source is not available.
Car Class Library - Details.CS
In the Console Application, reference Car.dll, you call the Wheel method.
Here you want to call Cost method, so Car.dll doesn't contain Cost method . Created a new Car Class Library called CarEM, referenced Car.dll, add a Cost Method.
In the Console Application, reference Car.dll, you call the Wheel and Cost method.
This is the one option, CarEm class invoke the Wheel function from Details and Cost function from DetailsEM, called Aggregating and it is complicated process.
You can achieve this by inheritance as well also.
If you able to see the Cost method from Details, instead of DetailsEM, this can be achieved by using Extension methods
In the Console Application,
Dataset is a disconnected architecture, even though if the database connection is close, the dataset will still able to browse the record. It means, records all fetched in memory, the time you call this Adpater.fill() method, all the records are fill in the memory. If sql server or connection is not here, still you able to browse the record from dataset. You call, DataSet is in-memory disconnected record set.
DataReader is a connected architecture, it needs a live connection with sql server, otherwise DataReade.Read() method will not able to process, Connection should open always.
SqlConnection con = new SqlConnection("ConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("select * from country");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//Adapter will fill the dataset object
sda.Fill(ds);
SqlDataReader sdr= cmd.ExecuteReader();
//Case 1 - Both dataset and datareader will browse the record
//Case 2 - Move con.Close() before for each loop,then run the code.
Dataset will browse the record,
//Datareader will throw the exception, that is 'Invalid attempt to call Read when reader is closed', Reason connection is closed here.
For example, let us assume Car.dll is the 3rd party DLL and source is not available.
Car Class Library - Details.CS
namespace Car
{
public class Details
{
public string Wheel(string cartype)
{
if (cartype=="CORROLA")
return "Two";
if (cartype == "JEEP")
return "Four";
else
return "No Wheel";
}
}
}
{
public class Details
{
public string Wheel(string cartype)
{
if (cartype=="CORROLA")
return "Two";
if (cartype == "JEEP")
return "Four";
else
return "No Wheel";
}
}
}
In the Console Application, reference Car.dll, you call the Wheel method.
using Car;
Details det = new Details();
Console.WriteLine(det.Wheel("JEEP"));
Console.WriteLine(det.Wheel("BMW"));
Details det = new Details();
Console.WriteLine(det.Wheel("JEEP"));
Console.WriteLine(det.Wheel("BMW"));
Here you want to call Cost method, so Car.dll doesn't contain Cost method . Created a new Car Class Library called CarEM, referenced Car.dll, add a Cost Method.
using Car;
namespace CarEM
{
public class DetailsEM
{
public Details det = new Details();
public string Cost(string cartype, string wheel)
{
if (cartype == "JEEP" && wheel == "Four")
return "20K";
if (cartype == "CORROLA" && wheel == "Two")
return "14K";
else
return "No Cost";
}
}
}
namespace CarEM
{
public class DetailsEM
{
public Details det = new Details();
public string Cost(string cartype, string wheel)
{
if (cartype == "JEEP" && wheel == "Four")
return "20K";
if (cartype == "CORROLA" && wheel == "Two")
return "14K";
else
return "No Cost";
}
}
}
In the Console Application, reference Car.dll, you call the Wheel and Cost method.
using CarEM;
DetailsEM detem = new DetailsEM();
Console.WriteLine("Aggergating - Complicated Proces");
Console.WriteLine(detem.Cost("JEEP", detem.det.Wheel("JEEP")));
Console.WriteLine(detem.Cost("CORROLA", detem.det.Wheel("CORROLA")));
DetailsEM detem = new DetailsEM();
Console.WriteLine("Aggergating - Complicated Proces");
Console.WriteLine(detem.Cost("JEEP", detem.det.Wheel("JEEP")));
Console.WriteLine(detem.Cost("CORROLA", detem.det.Wheel("CORROLA")));
This is the one option, CarEm class invoke the Wheel function from Details and Cost function from DetailsEM, called Aggregating and it is complicated process.
You can achieve this by inheritance as well also.
If you able to see the Cost method from Details, instead of DetailsEM, this can be achieved by using Extension methods
using Car;
public static class DetailsEM
{
public static string Cost(this Details det, string cartype, string wheel)
{
if (cartype == "JEEP" && wheel == "Four")
return "20K";
if (cartype == "CORROLA" && wheel == "Two")
return "14K";
else
return "No Cost";
}
}
public static class DetailsEM
{
public static string Cost(this Details det, string cartype, string wheel)
{
if (cartype == "JEEP" && wheel == "Four")
return "20K";
if (cartype == "CORROLA" && wheel == "Two")
return "14K";
else
return "No Cost";
}
}
In the Console Application,
using Car;
using CarEM;
Details detem = new Details();
Console.WriteLine("Extension Methods");
Console.WriteLine(detem.Cost("JEEP", detem.Wheel("JEEP")));
Console.WriteLine(detem.Cost("CORROLA", detem.Wheel("CORROLA")));
using CarEM;
Details detem = new Details();
Console.WriteLine("Extension Methods");
Console.WriteLine(detem.Cost("JEEP", detem.Wheel("JEEP")));
Console.WriteLine(detem.Cost("CORROLA", detem.Wheel("CORROLA")));
Dataset is a disconnected architecture, even though if the database connection is close, the dataset will still able to browse the record. It means, records all fetched in memory, the time you call this Adpater.fill() method, all the records are fill in the memory. If sql server or connection is not here, still you able to browse the record from dataset. You call, DataSet is in-memory disconnected record set.
DataReader is a connected architecture, it needs a live connection with sql server, otherwise DataReade.Read() method will not able to process, Connection should open always.
SqlConnection con = new SqlConnection("ConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("select * from country");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//Adapter will fill the dataset object
sda.Fill(ds);
SqlDataReader sdr= cmd.ExecuteReader();
//Browsing using dataset
foreach(DataRow dr in ds.Tables[0].Rows)
{
Debug.WriteLine("Set Countr Id " + dr["id"] + " Name " + dr["lname"]);
}
//Browsing using datareader
while(sdr.Read())
{
Debug.WriteLine("Read Countr Id " + sdr["id"] + " Name " + sdr["lname"]);
}
con.Close();
foreach(DataRow dr in ds.Tables[0].Rows)
{
Debug.WriteLine("Set Countr Id " + dr["id"] + " Name " + dr["lname"]);
}
//Browsing using datareader
while(sdr.Read())
{
Debug.WriteLine("Read Countr Id " + sdr["id"] + " Name " + sdr["lname"]);
}
con.Close();
//Case 2 - Move con.Close() before for each loop,then run the code.
Dataset will browse the record,
con.Close();
foreach(DataRow dr in ds.Tables[0].Rows) con.Close();
{
Debug.WriteLine("Set Countr Id " + dr["id"] + " Name " + dr["lname"]);
}
foreach(DataRow dr in ds.Tables[0].Rows) con.Close();
{
Debug.WriteLine("Set Countr Id " + dr["id"] + " Name " + dr["lname"]);
}
//Datareader will throw the exception, that is 'Invalid attempt to call Read when reader is closed', Reason connection is closed here.
while(sdr.Read())
{
Debug.WriteLine("Read Countr Id " + sdr["id"] + " Name " + sdr["lname"]);
}
{
Debug.WriteLine("Read Countr Id " + sdr["id"] + " Name " + sdr["lname"]);
}
No comments:
Post a Comment