What is CRUD operation PART 2 With Example Code

Create operation involves adding new records or entities to a database. This could include inserting a new row into a table, creating a new document in a document-based database, or adding a new object to a collection in an object oriented database.
For example, in a web application, creating a new user account or adding a new product to an inventory would be considered create operations.






namespace FreshWebApp.Repository
{
    public interface IRepositoryEF
    {
        T Single<T>(string procedureName, DynamicParameters param = null);
 
        void Execute(string procedureName, DynamicParameters param = null);
 
        T OneRecord<T>(string procedureName, DynamicParameters param = null);
 
        IEnumerable<T> List<T>(string procedureName, DynamicParameters param = null);
 
        Tuple<IEnumerable<T1>, IEnumerable<T2>> List<T1, T2>(string procedureName, DynamicParameters param = null);
 
        Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>> List<T1, T2, T3>(string procedureName, DynamicParameters param = null);
    }
}





using FreshWebApp.Data;
 
namespace FreshWebApp.Repository
{
    public class RepositoryEF : IRepositoryEF
    {
        private readonly AmazonFreshWebAppDBContext _db;
        private static string ConnectionString = "";
 
        public RepositoryEF(AmazonFreshWebAppDBContext db)
        {
            _db = db;
            ConnectionString = db.Database.GetDbConnection().ConnectionString;
        }
 
        public void Dispose()
        {
            _db.Dispose();
        }
 
        public T Single<T>(string procedureName, DynamicParameters param = null)
        {
            using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
                return (T)Convert.ChangeType(sqlCon.ExecuteScalar<T>(procedureName, param, commandType: System.Data.CommandType.StoredProcedure), typeof(T));
            }
        }
 
        public void Execute(string procedureName, DynamicParameters param = null)
        {
            using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
                sqlCon.Execute(procedureName, param, commandType: System.Data.CommandType.StoredProcedure);
            }
        }
 
        public T OneRecord<T>(string procedureName, DynamicParameters param = null)
        {
            using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
                var value = sqlCon.Query<T>(procedureName, param, commandType: System.Data.CommandType.StoredProcedure);
                return (T)Convert.ChangeType(value.FirstOrDefault(), typeof(T));
            }
        }
 
        public IEnumerable<T> List<T>(string procedureName, DynamicParameters param = null)
        {
            using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
                return sqlCon.Query<T>(procedureName, param, commandType: System.Data.CommandType.StoredProcedure);
            }
        }
 
        public Tuple<IEnumerable<T1>, IEnumerable<T2>> List<T1, T2>(string procedureName, DynamicParameters param = null)
        {
            using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
                var result = SqlMapper.QueryMultiple(sqlCon, procedureName, param, commandType: System.Data.CommandType.StoredProcedure);
                var item1 = result.Read<T1>().ToList();
                var item2 = result.Read<T2>().ToList();
 
                if (item1 != null && item2 != null)
                {
                    return new Tuple<IEnumerable<T1>, IEnumerable<T2>>(item1, item2);
                }
 
            }
 
            return new Tuple<IEnumerable<T1>, IEnumerable<T2>>(new List<T1>(), new List<T2>());
        }
 
        public (IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>) ExecuteStoredProc<T1, T2, T3>(string procedureName, DynamicParameters param = null)
        {
            using (var sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
 
                using (var multiResult = sqlCon.QueryMultiple(procedureName, param, commandType: CommandType.StoredProcedure))
                {
                    var item1 = multiResult.Read<T1>().ToList();
                    var item2 = multiResult.Read<T2>().ToList();
                    var item3 = multiResult.Read<T3>().ToList();
 
                    if (item1 != null && item1.Any() && item2 != null && item2.Any() && item3 != null && item3.Any())
                    {
                        return new Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>>(item1, item2, item3);
                    }
                }
            }
            return new Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>>(new List<T1>(), new List<T2>(), new List<T3>());
        }
 
    }
}