Using generics and nested classes for base view types

60 views
Skip to first unread message

Don

unread,
Jun 26, 2012, 1:15:04 PM6/26/12
to spar...@googlegroups.com
I'm trying to use Spark as a general rendering engine, similar to what is done in this example.  However, I'd like to be able to use a nested class within a generic as the base type, as in:

public class SparkEmailRenderer<DataType>
{
public class RendererView : AbstractSparkView
{
private Guid generatedViewId;

public RendererView() { generatedViewId = Guid.NewGuid(); }

public DataType Model { get; set; }

public override Guid GeneratedViewId { get { return this.generatedViewId; } }

public override void Render() { throw new NotImplementedException(); }
}

public SparkEmailRenderer(String templateString)
{
this.templateString = templateString;
}

public String Render(DataType data)
{
SparkSettings settings = new SparkSettings().SetPageBaseType(typeof(RendererView));
InMemoryViewFolder templates = new InMemoryViewFolder();
SparkViewEngine engine = new SparkViewEngine(settings) { ViewFolder = templates };
templates.Add("renderer.spark", this.templateString);

SparkViewDescriptor descriptor = new SparkViewDescriptor();
descriptor.AddTemplate("renderer.spark");

RendererView view = (RendererView) engine.CreateInstance(descriptor); // exception here
view.Model = data;

using (StringWriter stringWriter = new StringWriter())
{
view.RenderView(stringWriter);
stringWriter.Flush();

return stringWriter.ToString();
}
}
 
My very simple test case is this:

[TestMethod]
public void TestMethod1()
{
SparkEmailRendererFactory<Int32> factory = new SparkEmailRendererFactory<Int32>();
IEmailRenderer<SampleData> renderer = factory.BuildRenderer<SampleData>(@"This is some string data: ${Model.StringData}.

<for each='var tuple in Model.ListData'>
  - ${tuple.Item1} | ${tuple.Item2}
</for>");

SampleData sampleData = new SampleData
{
    StringData = "!@#$%^&*()"
  , ListData = Enumerable.Range(1, 9).Select(i => new Tuple<Int32, Double>(i, i * 1.1)).ToList()
};

String result = renderer.Render(sampleData);

Assert.AreEqual(result,@"This is some string data: !@#$%^&*().

  - 1 | 1.1
  - 2 | 2.2
  - 3 | 3.3
  - 4 | 4.4
  - 5 | 5.5
  - 6 | 6.6
  - 7 | 7.7
  - 8 | 8.8
  - 9 | 9.9");
}

I keep getting a dynamic view compilation error on public class View3d480988a882485c98ed589523d9e8b4 : Full.Name.For.SparkEmailRenderer`1+RendererView[[Full.Name.For.SampleData, Test.Assembly.Name, Version=2.0.2.0, Culture=neutral, PublicKeyToken=c5e39879b2509991]]

If I change the line about SparkSettings settings = new SparkSettings().SetPageBaseType(typeof(RendererView)); to just be the string literal Full."Name.For.SparkEmailRenderer.RendererView<Full.Name.For.SampleData>", it works, but that obviously won't work in the real world.

Any ideas on how to get that class name to render correctly?  Do I have to do it myself, or is there a way to get Spark to figure it out?

Thanks!

Don

unread,
Jun 27, 2012, 1:19:46 PM6/27/12
to spar...@googlegroups.com
So after some digging, I found how to get the C# name of a type on stackoverflow using

SparkViewDescriptor descriptor = new SparkViewDescriptor();
descriptor.SetLanguage(LanguageType.CSharp);
String typeName = new Microsoft.CSharp.CSharpCodeProvider().GetTypeOutput(new System.CodeDom.CodeTypeReference(typeof(RendererView)));
SparkSettings settings = new SparkSettings().SetPageBaseType(typeName);

to get the proper name.  It works for me.  Would there be any issue if someone were to use my assembly with a  VB project?

Thanks,

    Don

Rob G

unread,
Jul 7, 2012, 3:13:52 AM7/7/12
to spar...@googlegroups.com
Hi Don,

If you're using precompiled views with an assembly output, then I don't see an issue calling it from VB.NET. Also, if you're rendering views on the fly, then the C# compiler is used by default, which you can change to use the Visual Basic compiler - take a look here:  http://sparkviewengine.com/documentation/visualbasic 

Hope that answers your question...
Rob

--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spark-dev/-/q7P-bi0hPIEJ.

To post to this group, send email to spar...@googlegroups.com.
To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spark-dev?hl=en.

Reply all
Reply to author
Forward
0 new messages