I'm learning .net MVC while developing a simple word list - Eg. When you learn a new language and create a list of words related to a certain topic.
I don't think I'm following the rules or good practices of the MVC architecture.
- It doesn't feel right that I pass the full Model to the view, which also contains methods. I think it should be just properties. Should I create a ViewModel or repository or both?
- Should Models contain methods?
- Should repositories contain methods?
- If models have contain methods, than the equivalent repository shouldn't and vice-versa?
- Can I chain calls? Eg.
ListModel listModel = new ListModel(); listModel.GetListById(listId).SetOwnerId(222).save();
?
My ListModel.cs:
using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Dapper;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace WordList.Models
{
public class ListModel : BaseModel
{
public int listId { get; set; }
[Required(ErrorMessage = "Name is required.")]
public string name { get; set; }
[Required(ErrorMessage = "Description is required.")]
public string description { get; set; }
[Required(ErrorMessage = "Language is required.")]
public string language { get; set; }
public int ownerId { get; set; }
public DateTime date_created { get; set; }
public DateTime date_updated { get; set; }
public List<ListModel> GetAllLists()
{
using (MySqlConnection connection = new MySqlConnection(this.connStr))
{
return connection.Query<ListModel>("SELECT * FROM projectx.lists").AsList();
}
}
public ListModel GetListById(int _listId)
{
using (MySqlConnection connection = new MySqlConnection(this.connStr))
{
return connection.Query<ListModel>("SELECT * FROM projectx.lists WHERE listId = @listId",
new {listId = _listId}
).FirstOrDefault();
}
}
}
}
Here is my WordModel.cs:
using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Dapper;
using System.ComponentModel.DataAnnotations;
namespace WordList.Models
{
public class WordModel : BaseModel
{
public int wordId { get; set; }
public string listId { get; set; }
[Required(ErrorMessage = "word is required.")]
public string word { get; set; }
[Required(ErrorMessage = "Description is required.")]
public string description { get; set; }
public List<WordModel> GetWordsByListId(int _listId)
{
//this.connStr comes from the BaseModel
using (MySqlConnection connection = new MySqlConnection(this.connStr))
{
return connection.Query<WordModel>("SELECT * FROM projectx.words WHERE listId = @listId",
new { listId = _listId }
).AsList();
}
}
}
}
Here is my Action in the controller that invokes the model:
public ActionResult Index(int listId)
{
ListModel listModel = new ListModel().GetListById(listId);
if (!String.IsNullOrEmpty(listModel.name))
{
ViewData["ListName"] = listModel.name;
ViewData["ListDescription"] = listModel.description;
WordModel wordModel = new WordModel();
return View(wordModel.GetwordsByListId(realId));
}
else
{
ViewData["Message"] = "List doesn't exist";
return View();
}
}