CRUD WEB API с инфраструктурой сущностейC#

Место общения программистов C#
Ответить
Anonymous
 CRUD WEB API с инфраструктурой сущностей

Сообщение Anonymous »

Я начал разработку своего проекта — веб-приложения с базой данных. Я использовал WEB API с инфраструктурой сущностей.
Мне нужно реализовать операции CRUD в моем проекте.

Чтение — работает нормально

Но я не знаю, как реализовать «Создать», «Обновить», «Удалить»; У меня недостаточно опыта, и я буду рад вашему совету.

Я попытался реализовать хорошую архитектуру своего приложения - используя шаблон репозитория и шаблон структуры. Если у вас есть советы по архитектуре моего проекта, я буду вам благодарен.

Я не знаю, как это реализовать в контроллере значений и репозитории, не могли бы вы помочь?

Прикрепить мой код:

Репозиторий

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebAPI
{
public class CustomerRepository
{
public IQueryable GetAllCustomers()
{
DevelopersEntities dev = new DevelopersEntities();
return dev.Customers;
}

public IQueryable GetAllCustomers(int id)
{
DevelopersEntities dev = new DevelopersEntities();
return dev.Customers.Where(c=>c.Id==id).Select(e=>e);
}

public IQueryable DeleteCustomer(int id)
{
DevelopersEntities dev = new DevelopersEntities();
return dev.Customers.Remove(id);
}

public IQueryable CreateCustomer()
{
DevelopersEntities dev = new DevelopersEntities();

}

public IQueryable 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 Hardware { get; set; }
public IEnumerable 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 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)
{
}
}
}



Подробнее здесь: https://stackoverflow.com/questions/622 ... -framework
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»