|
| 1 | +// Copyright (C) 2020 Xtensive LLC. |
| 2 | +// This code is distributed under MIT license terms. |
| 3 | +// See the License.txt file in the project root for more information. |
| 4 | + |
| 5 | +using NUnit.Framework; |
| 6 | +using System; |
| 7 | +using System.Linq; |
| 8 | +using Xtensive.Orm; |
| 9 | +using Xtensive.Orm.Configuration; |
| 10 | +using Xtensive.Orm.Tests; |
| 11 | +using Xtensive.Orm.Tests.Issues.IssueJira0800_InterfaceImplementingDtoProblemModel; |
| 12 | + |
| 13 | +namespace Xtensive.Orm.Tests.Issues |
| 14 | +{ |
| 15 | + [TestFixture] |
| 16 | + public class IssueJira0800_DtoInterfaceFieldInitializationWithinCtor : AutoBuildTest |
| 17 | + { |
| 18 | + protected override DomainConfiguration BuildConfiguration() |
| 19 | + { |
| 20 | + var config = base.BuildConfiguration(); |
| 21 | + config.Types.Register(typeof(Example)); |
| 22 | + config.UpgradeMode = DomainUpgradeMode.Recreate; |
| 23 | + return config; |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + public void FilterThroughInterfaceMethod() |
| 28 | + { |
| 29 | + using (var session = Domain.OpenSession()) |
| 30 | + using (var transaction = session.OpenTransaction()) { |
| 31 | + var query = session.Query |
| 32 | + .All<Example>() |
| 33 | + .Select(x => new ExampleDto { EndDate = x.EndDate }) |
| 34 | + .ApplyFilterUsingInterface(); |
| 35 | + var expression = query.Expression; |
| 36 | + Console.WriteLine(expression); |
| 37 | + _ = query.ToArray(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void FilterThroughBaseClassMethod() |
| 43 | + { |
| 44 | + using (var session = Domain.OpenSession()) |
| 45 | + using (var transaction = session.OpenTransaction()) { |
| 46 | + var query = session.Query |
| 47 | + .All<Example>() |
| 48 | + .Select(x => new ExampleDto { EndDate = x.EndDate }) |
| 49 | + .ApplyFilterUsingBaseClass(); |
| 50 | + var expression = query.Expression; |
| 51 | + Console.WriteLine(expression); |
| 52 | + _ = query.ToArray(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void FilterDirectlyWithInterface() |
| 58 | + { |
| 59 | + using (var session = Domain.OpenSession()) |
| 60 | + using (var transaction = session.OpenTransaction()) { |
| 61 | + var query = session.Query |
| 62 | + .All<Example>() |
| 63 | + .Select(x => (IEndOwner) new ExampleDto { EndDate = x.EndDate }) |
| 64 | + .Where(x => x.EndDate == null); |
| 65 | + var expression = query.Expression; |
| 66 | + Console.WriteLine(expression); |
| 67 | + _ = query.ToArray(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void FilterDirectlyWithExactType() |
| 73 | + { |
| 74 | + using (var session = Domain.OpenSession()) |
| 75 | + using (var transaction = session.OpenTransaction()) { |
| 76 | + var query = session.Query |
| 77 | + .All<Example>() |
| 78 | + .Select(x => new ExampleDto { EndDate = x.EndDate }) |
| 79 | + .Where(x => x.EndDate == null); |
| 80 | + var expression = query.Expression; |
| 81 | + Console.WriteLine(expression); |
| 82 | + _ = query.ToArray(); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +namespace Xtensive.Orm.Tests.Issues.IssueJira0800_InterfaceImplementingDtoProblemModel |
| 89 | +{ |
| 90 | + public class ExampleDto : EndOwner |
| 91 | + { |
| 92 | + public long Id { get; set; } |
| 93 | + |
| 94 | + public override DateTime? EndDate { get; set; } |
| 95 | + } |
| 96 | + |
| 97 | + [HierarchyRoot] |
| 98 | + public class Example : Entity |
| 99 | + { |
| 100 | + [Field, Key] |
| 101 | + public long Id { get; set; } |
| 102 | + |
| 103 | + [Field] |
| 104 | + public DateTime? EndDate { get; set; } |
| 105 | + } |
| 106 | + |
| 107 | + public static class ContractsExtensions |
| 108 | + { |
| 109 | + public static IQueryable<TContractOwner> ApplyFilterUsingBaseClass<TContractOwner>(this IQueryable<TContractOwner> query) |
| 110 | + where TContractOwner : EndOwner |
| 111 | + { |
| 112 | + return query.Where(x => x.EndDate == null); |
| 113 | + } |
| 114 | + |
| 115 | + public static IQueryable<TContractOwner> ApplyFilterUsingInterface<TContractOwner>(this IQueryable<TContractOwner> query) |
| 116 | + where TContractOwner : class, IEndOwner |
| 117 | + { |
| 118 | + return query.Where(x => x.EndDate == null); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public abstract class EndOwner : IEndOwner |
| 123 | + { |
| 124 | + public abstract DateTime? EndDate { get; set; } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + public interface IEndOwner |
| 129 | + { |
| 130 | + DateTime? EndDate { get; } |
| 131 | + } |
| 132 | +} |
0 commit comments