Linq basics. Linq and n-tier architecture
Microsoft has provided a new easy-to-learn and very powerful and flexible extension of .NET languages for data processing called Linq.
How in multi-level applications to design a data provider so as to get a set of objects?
For example, there is a table tbCustomers ((Int64) CustID, (String) Name, (int32) Age) and there is a Customers class:
publicclass Customers
{
private Int64 _CustID;
public Int64 CustID
{
get {return _CustID; }
set {_CustID = value; }
}
private string _Name;
public string Name
{
get {return _Name; }
set {_Name = value; }
}
// Int32 type allowing null
private Int32? _Age;
public Int32? Age
{
get {return _Age; }
set {_Age = value; }
}
// Initializers
public Customers ()
{
}
public Customers (Int64 CustID, string Name, Int32? Age)
{
_CustID = CustID;
_Name = Name;
_Age = Age;
}
// Get a set of objects from the
publicstatic List data providerGetCustomers ()
{
return CustomersData.GetAllCustomers ();
}
}
and you need to get a set of Customers objects from the provider.
The standard solution to this issue in .NET2.0 is to use the System.Data.SqlClient namespace:
publicstatic class CustomersData
{
// Use the SqlClient
public static ListGetAllCustomers ()
{
List lst = new List();
using (SqlConnection conn = new SqlConnection ("Data Source = ..."))
{
conn.Open ();
SqlCommand cmd = new SqlCommand ("SELECT CustID, Name, Age FROM tbCustomers", conn);
SqlDataReader reader = cmd.ExecuteReader ();
while (reader.Read ())
{
lst.Add (new Customers ((Int64) reader ["CustID"], (String) reader ["Name"], (Int32)? reader ["Age"]));
}
}
return lst;
}
}
When using Linq, we can get a dataset from anonymous types using the object initializer, i.e.
publicstatic class CustomersData
{
// Usage of
Linq public static ListGetAllCustomers ()
{
LinqDcDataContext db = new LinqDcDataContext ();
IEnumerableresult = db.tbCustomers.Select (c => new Customers (c.CustID, c.Name, c.Age));
// cast a set of IEnumerable to list
return new List(result);
}
}
Original Linq article in n-tier architecture
How in multi-level applications to design a data provider so as to get a set of objects?
For example, there is a table tbCustomers ((Int64) CustID, (String) Name, (int32) Age) and there is a Customers class:
publicclass Customers
{
private Int64 _CustID;
public Int64 CustID
{
get {return _CustID; }
set {_CustID = value; }
}
private string _Name;
public string Name
{
get {return _Name; }
set {_Name = value; }
}
// Int32 type allowing null
private Int32? _Age;
public Int32? Age
{
get {return _Age; }
set {_Age = value; }
}
// Initializers
public Customers ()
{
}
public Customers (Int64 CustID, string Name, Int32? Age)
{
_CustID = CustID;
_Name = Name;
_Age = Age;
}
// Get a set of objects from the
publicstatic List data provider
{
return CustomersData.GetAllCustomers ();
}
}
and you need to get a set of Customers objects from the provider.
The standard solution to this issue in .NET2.0 is to use the System.Data.SqlClient namespace:
publicstatic class CustomersData
{
// Use the SqlClient
public static List
{
List
using (SqlConnection conn = new SqlConnection ("Data Source = ..."))
{
conn.Open ();
SqlCommand cmd = new SqlCommand ("SELECT CustID, Name, Age FROM tbCustomers", conn);
SqlDataReader reader = cmd.ExecuteReader ();
while (reader.Read ())
{
lst.Add (new Customers ((Int64) reader ["CustID"], (String) reader ["Name"], (Int32)? reader ["Age"]));
}
}
return lst;
}
}
When using Linq, we can get a dataset from anonymous types using the object initializer, i.e.
publicstatic class CustomersData
{
// Usage of
Linq public static List
{
LinqDcDataContext db = new LinqDcDataContext ();
IEnumerable
// cast a set of IEnumerable
return new List
}
}
Original Linq article in n-tier architecture