(1) Am I doing this right?
- Should I store my domain in the TemplateRepository and pass it to the service and controller like I'm doing?
(2) How do I reference ViewModels in the service? Just wrap them in methods in the service class?
(3) My biggest challenge is plumbing this to the views. I want to display a list of all templates with an Edit link, Create New Link, and a delete link. Can anyone provide some tips on that side? Thank you!
(4) I've been refactoring my code all night. I originally had the "public IList<TemplateListViewModel> GetAllTemplates()" method encapsulating "TemplateListViewModel". Would that method be better served by referencing the ViewModel instead of my current dev?
I have a model/service:
using HtmlAgilityPack;
using IDM.CMS3.Service.Models;
using Raven.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDM.CMS3.Service.ViewModels.Templates;
namespace IDM.CMS3.Service.Services
{
public interface ITemplateService
{
void Save(string content);
void AddTemplate(TemplateListViewModel template);
void DeleteTemplate(Guid id);
}
public class TemplateService : ITemplateService
{
IDocumentSession _documentSession;
TemplateRepository _repository;
public TemplateService(TemplateRepository repository)
{
//_documentSession = documentSession;
_repository = repository;
}
public IList<Template> GetAllTemplates()
{
return _repository.GetAllTemplates();
}
public void AddTemplate(Template newTemplate)
{
_repository.AddTemplate(newTemplate);
}
public Template GetTemplate(int TemplateAdminId)
{
return _repository.GetTemplate(TemplateAdminId);
}
public void DeleteTemplate(int TemplateAdminId)
{
_repository.DeleteTemplate(TemplateAdminId);
}
}
public void Save(string content)
{
//parse(content);
PageTemplate tm = new PageTemplate
{
Id = "site/1",
Text = content
};
_documentSession.Store(tm);
_documentSession.SaveChanges();
}
void parse(string content)
{
HtmlDocument document = new HtmlDocument();
string htmlString = "<a href>test.com</a>";
string target;
document.LoadHtml(htmlString);
HtmlNodeCollection collection = document.DocumentNode.SelectNodes("//a");
//var session = store.OpenSession();
foreach (HtmlNode link in collection)
{
target = link.Attributes["href"].Value;
}
}
}
}
I have a controller which calls the service and another model:
using System.Web.Mvc;
using IDM.CMS3.Web.Admin.Models;
using Raven.Abstractions.Data;
using Raven.Client;
using Raven.Client.Document;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDM.CMS3.Service.Models;
using Raven.Client.Indexes;
using IDM.CMS3.Service.App_Start;
using Raven.Abstractions.Indexing;
using IDM.CMS3.Service.Models.PageWidgets;
using System.IO;
using System.Reflection;
using IDM.CMS3.Service.Services;
using IDM.CMS3.Service.Models;
//using IDM.CMS3.Data.DTO;
namespace IDM.CMS3.Web.Admin.Controllers
{
public class TemplateController : Controller
{
readonly TemplateRepository _templateRepository;
readonly TemplateService _templateService;
public TemplateController(TemplateService templateService, TemplateRepository templateRepository)
{
_templateService = templateService;
_templateRepository = templateRepository;
}
public ActionResult Index()
{
TemplateService service = new TemplateService(_templateRepository);
return View(service.GetAllTemplates());
}
[HttpPost]
public ActionResult Details(int templateAdminId = 0)
{
TemplateService service = new TemplateService(_templateRepository);
Template template = service.GetTemplate(templateAdminId);
if (template == null)
{
return HttpNotFound();
}
return View(template);
}
[HttpPost]
public ActionResult Create(Template template)
{
if (ModelState.IsValid)
{
TemplateService service = new TemplateService(_templateRepository);
service.AddTemplate(template);
return RedirectToAction("Index", "TemplateController");
}
return View(template);
}
public ActionResult Delete(int templateAdminId = 0)
{
TemplateService service = new TemplateService(_templateRepository);
Template template = service.GetTemplate(templateAdminId);
if (template == null)
{
return HttpNotFound();
}
return View(template);
}
[HttpPost]
public ActionResult Save(TemplateViewModel content)
{
_templateService.Save(content.Content);
return View();
}
//public void Save()
//{
// HtmlDocument document = new HtmlDocument();
// string htmlString = "<a href>test.com</a>";
// string target;
// document.LoadHtml(htmlString);
// HtmlNodeCollection collection = document.DocumentNode.SelectNodes("//a");
// var session = store.OpenSession();
// store = new DocumentStore
// {
// Url = RavenUrl,
// DefaultDatabase = "WebCMS",
// };
// foreach (HtmlNode link in collection)
// {
// target = link.Attributes["href"].Value;
// }
// TemplateModel t0 = new TemplateModel()
// {
// Content = "test",
// };
// session.Store(t0);
// session.SaveChanges();
//}
}
}
Finally, I have a TemplateRepository:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDM.CMS3.Service.ViewModels.Templates;
using IDM.CMS3.Service.Models;
namespace IDM.CMS3.Service.Models
{
public class TemplateRepository : ModelBase
{
public IList<Template> GetAllTemplates();
IList<Template> GetTemplates(int userId);
public void AddTemplate(Template template);
public Template GetTemplate(int TemplateAdminId);
public void DeleteTemplate(int TemplateAdminId);
}
}
I also have a model for the singular Template:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IDM.CMS3.Service.Models
{
public class Template
{
public string Name { get; set; }
public string Category { get; set; }
public int TemplateAdminId { get; set; }
}
}
This post has been edited by regex: 19 February 2013 - 10:26 PM

New Topic/Question
Reply


MultiQuote




|