Fetching DataReaders and projections
Besides using Linq to LLBLGen or QuerySpec, LLBLGen Pro has two other ways of fetching a resultset of fields: as an open IDataReader object and as a projection. This section discusses both and offers examples for each of them, either using a stored procedure or a query build using entity fields.
Fetching a resultset as an open IDataReader is considered an advanced feature and should be used with care: an open IDataReader object represents an open cursor to data on a connected RDBMS over an open connection. This means that passing the IDataReader around in your application is not recommended. Instead use the IDataReader in the method where you also called the fetch logic to create it and immediately after that make sure the IDataReader gets closed and disposed. This way you're sure you'll free up resources early.
To understand projections better, it's recommended to first read the section about fetching an open IDataReader. Another section describing projections, but then related to an entity view object, is Generated code - using the EntityView class.
Although QuerySpec and Linq offer ways to fetch projections into objects, this section primarily discusses projections using the low-level API. QuerySpec also supports fetching a projection as IDataReader. An example of this is given below in the section about fetching a dynamic list as IDataReader.
Fetching a resultset as an open IDataReader
To fetch a resultset as an open IDataReader, you call one of the overloads of GetAsDataReader, a method of the class TypedListDAO. There are two ways to use the GetAsDataReader method: by supplying a ready to use IRetrievalQuery or by specifying a fields list, and various other elements which are required for creating a new query by the Dynamic Query Engine (DQE).
The first option, the IRetrievalQuery option, can be used to fetch a retrieval stored procedure as an open IDataReader, by using the RetrievalProcedures.GetStoredProcedureNameCallAsQuery**() method of the particular stored procedure call. This is a generated method, one for every retrieval stored procedure call known in the LLBLGen Pro project.
GetAsDataReader accepts also a parameter called CommandBehavior. This parameter is very important as it controls the behavior the datareader should perform when the datareader is closed. It's required to specify a behavior different than CloseConnection if the fetch is inside a transaction and the connection has to stay open after the datareader has been closed.
With SelfServicing, it's especially recommended to set CommandBehavior to CloseConnection, as closing the connection can be a little problematic, because it's abstracted away from you.
It's possible to construct your own IRetrievalQuery object with your own SQL, by instantiating a new RetrievalQuery object. However in general, it's recommended to use the GetAsDataReader overloads which accept a fieldslist and other elements and let LLBLGen Pro generate the query for you.
GetAsDataReader has an async variant, GetAsDataReaderAsync, with various overloads to be able to fetch the datareader asynchronously
Fetching a Retrieval Stored Procedure as an IDataReader
An example of calling a procedure and receive a datareader from it is enlisted below. It calls the Northwind stored procedure CustOrdersOrders which returns a single resultset with 4 fields. The example simply prints the output on the console.
var dao = new TypedListDAO();
var reader = dao.GetAsDataReader(null,
RetrievalProcedures.GetCustOrdersOrdersCallAsQuery("CHOPS"),
CommandBehavior.CloseConnection);
while(reader.Read())
{
Console.WriteLine("Row: {0} | {1} | {2} | {3} |",
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2),
reader.GetValue(3));
}
reader.Close();
Dim dao As New TypedListDAO()
Dim reader As IDataReader = dao.GetAsDataReader(Nothing, _
RetrievalProcedures.GetCustOrdersOrdersCallAsQuery("CHOPS"), CommandBehavior.CloseConnection)
While reader.Read()
Console.WriteLine("Row: {0} | {1} | {2} | {3} |", _
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3))
End While
reader.Close()
Fetching a Dynamic List as an IDataReader
An example of a dynamic list which is used to receive a datareader from it is enlisted below. The example simply prints the output on the console.
var qf = new QueryFactory();
var q = qf.Create()
.Select(CustomerFields.CustomerId, CustomerFields.CompanyName, OrderFields.OrderId)
.From(qf.Customer.InnerJoin(qf.Order).On(CustomerFields.CustomerId.Equal(OrderFields.CustomerId)))
.Where(CustomerFields.Country.Equal("Germany"));
var reader = new TypedListDAO().FetchAsDataReader(null, q, CommandBehavior.CloseConnection);
while(reader.Read())
{
Console.WriteLine("Row: {0} | {1} | {2} |",
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2));
}
reader.Close();
Dim qf As New QueryFactory()
Dim q = qf.Create() _
.Select(CustomerFields.CustomerId, CustomerFields.CompanyName, OrderFields.OrderId) _
.From(qf.Customer.InnerJoin(qf.Order).On(CustomerFields.CustomerId.equal(OrderFields.CustomerId))) _
.Where(CustomerFields.Country.Equal("Germany"))
Dim reader = New TypedListDAO().FetchAsDataReader(Nothing, q, CommandBehavior.CloseConnection)
While reader.Read()
Console.WriteLine("Row: {0} | {1} | {2} |", _
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2))
End While
reader.Close()
var fields = new ResultsetFields(3);
// simply set the fields in the indexes, which will use the field name for the column name
fields[0] = CustomerFields.CustomerId;
fields[1] = CustomerFields.CompanyName;
fields[2] = OrderFields.OrderId;
var filter = new PredicateExpression();
var relations = new RelationCollection();
relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);
var dao = new TypedListDAO();
IDataReader reader = dao.GetAsDataReader(null, fields, filter, relations,
CommandBehavior.CloseConnection, 0, true);
while(reader.Read())
{
Console.WriteLine("Row: {0} | {1} | {2} |",
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2));
}
reader.Close();
Dim fields As New ResultsetFields(3)
' simply set the fields in the indexes, which will use the field name for the column name
fields(0) = CustomerFields.CustomerId
fields(1) = CustomerFields.CompanyName
fields(2) = OrderFields.OrderId
Dim filter As New PredicateExpression(CustomerFields.Country.Equal(Germany"))
Dim relations As New RelationCollection()
relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId)
Dim dao As New TypedListDAO()
Dim reader As IDataReader = dao.GetAsDataReader(Nothing, fields, filter, relations, _
CommandBehavior.CloseConnection, 0, True)
While reader.Read()
Console.WriteLine("Row: {0} | {1} | {2} |", _
reader.GetValue(0), reader.GetValue(1), reader.GetValue(2))
End While
reader.Close()
Projecting Stored Procedure resultset onto POCO classes
For this stored procedure projection example, the following stored proecdure is used:
CREATE procedure pr_CustomersAndOrdersOnCountry
@country VARCHAR(50)
AS
SELECT * FROM Customers WHERE Country = @country
SELECT * FROM Orders WHERE CustomerID IN
(
SELECT CustomerID FROM Customers WHERE Country = @country
)
which is a SQL Server stored procedure and which returns 2 resultsets: the first is all customers filtered on a given Country, and the second is all orders of those filtered customers.
The stored procedure is fetched as an open IDataReader and both
resultsets are projected onto a List of Poco classes: the first resultset on
a List<CustomerDTO>
and the second on a List<OrderDTO>
. The stored procedure
uses a wildcard select list. This is for simplicity.
List<CustomerDTO> customers;
List<OrderDTO> orders;
var dao = new TypedListDAO();
using(var query = RetrievalProcedures.GetCustomersAndOrdersOnCountryCallAsQuery("USA"))
{
using(var reader = dao.GetAsDataReader(null, query, CommandBehavior.CloseConnection))
{
customers = dao.GetAsProjection<CustomerDTO>(reader);
reader.NextResult();
orders = dao.GetAsProjection<OrderDTO>(reader);
}
}
Where CustomerDTO is:
public partial class CustomerDTO
{
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string CustomerId { get; set; }
public string Fax { get; set; }
public string Phone { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Address { get; set; }
public string City { get; set; }
}
and OrderDTO is
public partial class OrderDTO
{
public string CustomerId { get; set; }
public int? EmployeeId { get; set; }
public Decimal? Freight { get; set; }
public DateTime? OrderDate { get; set; }
public int OrderId { get; set; }
public DateTime? RequiredDate { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public string ShipCountry { get; set; }
public string ShipName { get; set; }
public DateTime? ShippedDate { get; set; }
public string ShipPostalCode { get; set; }
public string ShipRegion { get; set; }
}
If only the first resultset is enough, or the stored procedure returns just one resultset, you can also use the following, which avoids fetching the reader first.
var dao = new TypedListDAO();
List<CustomerDTO> customers = dao.GetAsProjection<CustomerDTO>(
RetrievalProcedures.GetCustomersAndOrdersOnCountryCallAsQuery("Germany"));
You can use the above mechanism to project a resultset or resultsets onto entity classes, and that will work OK, however it's not recommended as it's not using the optimized fetch pipeline of entities and performance can be lower than expected. Additionally, the entities likely will have their fields all marked as changed. Either fetch entities using the normal way, i.e. by using Linq, QuerySpec or the lowlevel API, or (recommended) map a typed view onto the stored procedure resultset in the designer, or project a stored procedure resultset to POCO classes / DTO classes.
GetAsProjection(IRetrievalQuery) has an async variant, GetAsProjectionAsync, with various overloads to be able to fetch the query asynchronously.
Projecting Dynamic List resultset onto POCO classes
We can go one step further and create a fetch of a dynamic list and fill a list of custom class instances, for example for transportation by a Webservice and you want lightweight Data Transfer Objects (DTO). For clarity, the Linq and QuerySpec alternatives are given as well.
var qf = new QueryFactory();
var q = qf.Customer
.Select(() => new CustomCustomer()
{
City = CustomerFields.City.ToValue<string>(),
CompanyName = CustomerFields.CompanyName.ToValue<string>(),
Country = CustomerFields.Country.ToValue<string>(),
CustomerID = CustomerFields.CustomerId.ToValue<string>()
});
List<CustomCustomer> customClasses = new TypedListDAO().FetchQuery(q);
var metaData = new LinqMetaData();
var q = from c in metaData.Customer
select new CustomCustomer()
{
CustomerID = c.CustomerId,
CompanyName = c.CompanyName,
Country = c.Country,
City = c.City
};
List<CustomCustomer> customClasses = q.ToList();
var customClasses = new List<CustomCustomer>();
var fields = new ResultsetFields(4);
fields[0] = CustomerFields.City;
fields[1] = CustomerFields.CompanyName;
fields[2] = CustomerFields.CustomerId;
fields[3] = CustomerFields.Country;
var projector = new DataProjectorToCustomClass<CustomCustomer>(customClasses);
// Define the projections of the fields.
var valueProjectors = new List<IDataValueProjector>();
valueProjectors.Add(new DataValueProjector("City", 0, typeof(string)));
valueProjectors.Add(new DataValueProjector("CompanyName", 1, typeof(string)));
valueProjectors.Add(new DataValueProjector("CustomerID", 2, typeof(string)));
valueProjectors.Add(new DataValueProjector("Country", 3, typeof(string)));
// perform the fetch combined with the projection.
var dao = new TypedListDAO();
dao.GetAsProjection(valueProjectors, projector, null, fields, null, null, 0, null, true);
Where the custom class is:
public class CustomCustomer
{
public string CustomerID { get; set;}
public string City { get; set; }
public string CompanyName { get; set;}
public string Country { get; set;}
}