-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-convention configuration for conventions
Part of #214
- Loading branch information
1 parent
71b7f6d
commit 5e00822
Showing
8 changed files
with
268 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
/// <summary> | ||
/// Provides a simple API surface for configuring conventions. | ||
/// </summary> | ||
/// <remarks> | ||
/// Instances of this class are returned from methods when using the <see cref="ModelConfigurationBuilder" /> API | ||
/// and it is not designed to be directly constructed in your application code. | ||
/// </remarks> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
/// </remarks> | ||
public class ConventionsBuilder | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly ConventionSet _conventionSet; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
[EntityFrameworkInternal] | ||
public ConventionsBuilder(ConventionSet conventionSet, IServiceProvider serviceProvider) | ||
{ | ||
Check.NotNull(conventionSet, nameof(conventionSet)); | ||
|
||
_conventionSet = conventionSet; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
/// <summary> | ||
/// Replaces an existing convention with a derived convention. Also registers the new convention for any | ||
/// convention types not implemented by the existing convention. | ||
/// </summary> | ||
/// <typeparam name="TImplementation">The type of the old convention.</typeparam> | ||
/// <param name="conventionFactory">The factory that creates the new convention.</param> | ||
public virtual void Replace<TImplementation>(Func<IServiceProvider, TImplementation> conventionFactory) | ||
where TImplementation : IConvention | ||
{ | ||
var convention = conventionFactory(_serviceProvider); | ||
_conventionSet.Replace(convention); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a convention to the set. | ||
/// </summary> | ||
/// <param name="conventionFactory">The factory that creates the convention.</param> | ||
public virtual void Add(Func<IServiceProvider, IConvention> conventionFactory) | ||
{ | ||
var convention = conventionFactory(_serviceProvider); | ||
_conventionSet.Add(convention); | ||
} | ||
|
||
/// <summary> | ||
/// Removes the convention of the given type. | ||
/// </summary> | ||
/// <param name="conventionType">The convention type to remove.</param> | ||
public virtual void Remove(Type conventionType) | ||
{ | ||
_conventionSet.Remove(conventionType); | ||
} | ||
|
||
#region Hidden System.Object members | ||
|
||
/// <summary> | ||
/// Returns a string that represents the current object. | ||
/// </summary> | ||
/// <returns>A string that represents the current object.</returns> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public override string? ToString() | ||
=> base.ToString(); | ||
|
||
/// <summary> | ||
/// Determines whether the specified object is equal to the current object. | ||
/// </summary> | ||
/// <param name="obj">The object to compare with the current object.</param> | ||
/// <returns><see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />.</returns> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
// ReSharper disable once BaseObjectEqualsIsObjectEquals | ||
public override bool Equals(object? obj) | ||
=> base.Equals(obj); | ||
|
||
/// <summary> | ||
/// Serves as the default hash function. | ||
/// </summary> | ||
/// <returns>A hash code for the current object.</returns> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
// ReSharper disable once BaseObjectGetHashCodeCallInGetHashCode | ||
public override int GetHashCode() | ||
=> base.GetHashCode(); | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.