Skip to content

Commit 2846b10

Browse files
committed
Add ReactEnvironment.Current as a shortcut to get the current environment. Closes #195
1 parent dbdf756 commit 2846b10

File tree

7 files changed

+15
-7
lines changed

7 files changed

+15
-7
lines changed

src/React.AspNet/BabelFileMiddleware.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public async Task Invoke(HttpContext context)
6262
return;
6363
}
6464

65-
var reactEnvironment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
66-
var internalStaticMiddleware = CreateFileMiddleware(reactEnvironment.Babel);
65+
var internalStaticMiddleware = CreateFileMiddleware(ReactEnvironment.Current.Babel);
6766
await internalStaticMiddleware.Invoke(context);
6867
}
6968

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private static IReactEnvironment Environment
4040
{
4141
try
4242
{
43-
return global::React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
43+
return ReactEnvironment.Current;
4444
}
4545
catch (TinyIoCResolutionException ex)
4646
{

src/React.Core/ReactEnvironment.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public class ReactEnvironment : IReactEnvironment, IDisposable
8484
/// </summary>
8585
protected readonly IList<IReactComponent> _components = new List<IReactComponent>();
8686

87+
/// <summary>
88+
/// Gets the <see cref="IReactEnvironment"/> for the current request. If no environment
89+
/// has been created for the current request, creates a new one.
90+
/// </summary>
91+
public static IReactEnvironment Current
92+
{
93+
get { return AssemblyRegistration.Container.Resolve<IReactEnvironment>(); }
94+
}
95+
8796
/// <summary>
8897
/// Initializes a new instance of the <see cref="ReactEnvironment"/> class.
8998
/// </summary>

src/React.MSBuild/TransformBabel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override bool Execute()
4141
config
4242
.SetReuseJavaScriptEngines(false);
4343

44-
_environment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
44+
_environment = ReactEnvironment.Current;
4545

4646
Log.LogMessage("Starting Babel transform");
4747
var stopwatch = Stopwatch.StartNew();

src/React.Owin/BabelFileMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public BabelFileMiddleware(Func<IDictionary<string, object>, Task> next, BabelFi
5353
public async Task Invoke(IDictionary<string, object> environment)
5454
{
5555
// Create all "per request" instances
56-
var reactEnvironment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
56+
var reactEnvironment = ReactEnvironment.Current;
5757

5858
var internalStaticMiddleware = CreateFileMiddleware(reactEnvironment.Babel);
5959
await internalStaticMiddleware.Invoke(environment);

src/React.Sample.ConsoleApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void Main(string[] args)
2121
.SetReuseJavaScriptEngines(false)
2222
.AddScript("Sample.jsx");
2323

24-
var environment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
24+
var environment = ReactEnvironment.Current;
2525
var component = environment.CreateComponent("HelloWorld", new { name = "Daniel" });
2626
// renderServerOnly omits the data-reactid attributes
2727
var html = component.RenderHtml(renderServerOnly: true);

src/System.Web.Optimization.React/BabelTransform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class BabelTransform : IBundleTransform
2424
/// <param name="response">The bundle response.</param>
2525
public void Process(BundleContext context, BundleResponse response)
2626
{
27-
var environment = AssemblyRegistration.Container.Resolve<IReactEnvironment>();
27+
var environment = ReactEnvironment.Current;
2828
response.Content = environment.Babel.Transform(response.Content);
2929
}
3030
}

0 commit comments

Comments
 (0)