Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating DynamicDataContextDriver for linqpad with c#

Tags:

c#

linq

linqpad

I am trying to create driver for linqpad and have question:

When I am creating DynamicDataContextDriver, I must create class TypedDataContext.

  1. What I should put in it?
  2. How will it be populated?
  3. Can I control how will it be populated?
  4. If I use object database here, is there something that I must bear in mind?

I found some answer here, but I can not find there all the above answers.

like image 440
andrey Avatar asked Dec 19 '12 12:12

andrey


1 Answers

A typed data context is simply a class with properties/fields suitable for querying. Those properties/fields will typically return IEnumerables or IQueryables. For example:

public class TypedDataContext
{
   public IEnumerable<Customer> Customers { get { ... } }
   public IEnumerable<Order> Orders { get { ... } }
   ...
}

When you use Visual Studio to create a new item of kind "LINQ to SQL classes" or "ADO.NET Entity Data Model", Visual Studio creates a typed data context for you which is an excellent example of what LINQPad expects. A typed data context can also expose methods (e.g., to map stored procedures or functions) - in fact it can expose anything that makes sense to the end user.

When you execute a query in LINQPad that has a connection, LINQPad subclasses the typed data context associated with the connection so that the query has access to all of its fields/properties. This is why Customers.Dump() is a valid query - we can just access Customers without having to instantiate the typed data context first.

A LINQPad driver can work in one of two ways. Either it can act like Visual Studio and build the typed data context automatically and on the fly (dynamic data context driver), or it can extract a typed data context from an existing assembly provided by the user (static data context driver). When you add a connection in LINQPad, you'll notice that the drivers are listed in two list boxes (Build data context automatically = dynamic driver, and Use a typed data context from your own assembly = static driver).

The typed data context is instantiated whenever a query executes. Because its properties typically return lazily evaluated IEnumerables/IQueryables, it's not helpful to think of "populating" it. However, it will need to know how to access an underlying data source, and this is done by passing arguments into the constructor.

LINQPad normally keeps the query's application domain alive between query runs, and this might be useful with caching and optimization should you be writing a driver for an object database. Other than that, there shouldn't be any special considerations for object databases.

like image 171
Joe Albahari Avatar answered Sep 28 '22 02:09

Joe Albahari