user9671207
user9671207

Reputation: 35

Entity framework returned values manipulation

Entity framework/MVC newbie here. Writing my first EF application (api). So far so good, I can retrieve rows from the database but now I'm blocked by a problem which I can't wrap my head around. I can't figure out how to manipulate the values returned. I retrieve a resultset with 5 columns and I want to encrypt the individual values before returning it to the calling app in a JSON string. Can anyone point me to an example on where in the code to achieve this? Model? Repository? I'm lost here.

namespace app.Models
{
    public class ParameterSet
    {
        public int id { get; set; }
        public string DbServerInstance { get; set; }
        public string DbServerUser { get; set; }
        public string DbServerPassword { get; set; }
        public string DbServerDatabase { get; set; }
    }
}

Connection context

namespace app.Repositories
{
    public class DbconnectionContext : DbContext
    {
        public DbconnectionContext() : base("MobileAppsConnection")
        {
            Database.SetInitializer<DbconnectionContext>(null);
        }

        public DbSet<ParameterSet> ParameterSet { get; set; }
    }
}

interface

namespace app.Repositories
{
    interface IParameterSets
    {
        IEnumerable<ParameterSet> ListofParameterSet();
    }
}

repository

namespace MobileAppsService.Repositories
{
    public class ParameterSets : IParameterSets
    {
        public IEnumerable<ParameterSet> ListofParameterSet()
        {
            using (DbconnectionContext context = new DbconnectionContext())
            {
                var listofparameters = from parameters in context.ParameterSet
                select parameters;

                return listofparameters.ToList();
            }
        }
    }
}

values controller

namespace MobileAppsService.Controllers
{
    public class ValuesController : ApiController
    {
        readonly IParameterSets Iparamset;
        public ValuesController()
        {
            Iparamset = new ParameterSets();
        }

        // GET api/values
        public IEnumerable<ParameterSet> GetAlldata()
        {
            return Iparamset.ListofParameterSet();
        }
    }
}

Upvotes: 0

Views: 123

Answers (1)

ssilas777
ssilas777

Reputation: 9804

You should manipulate the result set in the controller before returning it to the client. you don't have to make this data manipulation in the data layer.

namespace MobileAppsService.Controllers
    {
        public class ValuesController : ApiController
        {
            readonly IParameterSets Iparamset;
            public ValuesController()
            {
                Iparamset = new ParameterSets();
            }

            // GET api/values
            public IEnumerable<ParameterSet> GetAlldata()
            {

               var paramList = Iparamset.ListofParameterSet();

               //do encryption of the paramlist here 
               //return the encrypted paramlist

               return paramList;
            }
        }
    }

Upvotes: 1

Related Questions