使用 Web API 存储库模式编辑/更新
Edit /Update with Web API Repository Pattern
我正在尝试使用由存储库模式支持的 Web API 控制器来了解更新数据库的基础知识。到目前为止,我的一切正常 POST、GET、DELETE(创建、读取、删除)。但是我错过了更新。
下面是我的 angular 代码,我不会 post Angular Views/Templates,但只知道它们确实绑定并且工作得很好.我的问题仅出在编辑视图上,我尝试在其中使用 vm.save
函数进行更新。我的保存功能在 Angular 端工作正常,但我不确定在 Web API 和存储库端做什么。你会看到我的代码让这个工作是非常基本的骨架。我的项目的所有代码页都在此处的要点中:
以防万一你想看大图,否则我只会把我在使用 http.put
和 [= 时无法使用 Edit/Update 方法的几页放在这里52=] 控制器,Web API 控制器和存储库。
工作中Angular编辑控制器:
function editFavoriteController($http, $window, $routeParams) {
var vm = this;
var url = "/api/favorites/" + $routeParams.searchId;
$http.get(url)
.success(function (result) {
vm.search = result[0];
})
.error(function () {
alert('error/failed');
})
.then(function () {
//Nothing
});
vm.update = function (id) {
var updateUrl = "/api/favorites/" + id;
$http.put(updateUrl, vm.editFavorite)
.success(function (result) {
var editFavorite = result.data;
//TODO: merge with existing favorites
//alert("Thanks for your post");
})
.error(function () {
alert("Your broken, go fix yourself!");
})
.then(function () {
$window.location = "#/";
});
};
};
网络不工作API控制器
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite)
{
if (_favRepo.EditFavorite(id, editFavorite) && _favRepo.Save())
{
return Request.CreateResponse(HttpStatusCode.Created, editFavorite);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
存储库不工作
public bool EditFavorite(int id, Search editFavorite)
{
try
{
var search = _ctx.Search.FirstOrDefault(s => s.SearchId == id);
search(editFavorite).State = EntityState.Modified;
return true;
}
catch
{
var item = "";
}
}
工作界面
bool EditFavorite(int id, Search newSearch);
同样,我唯一的问题是弄清楚如何在 Web 中进行更新API FavoritesController
和 FavoritesRepository
。我有例子说明我是如何在 Gist 中完成其他所有事情的,所以我希望有人能帮助我。我只是碰壁我知道如何在 Web API.
固定码:
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite)
{
if (_favRepo.EditFavorite(id, editFavorite))
{
_favRepo.Save()
return Request.CreateResponse(HttpStatusCode.Created, editFavorite);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
我也在发布代码,这些代码应该可以很好地处理使用 WEB API 和存储库模式在服务器端进行的编辑。
WebAPI 控制器:
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite)
{
if (!ModelState.IsValid || id != editFavorite.Id)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
db.EditFavorite(editFavorite);
try
{
db.Save();
}
catch (DbUpdateConcurrencyException)
{
if (!db.SearchExists(id))
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
else
{
throw;
}
}
return Request.CreateResponse(HttpStatusCode.Created, editFavorite);
}
存储库方法:
public void EditFavorite(Search editFavorite)
{
db.Entry(editFavorite).State = EntityState.Modified;
}
public void Save()
{
db.SaveChanges();
}
public bool SearchExists(int id)
{
return db.Search.Count(e => e.Id == id) > 0;
}
修改接口:
void Save();
void EditFavorite(Search newSearch);
bool SearchExists(int id);
编辑:
我做了一些更改,以便只有在您的数据库上下文中执行的操作才会在存储库层(数据层)中完成,并且错误检查在 WEB API Controller
中完成。
建议:
您应该在接口上继承 IDisposable
并在您的存储库 class 中实现它,以便正确处理您的实体...
public interface IFavoritesRepository : IDisposable
{
// code here
}
public class FavoritesRepository : IFavoritesRepository
{
// code here
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
db.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
我正在尝试使用由存储库模式支持的 Web API 控制器来了解更新数据库的基础知识。到目前为止,我的一切正常 POST、GET、DELETE(创建、读取、删除)。但是我错过了更新。
下面是我的 angular 代码,我不会 post Angular Views/Templates,但只知道它们确实绑定并且工作得很好.我的问题仅出在编辑视图上,我尝试在其中使用 vm.save
函数进行更新。我的保存功能在 Angular 端工作正常,但我不确定在 Web API 和存储库端做什么。你会看到我的代码让这个工作是非常基本的骨架。我的项目的所有代码页都在此处的要点中:
以防万一你想看大图,否则我只会把我在使用 http.put
和 [= 时无法使用 Edit/Update 方法的几页放在这里52=] 控制器,Web API 控制器和存储库。
工作中Angular编辑控制器:
function editFavoriteController($http, $window, $routeParams) {
var vm = this;
var url = "/api/favorites/" + $routeParams.searchId;
$http.get(url)
.success(function (result) {
vm.search = result[0];
})
.error(function () {
alert('error/failed');
})
.then(function () {
//Nothing
});
vm.update = function (id) {
var updateUrl = "/api/favorites/" + id;
$http.put(updateUrl, vm.editFavorite)
.success(function (result) {
var editFavorite = result.data;
//TODO: merge with existing favorites
//alert("Thanks for your post");
})
.error(function () {
alert("Your broken, go fix yourself!");
})
.then(function () {
$window.location = "#/";
});
};
};
网络不工作API控制器
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite)
{
if (_favRepo.EditFavorite(id, editFavorite) && _favRepo.Save())
{
return Request.CreateResponse(HttpStatusCode.Created, editFavorite);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
存储库不工作
public bool EditFavorite(int id, Search editFavorite)
{
try
{
var search = _ctx.Search.FirstOrDefault(s => s.SearchId == id);
search(editFavorite).State = EntityState.Modified;
return true;
}
catch
{
var item = "";
}
}
工作界面
bool EditFavorite(int id, Search newSearch);
同样,我唯一的问题是弄清楚如何在 Web 中进行更新API FavoritesController
和 FavoritesRepository
。我有例子说明我是如何在 Gist 中完成其他所有事情的,所以我希望有人能帮助我。我只是碰壁我知道如何在 Web API.
固定码:
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite)
{
if (_favRepo.EditFavorite(id, editFavorite))
{
_favRepo.Save()
return Request.CreateResponse(HttpStatusCode.Created, editFavorite);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
我也在发布代码,这些代码应该可以很好地处理使用 WEB API 和存储库模式在服务器端进行的编辑。
WebAPI 控制器:
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite)
{
if (!ModelState.IsValid || id != editFavorite.Id)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
db.EditFavorite(editFavorite);
try
{
db.Save();
}
catch (DbUpdateConcurrencyException)
{
if (!db.SearchExists(id))
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
else
{
throw;
}
}
return Request.CreateResponse(HttpStatusCode.Created, editFavorite);
}
存储库方法:
public void EditFavorite(Search editFavorite)
{
db.Entry(editFavorite).State = EntityState.Modified;
}
public void Save()
{
db.SaveChanges();
}
public bool SearchExists(int id)
{
return db.Search.Count(e => e.Id == id) > 0;
}
修改接口:
void Save();
void EditFavorite(Search newSearch);
bool SearchExists(int id);
编辑:
我做了一些更改,以便只有在您的数据库上下文中执行的操作才会在存储库层(数据层)中完成,并且错误检查在 WEB API Controller
中完成。
建议:
您应该在接口上继承 IDisposable
并在您的存储库 class 中实现它,以便正确处理您的实体...
public interface IFavoritesRepository : IDisposable
{
// code here
}
public class FavoritesRepository : IFavoritesRepository
{
// code here
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
db.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}