Skip to content

Commit

Permalink
Simplify the code
Browse files Browse the repository at this point in the history
Update the test
  • Loading branch information
smitpatel committed Aug 5, 2020
1 parent 8dcaf86 commit afd904c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
13 changes: 4 additions & 9 deletions src/EFCore/Metadata/Internal/ConstructorBindingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,12 @@ private bool TryBindConstructor(
out InstantiationBinding binding,
out IEnumerable<ParameterInfo> unboundParameters)
{
if (constructor.GetParameters().Any(x => string.IsNullOrEmpty(x.Name)))
{
unboundParameters = constructor.GetParameters().ToList();
binding = null;
return false;
}

IEnumerable<(ParameterInfo Parameter, ParameterBinding Binding)> bindings
= constructor.GetParameters().Select(
p => (p, _propertyFactory.FindParameter(entityType, p.ParameterType, p.Name)
?? bind(_factories.FindFactory(p.ParameterType, p.Name), entityType, p.ParameterType, p.Name)))
p => (p, string.IsNullOrEmpty(p.Name)
? null
: _propertyFactory.FindParameter(entityType, p.ParameterType, p.Name)
?? bind(_factories.FindFactory(p.ParameterType, p.Name), entityType, p.ParameterType, p.Name)))
.ToList();

if (bindings.Any(b => b.Binding == null))
Expand Down
85 changes: 45 additions & 40 deletions test/EFCore.Specification.Tests/WithConstructorsTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
Expand All @@ -18,6 +19,8 @@
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable InconsistentNaming
#pragma warning disable IDE0051 // Remove unused private members
#pragma warning disable IDE0052 // Remove unread private members
namespace Microsoft.EntityFrameworkCore
{
public abstract class WithConstructorsTestBase<TFixture> : IClassFixture<TFixture>
Expand Down Expand Up @@ -742,41 +745,22 @@ public virtual void Query_with_loader_delegate_injected_into_property_via_constr
[ConditionalFact]
public virtual async Task Add_immutable_record()
{
using var context = CreateContext();
var BlogTitle = "xyzzy";
var immutableBlog = new BlogAsImmutableRecord(BlogTitle);

context.Add(immutableBlog);
await context.SaveChangesAsync();

Assert.NotEqual(0, immutableBlog.BlogId);
Assert.Equal(BlogTitle, immutableBlog.Title);
}
#endif
var title = "xyzzy";
int blogId;
using (var context = CreateContext())
{
var immutableBlog = new BlogAsImmutableRecord(title);

context.Add(immutableBlog);
await context.SaveChangesAsync();

#if NET5_0
protected record BlogAsImmutableRecord
{
public int BlogId { get; init; }
public string Title { get; init; }
public int? MonthlyRevenue { get; init; }

private BlogAsImmutableRecord(
int blogId,
string title,
int? monthlyRevenue)
{
BlogId = blogId;
Title = title;
MonthlyRevenue = monthlyRevenue;
Assert.NotEqual(0, immutableBlog.BlogId);
blogId = immutableBlog.BlogId;
}

public BlogAsImmutableRecord(
string title,
int? monthlyRevenue = null)
: this(0, title, monthlyRevenue)
using (var context = CreateContext())
{
Assert.Equal(title, context.Set<BlogAsImmutableRecord>().Single(e => e.BlogId == blogId).Title);
}
}
#endif
Expand Down Expand Up @@ -1564,6 +1548,33 @@ public async Task<LazyAsyncBlog> LoadBlogAsync(CancellationToken cancellationTok
public LazyAsyncBlog LazyAsyncBlog { get; set; }
}

#if NET5_0
protected record BlogAsImmutableRecord
{
public BlogAsImmutableRecord(
string title,
int? monthlyRevenue = null)
: this(0, title, monthlyRevenue)
{
}

private BlogAsImmutableRecord(
int blogId,
string title,
int? monthlyRevenue)
{
BlogId = blogId;
Title = title;
MonthlyRevenue = monthlyRevenue;
}

[Key]
public int BlogId { get; init; }
public string Title { get; init; }
public int? MonthlyRevenue { get; init; }
}
#endif

public class OtherContext : DbContext
{
}
Expand All @@ -1582,16 +1593,6 @@ public abstract class WithConstructorsFixtureBase : SharedStoreFixtureBase<WithC

protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{

#if NET5_0
modelBuilder.Entity<BlogAsImmutableRecord>(
b =>
{
b.HasKey(e => e.BlogId);
b.Property(e => e.Title);
});
#endif

modelBuilder.Entity<Blog>(
b =>
{
Expand Down Expand Up @@ -1645,6 +1646,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
modelBuilder.Entity<LazyAsyncPsBlog>();
modelBuilder.Entity<LazyPcsBlog>();

#if NET5_0
modelBuilder.Entity<BlogAsImmutableRecord>();
#endif

// Manually configure service fields since there is no public API yet

var bindingFactories = context.GetService<IParameterBindingFactories>();
Expand Down

0 comments on commit afd904c

Please sign in to comment.