Razor View Engine in Console App with Complex Bass Class

c# dapper razor

Question

I'm using the Razor view engine to render some HTML which will then live within an XML document. the base class I'm using has a number of properties, along with a static method which will return a list of that object (using Dapper to populate the list). I'm having trouble executing the method since it needs to return the base class, which is an abstract class. Some sample code is below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.IO;

namespace LocalBranchesPOC
{
    public abstract class PersonData : TemplateBase
    {
        #region Properties
        public string RecordId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public string Zip { get; set; }
        public string Phone { get; set; }
        public string Variable1 { get; set; }
        public string Variable2 { get; set; }
        public string Variable3 { get; set; }

        #endregion

        public static List<PersonData> GetPeople() 
        {
            const string QUERY = "SELECT [RecordId], [Name], [Address], [City], [County], [State], [Country], [Zip], [Phone], [Variable1], [Variable2], [Variable3] FROM Data.Person";

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BranchLocator"].ConnectionString))
            {
                return getPeople(QUERY, conn);
            }
        }

        private static List<PersonData> getPeople(string QUERY, SqlConnection conn)
        {
            conn.Open();
            var result = conn.Query<PersonData>(QUERY).ToList();
            conn.Close();
            return result;
        }
    }

    public abstract class TemplateBase
    {
        [Browsable(false)]
        public StringBuilder Buffer { get; set; }

        [Browsable(false)]
        public StringWriter Writer { get; set; }

        public TemplateBase()
        {
            Buffer = new StringBuilder();
            Writer = new StringWriter(Buffer);
        }

        public abstract void Execute();

        // Writes the results of expressions like: "@foo.Bar"
        public virtual void Write(object value)
        {
            // Don't need to do anything special
            // Razor for ASP.Net does HTML encoding here.
            WriteLiteral(value);
        }

        // Writes literals like markup: "<p>Foo</p>"
        public virtual void WriteLiteral(object value)
        {
            Buffer.Append(value);
        }
    }
}

Basically my call to PersonData.GetPeople() is failing because the PersonData class is abstract. Any thoughts would be appreciated. I'm using the example from here as my guide.

Accepted Answer

You're trying to merge the model and the view.
Don't do that; it cannot possibly work.

Instead, pass the model to the view as a separate property, perhaps loading it in the TemplateBase constructor.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why