csharp 这是一个文本上下文构建器,它基本上模仿了一个模拟框架,而没有将它添加到项目中。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 这是一个文本上下文构建器,它基本上模仿了一个模拟框架,而没有将它添加到项目中。相关的知识,希望对你有一定的参考价值。

	var context = new TestContext();
	context.SavedQueries.Add(new Mvc.Models.SavedQuery() { Id = 1, Name = "TestSavedQueryName", CreatedBy = "12345" });
	IEnumerable<Mvc.Models.SavedQuery> savedQueries = context.SavedQueryByQueryUserID("12345");
	Assert.AreEqual(1, savedQueries.Count());

//https://msdn.microsoft.com/en-us/data/dn314431.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using QueryBuilder.Mvc.Models;
using System.Data.Entity.Infrastructure;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Threading;

namespace QueryBuilder.Mvc.Tests.Models
{
    public class TestContext : QueryBuilderEntities, IQueryBuilderModelContext
    {
        public TestContext()
        {
            this.DataSources = new TestDbSet<DataSource>();
            this.DataViews = new TestDbSet<DataView>();
            this.SavedQueries = new TestDbSet<SavedQuery>();
            this.SavedQueryCriterias = new TestDbSet<SavedQueryCriteria>();
            this.SavedQueryFields = new TestDbSet<SavedQueryField>();
        }

        public override DbSet<DataSource> DataSources { get; set; }
        public override DbSet<DataView> DataViews { get; set; }
        public override DbSet<SavedQuery> SavedQueries { get; set; }
        public override DbSet<SavedQueryCriteria> SavedQueryCriterias { get; set; }
        public override DbSet<SavedQueryField> SavedQueryFields { get; set; }
        //IOrderedQueryable<DataView> DataViewsByDataSourceID(int? dataSourceID);
    }

    public class TestDbSet<TEntity> : DbSet<TEntity>, IQueryable, IEnumerable<TEntity>, IDbAsyncEnumerable<TEntity>
        where TEntity : class
    {
        ObservableCollection<TEntity> _data;
        IQueryable _query;

        public TestDbSet()
        {
            _data = new ObservableCollection<TEntity>();
            _query = _data.AsQueryable();
        }

        public override TEntity Add(TEntity item)
        {
            _data.Add(item);
            return item;
        }

        public override TEntity Remove(TEntity item)
        {
            _data.Remove(item);
            return item;
        }

        public override TEntity Attach(TEntity item)
        {
            _data.Add(item);
            return item;
        }

        public override TEntity Create()
        {
            return Activator.CreateInstance<TEntity>();
        }

        public override TDerivedEntity Create<TDerivedEntity>()
        {
            return Activator.CreateInstance<TDerivedEntity>();
        }

        public override ObservableCollection<TEntity> Local
        {
            get { return _data; }
        }

        Type IQueryable.ElementType
        {
            get { return _query.ElementType; }
        }

        Expression IQueryable.Expression
        {
            get { return _query.Expression; }
        }

        IQueryProvider IQueryable.Provider
        {
            get { return new TestDbAsyncQueryProvider<TEntity>(_query.Provider); }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _data.GetEnumerator();
        }

        IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
        {
            return _data.GetEnumerator();
        }

        IDbAsyncEnumerator<TEntity> IDbAsyncEnumerable<TEntity>.GetAsyncEnumerator()
        {
            return new TestDbAsyncEnumerator<TEntity>(_data.GetEnumerator());
        }
    }

    internal class TestDbAsyncQueryProvider<TEntity> : IDbAsyncQueryProvider
    {
        private readonly IQueryProvider _inner;

        internal TestDbAsyncQueryProvider(IQueryProvider inner)
        {
            _inner = inner;
        }

        public IQueryable CreateQuery(Expression expression)
        {
            return new TestDbAsyncEnumerable<TEntity>(expression);
        }

        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            return new TestDbAsyncEnumerable<TElement>(expression);
        }

        public object Execute(Expression expression)
        {
            return _inner.Execute(expression);
        }

        public TResult Execute<TResult>(Expression expression)
        {
            return _inner.Execute<TResult>(expression);
        }

        public Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)
        {
            return Task.FromResult(Execute(expression));
        }

        public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
        {
            return Task.FromResult(Execute<TResult>(expression));
        }
    }

    internal class TestDbAsyncEnumerable<T> : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable<T>
    {
        public TestDbAsyncEnumerable(IEnumerable<T> enumerable)
            : base(enumerable)
        { }

        public TestDbAsyncEnumerable(Expression expression)
            : base(expression)
        { }

        public IDbAsyncEnumerator<T> GetAsyncEnumerator()
        {
            return new TestDbAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
        }

        IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
        {
            return GetAsyncEnumerator();
        }

        IQueryProvider IQueryable.Provider
        {
            get { return new TestDbAsyncQueryProvider<T>(this); }
        }
    }

    internal class TestDbAsyncEnumerator<T> : IDbAsyncEnumerator<T>
    {
        private readonly IEnumerator<T> _inner;

        public TestDbAsyncEnumerator(IEnumerator<T> inner)
        {
            _inner = inner;
        }

        public void Dispose()
        {
            _inner.Dispose();
        }

        public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
        {
            return Task.FromResult(_inner.MoveNext());
        }

        public T Current
        {
            get { return _inner.Current; }
        }

        object IDbAsyncEnumerator.Current
        {
            get { return Current; }
        }
    }
}

以上是关于csharp 这是一个文本上下文构建器,它基本上模仿了一个模拟框架,而没有将它添加到项目中。的主要内容,如果未能解决你的问题,请参考以下文章

VS解决方案文件的Windows资源管理器上下文菜单中的“构建”项?

AQS基本原理

csharp 将“Move to Top”和“Move to Bottom”项添加到组件的检查器上下文菜单中。

csharp 将“Move to Top”和“Move to Bottom”项添加到组件的检查器上下文菜单中。

csharp 用于创建文本表的字符串构建器

反应中的全局屏幕加载器