Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Internal Properties in RazorEngine

Our domain model properties are all "Internal" to protect the BDD approach we have taken. I really like making everything internal.

Recently I am trying to get the RazorEngine to process a template using one of these domain models with internal members. When it compiles, I get the error: .Attendee.FirstName' is inaccessible due to its protection level

I tried adding this line [assembly: InternalsVisibleTo("RazorEngine")] to the AssemblyInfo.cs of my domain model, but it doesnt seem to help.

How can I make my internal properties visible to the RazorEngine within the same project.

Code

public class Attendee : AggregateRoot {
    protected internal virtual new long Id { get; protected set; }
    protected internal virtual Event Event { get; protected set; }
    protected internal virtual bool? Online { get; protected set; }
    protected internal virtual string FirstName { get; protected set; }
    protected internal virtual string LastName { get; protected set; }
    protected internal virtual string Email { get; protected set; }
    protected internal virtual string Affiliation { get; protected set; }
    protected internal virtual string MeetingPassword { get; protected set; }
    protected internal virtual decimal AmountPaid { get; protected set; }
    protected internal virtual DateTime DateRegistered { get; protected set; } 

    public virtual void SendEmail() {

                var assembly = Assembly.GetExecutingAssembly();
                var stream = assembly.GetManifestResourceStream("VirtualAcademy.Domain.Email.RegistrationConfirmation.cshtml");
                var reader = new StreamReader(stream);
                var template = reader.ReadToEnd();                 
                Engine.Razor.RunCompile(template, "key", null, this);
     }

CSHTML FIle

@model VirtualAcademy.Domain.Attendee

<html>
<body style="font: 12px arial, sans-serif">
    <div>
        <span id="SalutationLabel">Hello @Model.FirstName @Model.LastName,</span>
like image 343
Chris Kooken Avatar asked Feb 16 '17 04:02

Chris Kooken


1 Answers

Unfortunately you can't do that...

@Model is just a public property of WebViewPage< TModel > so InternalsVisibleTo makes no difference

I suggest you create a ViewModel for your view.
(Maybe with a T4 template you can generate classes for your internal Models)

Now,if you are willing to lose type safety you can write an extension method and access the value using reflection

like image 126
George Vovos Avatar answered Oct 13 '22 09:10

George Vovos