I'm writing a blog system as a learning exercise and I would like to allow the admin (or blog owner) to select/change some custom options based on features that I choose to expose to them such as:
- change blog name (string)
- select layout (int)
- number of blog entries per page (int)
- more stuff like this...
The only way that I can really think of to do this is by creating a class, with a property for each option and letting the owner get and set the properties.
What are my options for exposing customization settings to the admin / owner?
This is how I've implemented it for the time-being, although I am aware that this may not be the best way to do this:
I've created a class called Setting.cs
public class Setting : IEntity
{
[Key]
public int Id { get; set; }
public string BlogName { get; set; }
public string SubTitle { get; set; }
public int PostsPerPage { get; set; }
}
Then I added a settings method to my Admin Dashboard controller like this:
public ActionResult Settings(int? id = 1)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Setting set = db.Settings.Find(id);
if (set == null)
{
return HttpNotFound();
}
return View(set);
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Settings([Bind(Include = "Id,BlogName,SubTitle,PostsPerPage")] Setting sets)
{
if (ModelState.IsValid)
{
db.Entry(sets).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index", "Dashboard");
}
return View(sets);
}
I then seed the record and just retrieve the same record and update it with the new settings each time.
I suppose I should state my concerns:
- It seems strange to store a single row of data in it's own table of my database.
- It also seems strange that if one wanted to, one could instantiate the Settings.cs class and possibly make multiple records of it.
- I've hard-coded my controller with the record ID, as well as any calls to the controller, which to me feels like I'm abusing the intended purpose of using a controller by hard coding it.
I'm new to programming but this doesn't seem right to me. In Swift iOS programming I could create a pList (flat-file) or store settings in the NSUserDefaults. I'm just wondering what my options are in ASP.NET MVC.
I originally wanted to ask "What's the best way to implement admin/owner settings?" but I didn't want my question closed as being subjective. I am, however, hoping to deduce what the preferred method is ultimately to adhere to best practices.