Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design: Enable instantiating DbContext types at design time #8910

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/EFCore.Design/Design/DbContextActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Design
{
/// <summary>
/// Used to instantiate <see cref="DbContext"/> types at design time.
/// </summary>
public static class DbContextActivator
{
/// <summary>
/// Creates an instance of the specified <see cref="DbContext"/> type using the standard design-time
/// mechanisms. When available, this will use any <see cref="IDesignTimeDbContextFactory{TContext}"/>
/// implementations or the application's service provider.
/// </summary>
/// <param name="contextType"> The <see cref="DbContext"/> type to instantiate. </param>
/// <param name="startupAssembly"> The application's startup assembly. </param>
/// <param name="reportHandler"> The design-time report handler. </param>
/// <returns> The newly created object. </returns>
public static DbContext CreateInstance(
[NotNull] Type contextType,
[CanBeNull] Assembly startupAssembly = null,
[CanBeNull] IOperationReportHandler reportHandler = null)
{
Check.NotNull(contextType, nameof(contextType));

return new DbContextOperations(
new OperationReporter(reportHandler),
contextType.Assembly,
startupAssembly ?? contextType.Assembly)
.CreateContext(contextType.FullName);
}
}
}
27 changes: 27 additions & 0 deletions src/EFCore.Design/Design/IOperationReportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@

namespace Microsoft.EntityFrameworkCore.Design
{
/// <summary>
/// Used to handle reported design-time activity.
/// </summary>
public interface IOperationReportHandler
{
/// <summary>
/// Gets the contract version of this handler.
/// </summary>
/// <value> The contract version of this handler. </value>
int Version { get; }

/// <summary>
/// Invoked when an error is reported.
/// </summary>
/// <param name="message"> The message. </param>
void OnError([NotNull] string message);

/// <summary>
/// Invoked when a warning is reported.
/// </summary>
/// <param name="message"> The message. </param>
void OnWarning([NotNull] string message);

/// <summary>
/// Invoked when information is reported.
/// </summary>
/// <param name="message"> The message. </param>
void OnInformation([NotNull] string message);

/// <summary>
/// Invoked when verbose information is reported.
/// </summary>
/// <param name="message"> The message. </param>
void OnVerbose([NotNull] string message);
}
}
10 changes: 5 additions & 5 deletions src/EFCore.Design/Design/Internal/OperationReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class OperationReporter : IOperationReporter
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public OperationReporter([NotNull] IOperationReportHandler handler)
public OperationReporter([CanBeNull] IOperationReportHandler handler)
{
_handler = handler;
}
Expand All @@ -27,27 +27,27 @@ public OperationReporter([NotNull] IOperationReportHandler handler)
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void WriteError(string message)
=> _handler.OnError(message);
=> _handler?.OnError(message);

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void WriteWarning(string message)
=> _handler.OnWarning(message);
=> _handler?.OnWarning(message);

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void WriteInformation(string message)
=> _handler.OnInformation(message);
=> _handler?.OnInformation(message);

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void WriteVerbose(string message)
=> _handler.OnVerbose(message);
=> _handler?.OnVerbose(message);
}
}
24 changes: 24 additions & 0 deletions test/EFCore.Design.Tests/Design/DbContextActivatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Xunit;

namespace Microsoft.EntityFrameworkCore.Design
{
public class DbContextActivatorTest
{
[Fact]
public void CreateInstance_works()
{
var result = DbContextActivator.CreateInstance(typeof(TestContext));

Assert.IsType<TestContext>(result);
}

private class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseInMemoryDatabase(nameof(DbContextActivatorTest));
}
}
}