Generic SparkViewBase in a standalone app.

29 views
Skip to first unread message

Stéphane

unread,
May 24, 2010, 3:48:13 PM5/24/10
to Spark View Engine Dev
Hi,
I am building a newsletter service that uses spark to generate the
HTML.
I can get Spark to work fine in my standalone application if the
DefaultPageBaseType is a simple class inheriting from SparkViewBase

So I have a ViewData class inheriting from SparkViewBase with a Model
property of type EmailData containing all the info to build up the
html, I then access it in the view with Model.Xxx

What I would like is to have the ViewData class agnostic of the model
it is going to present to the view.
So I changed my code to be as follow :

public class ViewData<T> : SparkViewBase
{
public T Model {get; set;}
}

In the constructor of my HtmlBuilder<T> (T being the Model) I
instanciate the engine like that:

engine = new SparkViewEngine(setup);
engine.DefaultPageBaseType = typeof(ViewData<T>).FullName;

And finally to create my view I do :

var view = (ViewData<T>)engine.CreateInstance(descriptor);


Everything seems ok but at runtime :

Unhandled Exception: Spark.Compiler.CompilerException: Dynamic view
compilation
failed.
\Temp\hxntjkr1.0.cs(10,81): error CS1056: Unexpected character '`'
l\Temp\hxntjkr1.0.cs(10,81): error CS1514: { expected
\Temp\hxntjkr1.0.cs(10,81): error CS1519: Invalid token '`' in class,
struct, or interface member declaration
\Temp\hxntjkr1.0.cs(10,84): error CS1001: Identifier expected
\Temp\hxntjkr1.0.cs(10,145): error CS1003: Syntaxerror, ']' expected
\Temp\hxntjkr1.0.cs(10,145): error CS1519: Invalid token '=' in class,
struct, or interface member declaration
\Temp\hxntjkr1.0.cs(10,162): error CS1519: Invalid token '=' in class,
struct, or interface member declaration
\Temp\hxntjkr1.0.cs(10,170): error CS1519: Invalid token ',' in class,
struct, or interface member declaration
\Temp\hxntjkr1.0.cs(10,186): error CS1519: Invalid token '=' in class,
struct, or interface member declaration

From what I understand Spark doesnt understand the <> and the []given
by typeof(ViewData<T>).FullName due to the special character generated
by generics.
But this is working when used in MVC right? (I assume) So how can I
get this to work in my standalone app?

Thanks for help,

Stéphane


--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
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.

TMFReason

unread,
May 25, 2010, 12:39:58 PM5/25/10
to Spark View Engine Dev
I'm not sure if there is any fundamental issue with your generics, but
on the view side, how are you declaring your model? Are you using
something like <viewdata model ="IEnumerable[[EmailData]]"/> with ['s
in place of >'s? A simple answer, but it would match the errors you
are getting.
> You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.To post to this group, send email tospa...@googlegroups.com.To unsubscribe from this group, send email tospark-dev...@googlegroups.com.

Stéphane

unread,
May 25, 2010, 12:52:26 PM5/25/10
to Spark View Engine Dev
No I have nothing in the view. The view is not declaring any
viewmodel, since the code that instanciate the view is setting the
DefaultPageBaseType Property using typeOf().FullName
But should I then build the string manually in my code because typeof
returns me those unhandled characters? I'll give it a try and reply in
a minute.
> > You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.To post to this group, send email tospark-...@googlegroups.com.To unsubscribe from this group, send email tospark-dev...@googlegroups.com.

Stéphane

unread,
May 25, 2010, 1:09:50 PM5/25/10
to Spark View Engine Dev
Ha! Got It!
I tried multiple syntaxes and the last one, desperate try, was the
correct one apparently :)

public HtmlGenerator()
{
var setup = new SparkSettings()
.AddNamespace("System")
.AddNamespace("System.Collections.Generic")
.AddNamespace(typeof (Program).Namespace)
.AddNamespace(typeof
(System.Linq.Enumerable).Namespace);

engine = new SparkViewEngine(setup);
engine.DefaultPageBaseType = "ViewData<EmailModel>";
FAIL //engine.DefaultPageBaseType =
"ConsoleApplication1.ViewData[[ConsoleApplication1.EmailModel]]";
FAIL //engine.DefaultPageBaseType =
"ViewData[[EmailModel]]";
FAIL //engine.DefaultPageBaseType =
typeof(ViewData<T>).FullName;
}


So I can actually write : engine.DefaultPageBaseType = "ViewData<"+
typeof(T).FullName +">";
and that works. If I can get rid of the string completely, that would
be even better, but that's doing it already. :) Thanks for putting me
on the way!

attilah

unread,
May 25, 2010, 2:48:13 PM5/25/10
to Spark View Engine Dev
Hi Stéphane!

The thing you are doing is a good direction, when I've faced with this
problem I had to make a Type extension method which generated the type
name for the generic parameter.

Offline I'm sending you some of my files what I'm currently using and
can be a good starter for you and with little modification it can
serve you well.

Thanks,
Attila

Stéphane

unread,
May 26, 2010, 4:28:38 AM5/26/10
to Spark View Engine Dev
Thank you very much. The extensions are helpful :)
That's pretty much what I was about to write anyway.
Now, time to put this together and start building spark templates :)

Any status on the intellisense plugin for VS2010?

Rob G

unread,
May 26, 2010, 5:04:04 AM5/26/10
to spar...@googlegroups.com
Hi Stéphane,

I published this post last night with an update about the VS2010 extension. I'm hoping to release an alpha very soon.

Regards,
Rob

Louis DeJardin

unread,
Jun 15, 2010, 2:53:40 PM6/15/10
to spar...@googlegroups.com

Great post, and thanks for the effort! I’ll forward the feedback to the VS team if you’d like. :)

Reply all
Reply to author
Forward
0 new messages