繁体   English   中英

CRUD WEB API 与实体框架

[英]CRUD WEB API with entity framework

我开始使用数据库开发我的项目 - web 应用程序。 我将 WEB API 与我需要在项目中实现 CRUD 操作的实体框架一起使用。

阅读 - 工作正常

但我不知道如何实现创建、更新、删除; 我没有足够的经验,很高兴你的建议。

我尝试实现我的应用程序的良好架构 - 使用存储库模式和结构模式。 如果您对我的项目架构有任何建议,我将不胜感激。

我不知道如何在价值 controller 和存储库中实现它,你能帮忙吗?

附上我的代码:

存储库

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();

        }
    }
}

客户 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; }
    }
}

硬件 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; }

    }
}

软件 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 工厂

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
            };
        }
    }
}

值 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)
        {
        }
    }
}

以下是您应该如何保存客户 object:

public void CreateCustomer(Customer customer)
{
    DevelopersEntities dev = new DevelopersEntities();
    dev.Customers.Add(customer)
    dev.SaveChanges();
}

这是您的 api 操作:

  // POST api/values
    public void Post([FromBody]CustomerModel customerModel)
    {
       //Here you should transform CustomerModel object to customerEntity
        CustomerRepository cr = new CustomerRepository();
        cr.CreateCusomer(customer);

        return Ok();
    }

您也应该考虑在这里使用依赖注入模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM