crudwebapi实体框架

xwmevbvl  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(371)

我开始用数据库开发我的项目web应用程序。我使用了webapi和实体框架,我需要在我的项目中实现crud操作。
读书-好好工作
但我不知道如何实现创建、更新、删除;我没有足够的经验,很高兴你的建议。
我尝试使用存储库模式和结构模式来实现我的应用程序的良好体系结构。如果你对我的项目有建筑学方面的建议,我将不胜感激。
我不知道如何在值控制器和存储库中实现它,您能帮忙吗?
附上我的代码:
存储库

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

        }
    }
}

客户模型

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

硬件模型

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

    }
}

软件模型

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

模型工厂

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

值控制器

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

o2gm4chl1#

以下是保存客户对象的方法:

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

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

相关问题