Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3755

CRUD WEB API with entity framework

$
0
0

I started develop my project - web application with Database. I used WEB API with entity frameworkI need CRUD operations realize in my project.

Read - work fine

But I don't know how realize Create, Update, Delete; I don't have enough experience and be glad yours advice.

I tried realize good architecture of my application - using repository pattern and fabric pattern. If you have advice in architecture of my project , I'll be grateful you.

I don't know how realize it in value controller and repository, could you help please?

Attach my code:

Repository

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace WebAPI{    public class CustomerRepository    {        public IQueryable<Customer> GetAllCustomers()        {            DevelopersEntities dev = new DevelopersEntities();            return dev.Customers;        }        public IQueryable<Customer> GetAllCustomers(int id)        {            DevelopersEntities dev = new DevelopersEntities();            return dev.Customers.Where(c=>c.Id==id).Select(e=>e);        }        public IQueryable<Customer> DeleteCustomer(int id)        {            DevelopersEntities dev = new DevelopersEntities();            return dev.Customers.Remove(id);        }        public IQueryable<Customer> CreateCustomer()        {            DevelopersEntities dev = new DevelopersEntities();        }        public IQueryable<Customer> UpdateCustomer(int id)        {            DevelopersEntities dev = new DevelopersEntities();        }    }}

Customer model

using System;using System.Collections.Generic;using System.Linq;using System.Web;using WebAPI;namespace DevelopersWeb.Models{    public class CustomerModel    {        public int CustomerId { get; set; }        public string CustomerName { get; set; }        public IEnumerable<HardwareModel> Hardware { get; set; }        public IEnumerable<SoftwareModel> Software { get; set; }    }}

Harware Model

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace DevelopersWeb.Models{    public class HardwareModel    {        public int HardwareId { get; set; }        public string HardwareName { get; set; }    }}

Software Model

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace DevelopersWeb.Models{    public class SoftwareModel    {        public int SoftwareId { get; set; }        public string SoftwareName { get; set; }    }}

Model Factory

using System;using System.Collections.Generic;using System.Linq;using System.Web;using WebAPI;namespace DevelopersWeb.Models{    public class ModelFactory    {        public CustomerModel Create(Customer customer)        {            return new CustomerModel()            {                CustomerId = customer.Id,                CustomerName = customer.Name,                Hardware = customer.HardWares.Select(h=>Create(h)),                Software = customer.Softwares.Select(c=>Create(c))            };        }        public HardwareModel Create(HardWare hardware)        {            return new HardwareModel()            {                HardwareId = hardware.HardWareId,                HardwareName = hardware.HardWareName,            };        }        public SoftwareModel Create(Software software)        {            return new SoftwareModel()            {                SoftwareId = software.SoftwareId,                SoftwareName = software.SoftwareName            };        }    }}

Value Controller

using DevelopersWeb.Models;using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using WebAPI;namespace DevelopersWeb.Controllers{    public class ValuesController : ApiController    {        ModelFactory _modelFactory;        public ValuesController()        {            _modelFactory = new ModelFactory();        }        // GET api/values        public IEnumerable<CustomerModel> Get()        {            CustomerRepository cr = new CustomerRepository();            return cr.GetAllCustomers().ToList().Select(c=> _modelFactory.Create(c));        }        // GET api/values/5        public string Get(int id)        {            return "xxx";        }        // POST api/values        public void Post([FromBody]string value)        {        }        // PUT api/values/5        public void Put(int id, [FromBody]string value)        {        }        // DELETE api/values/5        public void Delete(int id)        {        }    }}

Viewing all articles
Browse latest Browse all 3755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>