I have a Sql DB with 40 tables and I built models representing each table, I am also using Dapper as my ORM...
I am building my CRUD statements within a DAL (data access layer) that will take myModelObj (coming in via HTTPPost) and modelName (coming in via URL param)
I also have a method (MyMapper) that will map my myModelObj to my models - however the method I created is of type object, which returns System.Object types - what I would like to have happen is the method return the model and convert the data type of the calling method, dynamically/at runtime, to match the model type being returned...
My code:
Controller
private readonly IDbOperation _dbo;
...
...
public DataController(IDbOperation dboperation)
{
      _dbo = dboperation;
}
...
[HttpPost("create/{modelName}", Name = "Generic CRUD: Create Method")]
[Produces("application/json")]
public ActionResult Create([FromBody] JObject createObject, string modelName)
{
  try
  {
      ... ... ...
      object response = _dbo.CreateRecord(createObject, modelName);
// HTTP Post Body example
{
    "Fullname": "Juan Carlos",
    "Firstname": "Juan",
    "Lastname": "Carlos",
    "Salutation": "Dr.",
    "Age": 30,
    "CreatedOn": "2019-11-07T12:25:10"
}
DbOperation
private readonly IDbConnector _dbConnector
...
public DbOperation(IDbConnector dbConnector)
{
      _dbConnector = dbConnector;
}
public JsonResult CreateRecord(JObject model, string modelName)
{
      ... ... ...
      var create = MyMapper(modelName, model); // <<<
      ... ...
      try
      {
            using (var connection = _dbConnector.GetMsSqlConnection())
            {
                 // https://dapper-tutorial.net/insert
                 var createdId = connection.Insert(create); 
                 ...
            }
      }
}
private object MyMapper(string modelName, JObject mod)
{
      switch (modelName.ToLower())
      {
           case "transaction":
                return JsonConvert.DeserializeObject<ModelRepoDTO.Transaction>(model.ToString());
           case "items":
                return JsonConvert.DeserializeObject<ModelRepoDTO.Items>(model.ToString());
           case "users":
                return JsonConvert.DeserializeObject<ModelRepoDTO.Users>(mod.ToString());
      ...
      ...
           default:
                _logger.Error("DataAccessLayer", "DbOperation", ">>> Mapping decision could not be made");
                break;
    }
}
DbConnector
public class DbConnector : IDbConnector
{
    private readonly string _connectionstring;
    public DbConnector(IConfiguration config)
    {
        _connectionstring = config.GetValue<string>("Connectionstring");
    }
    public SqlConnection GetMsSqlConnection()
    {
        SqlConnection conn = null;
        try
        {
            conn = new SqlConnection(_connectionstring);
            conn.Open();
            if (conn.State != ConnectionState.Open)
            {
                // throw error
            }
        }
        catch (Exception ex)
        {
            if (conn != null && conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            conn = null;
        }
        // return the connection
        return conn;
    }
}
(Currently) The data type coming back from MyMapper("User", {<Object from HTTP Post>}) is System.Object because MyMapper is of type object
What I want is to have the data type dynamically changed to match the model...
MyMapper("User", {<Object from HTTP Post>}) => data type = User
Keep in mind there are 40 different tables/models I can be CRUD'ing against, obviously I won't know which one is being called until runtime...
(to complete the example) 
...I take the object returned from MyMapper and pass it to connection.insert(...);
(I modified my post to show this - look at DbOperation)
Any suggestions? Thoughts?
 
    