Skip to content

Commit

Permalink
Merge pull request #7 from conficient/v1.1
Browse files Browse the repository at this point in the history
Rename `BlazorTemplater` class to `Templater`. Fixes #4
  • Loading branch information
conficient committed Apr 13, 2021
2 parents 74c6c91 + 1c63049 commit e0a89b7
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 85 deletions.
2 changes: 0 additions & 2 deletions BlazorTemplater.ConsoleApp/BlazorTemplater.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.14" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\BlazorTemplater\BlazorTemplater.csproj" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions BlazorTemplater.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace BlazorTemplater.ConsoleApp
{
class Program
{
static void Main(string[] args)
static void Main()
{
Console.WriteLine("Rendering Sample.razor to HTML..");

var renderer = new BlazorTemplater();
var html = renderer.RenderComponent<Sample>();
var templater = new Templater();
var html = templater.RenderComponent<Sample>();

Console.WriteLine(html);
}
Expand Down
1 change: 0 additions & 1 deletion BlazorTemplater.Library/BlazorTemplater.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<Product>BlazorTemplater</Product>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.14" />
Expand Down
1 change: 1 addition & 0 deletions BlazorTemplater.Library/CodeBehind.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Yes @Name, we can use code-behind files with @App</p>
15 changes: 15 additions & 0 deletions BlazorTemplater.Library/CodeBehind.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorTemplater.Library
{
public partial class CodeBehind : ComponentBase
{
[Parameter] public string Name { get; set; }
[Parameter] public string App { get; set; }
}
}
2 changes: 1 addition & 1 deletion BlazorTemplater.Library/ServiceInjection.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inject ITestService testService;
@inject ITestService testService
<p>If you add @A and @B you get @testService.Add(A,B)</p>
@code
{
Expand Down
110 changes: 61 additions & 49 deletions BlazorTemplater.Tests/BlazorTemplater_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BlazorTemplater_Tests
[TestMethod]
public void Ctor_Test()
{
var templater = new BlazorTemplater();
var templater = new Templater();
Assert.IsNotNull(templater);
}

Expand All @@ -29,8 +29,8 @@ public void RenderComponent_Simple_Test()
{
const string expected = @"<b>Jan 1st is 2021-01-01</b>";

var renderer = new BlazorTemplater();
var actual = renderer.RenderComponent<Simple>();
var templater = new Templater();
var actual = templater.RenderComponent<Simple>();

Console.WriteLine(actual);
Assert.AreEqual(expected, actual);
Expand All @@ -49,7 +49,7 @@ public void RenderComponent_Parameters_Test()
// expected output
const string expected = "<p>Steve Sanderson is awesome!</p>";

var renderer = new BlazorTemplater();
var templater = new Templater();
var model = new TestModel()
{
Name = "Steve Sanderson",
Expand All @@ -59,7 +59,7 @@ public void RenderComponent_Parameters_Test()
{
{ nameof(Parameters.Model), model }
};
var html = renderer.RenderComponent<Parameters>(parameters);
var html = templater.RenderComponent<Parameters>(parameters);

// trim leading space and trailing CRLF from output
var actual = html.Trim();
Expand All @@ -77,7 +77,7 @@ public void RenderComponent_Parameters_TestHtmlEncoding()
// expected output
const string expected = "<p>Safia &amp; Pranav are awesome too!</p>";

var renderer = new BlazorTemplater();
var templater = new Templater();
var model = new TestModel()
{
Name = "Safia & Pranav",
Expand All @@ -87,7 +87,7 @@ public void RenderComponent_Parameters_TestHtmlEncoding()
{
{ nameof(Parameters.Model), model }
};
var html = renderer.RenderComponent<Parameters>(parameters);
var html = templater.RenderComponent<Parameters>(parameters);

// trim leading space and trailing CRLF from output
var actual = html.Trim();
Expand All @@ -105,8 +105,8 @@ public void RenderComponent_Parameters_TestIfModelNotSet()
// expected output
const string expected = "<p>No model!</p>";

var renderer = new BlazorTemplater();
var html = renderer.RenderComponent<Parameters>();
var templater = new Templater();
var html = templater.RenderComponent<Parameters>();

// trim leading space and trailing CRLF from output
var actual = html.Trim();
Expand All @@ -125,12 +125,12 @@ public void RenderComponent_Parameters_TestIfModelNotSet()
[TestMethod]
public void RenderComponent_Error_Test()
{
var renderer = new BlazorTemplater();
var templater = new Templater();

// we should get a NullReferenceException thrown as Model parameter is not set
Assert.ThrowsException<NullReferenceException>(() =>
{
_ = renderer.RenderComponent<ErrorTest>();
_ = templater.RenderComponent<ErrorTest>();
});
}

Expand All @@ -150,16 +150,16 @@ public void AddService_TestInstanceMethod()
const int c = a + b;
string expected = $"<p>If you add {a} and {b} you get {c}</p>";

// create a renderer and register an ITestService. The service adds values
var renderer = new BlazorTemplater();
renderer.AddService<ITestService>(new TestService());
// create a templater and register an ITestService. The service adds values
var templater = new Templater();
templater.AddService<ITestService>(new TestService());

var parameters = new Dictionary<string, object>()
{
{ nameof(ServiceInjection.A), a },
{ nameof(ServiceInjection.B), b }
};
var actual = renderer.RenderComponent<ServiceInjection>(parameters);
var actual = templater.RenderComponent<ServiceInjection>(parameters);

Console.WriteLine(actual);
Assert.AreEqual(expected, actual);
Expand All @@ -180,7 +180,7 @@ public void RenderComponent_Nested_Test()
// on Windows the string contains \r\n and on unix it's just \n
string expected = $"<b>Jan 1st is 2021-01-01</b>{Environment.NewLine} <p>Dan Roth is cool!</p>";

var renderer = new BlazorTemplater();
var templater = new Templater();
var model = new TestModel()
{
Name = "Dan Roth",
Expand All @@ -190,7 +190,7 @@ public void RenderComponent_Nested_Test()
{
{ nameof(Parameters.Model), model }
};
var html = renderer.RenderComponent<NestedComponents>(parameters);
var html = templater.RenderComponent<NestedComponents>(parameters);

// trim leading space and trailing CRLF from output
var actual = html.Trim();
Expand All @@ -209,47 +209,59 @@ public void RenderComponent_Nested_Test()
[TestMethod]
public void RenderComponent_ReusingRenderer()
{
// set up
const int a = 2;
const int b = 3;
const int c = a + b;
string expected1 = $"<p>If you add {a} and {b} you get {c}</p>";
// create a templater and register an ITestService. The service adds values
var templater = new Templater();
templater.AddService<ITestService>(new TestService());

const int x = 456;
const int y = 123;
const int z = x + y;
string expected2 = $"<p>If you add {x} and {y} you get {z}</p>";

// create a renderer and register an ITestService. The service adds values
var renderer = new BlazorTemplater();
renderer.AddService<ITestService>(new TestService());

var parameters1 = new Dictionary<string, object>()
// Render the component 100 times
const int count = 100;
for (int a = 1; a <= count; a++)
{
{ nameof(ServiceInjection.A), a },
{ nameof(ServiceInjection.B), b }
};

// new parameters
var parameters2 = new Dictionary<string, object>()
{
{ nameof(ServiceInjection.A), x },
{ nameof(ServiceInjection.B), y }
};
// set up
const int b = 3;
int c = a + b;
string expected = $"<p>If you add {a} and {b} you get {c}</p>";

var parameters = new Dictionary<string, object>()
{
{ nameof(ServiceInjection.A), a },
{ nameof(ServiceInjection.B), b }
};

// render both components
var actual = templater.RenderComponent<ServiceInjection>(parameters);

Console.WriteLine(actual);
Assert.AreEqual(expected, actual);
}
}

// render both components
var actual1 = renderer.RenderComponent<ServiceInjection>(parameters1);
var actual2 = renderer.RenderComponent<ServiceInjection>(parameters2);
#endregion

Console.WriteLine(actual1);
Console.WriteLine(actual2);
#region Code-Behind Test

/// <summary>
/// Test a simple component with no parameters
/// </summary>
[TestMethod]
public void RenderComponent_CodeBehind_Test()
{
const string expected = @"<p>Yes Jon, we can use code-behind files with BlazorTemplater</p>";

Assert.AreEqual(expected1, actual1);
Assert.AreEqual(expected2, actual2);
var templater = new Templater();
var parameters = new Dictionary<string, object>()
{
{ nameof(CodeBehind.App), "BlazorTemplater" },
{ nameof(CodeBehind.Name), "Jon" }
};
var actual = templater.RenderComponent<CodeBehind>(parameters);

Console.WriteLine(actual);
Assert.AreEqual(expected, actual);
}


#endregion

}
}
3 changes: 2 additions & 1 deletion BlazorTemplater.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{B0E3EF40-CE74-4196-993F-D65E256F52FB}"
ProjectSection(SolutionItems) = preProject
Docs\AddRazorSupport.md = Docs\AddRazorSupport.md
Docs\Usage.md = Docs\Usage.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorTemplater.ConsoleApp", "BlazorTemplater.ConsoleApp\BlazorTemplater.ConsoleApp.csproj", "{B1C3C891-8000-4AAB-AEAA-1902EDF6D3B0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorTemplater.ConsoleApp", "BlazorTemplater.ConsoleApp\BlazorTemplater.ConsoleApp.csproj", "{B1C3C891-8000-4AAB-AEAA-1902EDF6D3B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
5 changes: 2 additions & 3 deletions BlazorTemplater/BlazorTemplater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
<PackageProjectUrl>https://github.com/conficient/BlazorTemplater</PackageProjectUrl>
<RepositoryUrl>https://github.com/conficient/BlazorTemplater</RepositoryUrl>
<PackageTags>Blazor RazorComponents HTML Email Templating</PackageTags>
<Version>1.0.0</Version>
<PackageReleaseNotes>Initial Version</PackageReleaseNotes>
<Version>1.1.0</Version>
<PackageReleaseNotes>Breaking change: renamed BlazorTemplater class to Templater</PackageReleaseNotes>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.14" />
Expand Down
31 changes: 21 additions & 10 deletions BlazorTemplater/BlazorTemplater.cs → BlazorTemplater/Templater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
namespace BlazorTemplater
{
/// <summary>
/// Rendering 'host' that supports service injection
/// Templating host that supports service injection and rendering
/// </summary>
public class BlazorTemplater
public class Templater
{
/// <summary>
/// Ctor
/// </summary>
public BlazorTemplater()
public Templater()
{
// define a lazy service provider
_serviceProvider = new Lazy<IServiceProvider>(() =>
Expand All @@ -31,26 +31,37 @@ public BlazorTemplater()
});
}

/// <summary>
/// Lazy service collection instance
/// </summary>
private readonly ServiceCollection _serviceCollection = new ServiceCollection();

/// <summary>
/// Lazy HtmlRenderer instance
/// </summary>
private readonly Lazy<HtmlRenderer> _renderer;

/// <summary>
/// Lazy ServiceProvider instance
/// </summary>
private readonly Lazy<IServiceProvider> _serviceProvider;

/// <summary>
/// Services provided by DI
/// Services provided by Dependency Injection
/// </summary>
public IServiceProvider Services => _serviceProvider.Value;

/// <summary>
/// Gets lazy renderer
/// Gets Renderer
/// </summary>
private HtmlRenderer Renderer => _renderer.Value;

/// <summary>
/// Add a service for injection - do this before rendering
/// </summary>
/// <typeparam name="TContract"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="implementation"></param>
/// <typeparam name="TContract">The interface/contract</typeparam>
/// <typeparam name="TImplementation">The implementation type</typeparam>
/// <param name="implementation">Instance to return</param>
public void AddService<TContract, TImplementation>(TImplementation implementation) where TImplementation : TContract
{
if (_renderer.IsValueCreated)
Expand All @@ -63,8 +74,8 @@ public BlazorTemplater()
/// <summary>
/// Add a service with implementation
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="implementation"></param>
/// <typeparam name="T">Type of service</typeparam>
/// <param name="implementation">Instance to return</param>
public void AddService<T>(T implementation)
=> AddService<T, T>(implementation);

Expand Down
Loading

0 comments on commit e0a89b7

Please sign in to comment.