C# ASP.NET | Is there any point in me using a model class when saving data to MongoDB?

I have begun writing an ASP.NET Web API for an app that I am building. I have set up a MongoCRUD.cs class to save data from POST requests made by the app to a MongoDB database (and other CRUD actions).

I (following a beginner tutorial), also set up a Submission.cs model class to act as a blueprint for the objects I wanted to save to the database. However, now that I have implemented the InsertRecord() method in MongoCRUD.cs, I cannot see a use for this model.

MongoCRUD.cs:

using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebAPI.Services
{
    public class MongoCRUD
    {
        private IMongoDatabase db;

        public MongoCRUD(string database)
        {
            var client = new MongoClient();
            db = client.GetDatabase(database);
        }

        public void InsertRecord<T>(string table, T record)
        {
            var collection = db.GetCollection<T>(table);
            collection.InsertOne(record);
        }
    }
}

Submission.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace WebAPI.Models
{
    public class Submission
    {
        [BsonId]
        public string SubId { get; set; }
        public string Url { get; set; }
        public string Text { get; set; }
    }
}

SubmissionsController.cs:
using WebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI.Services;
using System.IO;
using System.Web.Script.Serialization;

namespace WebAPI.Controllers
{
    public class SubmissionsController : ApiController
    {
        MongoCRUD db = new MongoCRUD("myDb");
    
        Submission[] submission = new Submission[] {
            new Submission { SubId = "test", Url = "test", Text = "test" };
        };

        public IEnumerable<Submission> GetAllSubmissions()
        {
            //add MongoCRUD functionality for load record
            return submission;
        }

        public IHttpActionResult GetSubmission(string id)
        {
            //add MongoCRUD functionality for load record
            return Ok();
        }

        public IHttpActionResult PostSubmission(object body)
        {
            //validate body
            db.InsertRecord("Submissions", body);
            return Ok();
        }
    }
}

As you can see at PostSubmission(), the body of the POST request can be saved to the database directly, so my question is what is the benefit of using a model such as Submission.cs instead of just using the object type?

I’m having some trouble traversing the body object to access its values (for carrying out validation etc), so my only guess is that using a model makes it easier to access values?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

object is the base type for all classes (See – https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netcore-3.1).

C# is an object-orientated language so we try to model classes based on our business domain, this makes it easier to reason about the code.

Like you said you can have models that can be used to validate the incoming data into the controller, also you might want to add extra methods on the models that related to your business domain.

For example

class CreditCard
{
    string CardNumber { get;set; }
    string Csv { get;set; }

    bool ValidateChecksum()
    { }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x