Implement repository (Orum) (10) LINQ provider using nhib1_3.2

Source: Internet
Author: User
LINQ scalability

You can use multiple methods to extend the value of LINQ to query any data source in the LINQ mode. Data sources can be data structures, web services, file systems, databases, etc. The LINQ mode allows the client to easily query the data sources that can be queried by using LINQ, because the query syntax and mode are not changed.

You can use the following methods to extend the data source to these data sources:

  • Implement the ienumerable <t> interface in a certain type to enable LINQ to objects to query this type.
  • Create standard query operator methods (such as where and select) that can expand a type so that custom LINQ can query this type.
  • Create a provider for the data source that implements the iqueryable <t> interface. The provider that implements this interface receives the LINQ query in the form of an expression tree. The provider can execute the query in a custom way (for example, remotely.
  • Create a provisioner for the data source that utilizes the existing LINQ technology. This type of provider not only supports queries, but also supports user-defined insert, update, and delete operations and ing.
Query data sources by using LINQ
  • Memory Data.You can use either of the following methods to query data in the memory. If the data type implements ienumerable <t>, you can use LINQ to objects to query data. If you cannot enable type enumeration by implementing the ienumerable <t> interface, you can define the standard query operator methods of the LINQ standard query type in this type, or create a standard query operator method that can expand the standard query operator of this type. The custom implementation of standard query operators should use delayed execution to return results.
  • Remote Data.The iqueryable <t> interface is the best option for querying remote data sources. However, this is different from providing extended data sources (such as LINQ to SQL. Visual Studio does not have a provider model that extends existing LINQ technologies (such as LINQ to SQL) to other data source types.
LINQ provider

The complexity of implementing the iqueryable <t> LINQ provider may vary greatly.

  • The iqueryable provider with lower complexity can interact with a Web service method. This type of provider is very specific because it requires specific information in the query to be processed. It has a closed type system and may expose a single result type. Most queries are executed locally, for example, by using the enumerable of the standard query operator. A less complex provider may only check one method call expression in the expression tree that represents the query, and allow other queries to be processed.
  • The moderately complex iqueryable provider may target data sources that have some expressive query languages. If the provider is targeting a web service, it can interact with multiple methods of the web service and select the method to be called Based on the question raised by the query. Compared with simple providers, medium-complexity providers have a wide variety of type systems, but they are still fixed type systems. For example, a provider can expose a type with One-to-multiple relationships that can be traversed, but does not provide ing technology for user-defined types.
  • Complex iqueryable providers (such as the LINQ to SQL provider) can convert a complete LINQ query to a expressive Query Language (such as SQL ). Compared with a provider with lower complexity, a complex provider is more comprehensive because it can handle various problems in queries. It also has an open type system, so it must contain a wide range of infrastructure to map user-defined types. Developing complex providers requires a lot of effort.
How to Implement the LINQ provider

Interfaces required to implement iqueryable LINQ: iqueryable <t>, iorderedqueryable <t>, and iqueryprovider.

System. LINQ. iqueryable <t> Interface

using System;
using System.Collections;
using System.Collections.Generic;
namespace System.Linq
{
public interface IQueryable<out T> : IEnumerable<T>, IQueryable, IEnumerable
{
}
}

System. LINQ. iqueryable Interface

using System;
using System.Collections;
using System.Linq.Expressions;
namespace System.Linq
{
public interface IQueryable : IEnumerable
{
Expression Expression
{
get;
}
Type ElementType
{
get;
}
IQueryProvider Provider
{
get;
}
}
}

System. LINQ. iorderedqueryable <t> Interface

using System;
using System.Collections;
using System.Collections.Generic;
namespace System.Linq
{
public interface IOrderedQueryable<out T> : IQueryable<T>, IEnumerable<T>, IOrderedQueryable, IQueryable, IEnumerable
{
}
}

System. LINQ. iorderedqueryable Interface

using System;
using System.Collections;
namespace System.Linq
{
public interface IOrderedQueryable : IQueryable, IEnumerable
{
}
}

System. LINQ. iqueryprovider Interface

using System;
using System.Linq.Expressions;
namespace System.Linq
{
public interface IQueryProvider
{
IQueryable CreateQuery(Expression expression);
IQueryable<TElement> CreateQuery<TElement>(Expression expression);
object Execute(Expression expression);
TResult Execute<TResult>(Expression expression);
}
}

 

LINQ to SQL provider

As a component of. NET Framework Version 3.5, LINQ to SQL provides a runtime infrastructure for using relational data as object management.

In LINQ to SQL, the relational database data model is mapped to the object model represented by the programming language used by developers. When the application is running, LINQ to SQL converts the language integration query in the object model to SQL, and then sends them to the database for execution. When the database returns the results, LINQ to SQL converts them back to objects that can be processed in your own programming language.

LINQ to SQL only supports SQL Server databases. Its LINQ provider is implemented through the internal class system. Data. LINQ. dataquery.

System. Data. LINQ. dataquery <t>

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Provider;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime;
using System.Runtime.CompilerServices;
namespace System.Data.Linq
{
internal sealed class DataQuery<T> : IOrderedQueryable<T>, IQueryable<T>, IQueryProvider, IEnumerable<T>, IOrderedQueryable, IQueryable, IEnumerable, IListSource
{
private DataContext context;
private Expression queryExpression;
private IBindingList cachedList;
Expression IQueryable.Expression
{
get
{
return this.queryExpression;
}
}
Type IQueryable.ElementType
{
get
{
return typeof(T);
}
}
IQueryProvider IQueryable.Provider
{
get
{
return this;
}
}
bool IListSource.ContainsListCollection
{
get
{
return false;
}
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public DataQuery(DataContext context, Expression expression)
{
this.context = context;
this.queryExpression = expression;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
IQueryable IQueryProvider.CreateQuery(Expression expression)
{
if (expression == null)
{
throw Error.ArgumentNull("expression");
}
Type elementType = TypeSystem.GetElementType(expression.Type);
Type type = typeof(IQueryable<>).MakeGenericType(new Type[]
{
elementType
});
if (!type.IsAssignableFrom(expression.Type))
{
throw Error.ExpectedQueryableArgument("expression", type);
}
Type type2 = typeof(DataQuery<>).MakeGenericType(new Type[]
{
elementType
});
return (IQueryable)Activator.CreateInstance(type2, new object[]
{
this.context,
expression
});
}
IQueryable<S> IQueryProvider.CreateQuery<S>(Expression expression)
{
if (expression == null)
{
throw Error.ArgumentNull("expression");
}
if (!typeof(IQueryable<S>).IsAssignableFrom(expression.Type))
{
throw Error.ExpectedQueryableArgument("expression", typeof(IEnumerable<S>));
}
return new DataQuery<S>(this.context, expression);
}
object IQueryProvider.Execute(Expression expression)
{
return this.context.Provider.Execute(expression).ReturnValue;
}
S IQueryProvider.Execute<S>(Expression expression)
{
return (S)this.context.Provider.Execute(expression).ReturnValue;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this.context.Provider.Execute(this.queryExpression).ReturnValue).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return ((IEnumerable<T>)this.context.Provider.Execute(this.queryExpression).ReturnValue).GetEnumerator();
}
IList IListSource.GetList()
{
if (this.cachedList == null)
{
this.cachedList = this.GetNewBindingList();
}
return this.cachedList;
}
internal IBindingList GetNewBindingList()
{
return BindingList.Create<T>(this.context, this);
}
public override string ToString()
{
return this.context.Provider.GetQueryText(this.queryExpression);
}
}
}
List of LINQ providers

From: http://blogs.msdn.com/ B /charlie/archive/2008/02/28/link-to-everything-a-list-of-linq-providers.aspx

LINQ to Amazon: http://weblogs.asp.net/fmarguerie/archive/2006/06/26/Introducing-Linq-to-Amazon.aspx
LINQ to Active Directory: http://www.codeplex.com/LINQtoAD
LINQ to Bindable sources (synclinq): http://paulstovell.net/blog/index.php/why-synclinq-should-matter-to-you/
LINQ over C # project: http://www.codeplex.com/LinqOverCSharp
LINQ to CRM: http://www.codeplex.com/LinqtoCRM
LINQ to geo-Language Integrated Query for geospatial data: http://www.codeplex.com/LinqToGeo
LINQ to excel: http://www.codeplex.com/xlslinq
LINQ to expressions (metalinq): http://www.codeplex.com/metalinq
LINQ extender (Toolkit for building LINQ providers): http://www.codeplex.com/LinqExtender
LINQ to Flickr: http://www.codeplex.com/LINQFlickr
To Google: http://www.codeplex.com/glinq ">
LINQ to indexes (LINQ and i40): http://www.codeplex.com/i4o/Release/ProjectReleases.aspx? Releaseid = 3519
LINQ to iqueryable (Matt Warren on providers): http://blogs.msdn.com/mattwar/archive/2007/08/09/linq-building-an-iqueryable-provider-part-vi.aspx
LINQ to JSON: http://james.newtonking.com/archive/2008/02/11/linq-to-json-beta.aspx
LINQ to nhib.pdf: http://www.ayende.com/Blog/archive/2007/03/17/Implementing-Linq-for-NHibernate-A-How-To-Guide--Part.aspx
LINQ to javascript: http://www.codeplex.com/JSLINQ
LINQ to LDAP: http://community.bartdesmet.net/blogs/bart/archive/2007/04/05/the-iqueryable-tales-linq-to-ldap-part-0.aspx
LINQ to llblgen Pro: http://weblogs.asp.net/fbouma/archive/2008/03/12/beta-of-linq-to-llblgen-pro-released.aspx
LINQ to Lucene: http://www.codeplex.com/linqtolucene
LINQ to metaweb (freebase): http://www.codeplex.com/metawebToLinQ
LINQ to MySQL, Oracle and PostgreSQL (dblinq): http://code2code.net/DB_Linq/
LINQ to ncover: http://blog.joefeser.com/post/Linq-To-NCover-Part-2.aspx
LINQ to opf3: http://www.liensberger.it/web/blog? P = 235
LINQ to parallel (Plinq): http://www.microsoft.com/downloads/details.aspx? Familyid = e848dc1d-5be3-4941-8705-024bc7f180ba & amp; displaylang = en
LINQ to RDF files: http://blogs.msdn.com/hartmutm/archive/2006/07/24/677200.aspx
LINQ to SharePoint: http://www.codeplex.com/LINQtoSharePoint
LINQ to simpledb: http://www.codeplex.com/LinqToSimpleDB
LINQ to streams: http://www.codeplex.com/Slinq/
LINQ to webqueries: http://blogs.msdn.com/hartmutm/archive/2006/06/12/628382.aspx
LINQ to WMI: http://bloggingabout.net/blogs/emile/archive/2005/12/12/10514.aspx
LINQ to xtragrid: http://cs.rthand.com/blogs/blog_with_righthand/archive/2008/02/23/LINQ-to-XtraGrid.aspx

 

Source code download: http://mvcquick.codeplex.com/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.