我有各种具有DeleteEntity方法的存储库clases:
public void DeleteEntity(int Key)
{
try
{
ObjectParameter error = new ObjectParameter("Error", typeof(string));
context.proc_BorrarChofer(Key, error);
if (error.Value.ToString() != "")
{
Errores myerror = new Errores();
myerror.ID = 100;
myerror.Descripcion = error.Value.ToString();
MyErrors.Add(myerror);
}
}
catch (DbUpdateConcurrencyException ex)
{
// Update the values of the entity that failed to save from the store
ex.Entries.Single().Reload();
// status = ex.Message;
Errores myerror = new Errores();
myerror.ID = 1000;
myerror.Descripcion = ex.Message.ToString();
MyErrors.Add(myerror);
}
catch (DbUpdateException ex)
{
string status = (ex.InnerException.InnerException != null) ? ex.InnerException.InnerException.Message : "";
Errores myerror = new Errores();
myerror.ID = 1000;
myerror.Descripcion = status;
MyErrors.Add(myerror);
}
catch (Exception ex)
{
//paso los errores
Errores myerror = new Errores();
myerror.ID = 1000;
myerror.Descripcion = ex.Message.ToString();
MyErrors.Add(myerror);
}
现在每个Repository类都有相同的方法。只有存储过程的名称发生更改。如何创建通用删除方法并作为参数sp名称传递?
感谢!!!
答案 0 :(得分:3)
在你的地方,我会使用模板设计模式重新制作这段代码
public abstract class BaseRepository
{
protected abstract void DeleteEntityWithProcedure(int key, ObjectParameter error);
public void DeleteEntity(int Key)
{
try
{
ObjectParameter error = new ObjectParameter("Error", typeof(string));
DeleteEntityWithProcedure(key, error);
if (error.Value.ToString() != "")
{
Errores myerror = new Errores();
myerror.ID = 100;
myerror.Descripcion = error.Value.ToString();
MyErrors.Add(myerror);
}
}
catch (DbUpdateConcurrencyException ex)
{
// Update the values of the entity that failed to save from the store
ex.Entries.Single().Reload();
// status = ex.Message;
Errores myerror = new Errores();
myerror.ID = 1000;
myerror.Descripcion = ex.Message.ToString();
MyErrors.Add(myerror);
}
catch (DbUpdateException ex)
{
string status = (ex.InnerException.InnerException != null) ? ex.InnerException.InnerException.Message : "";
Errores myerror = new Errores();
myerror.ID = 1000;
myerror.Descripcion = status;
MyErrors.Add(myerror);
}
catch (Exception ex)
{
//paso los errores
Errores myerror = new Errores();
myerror.ID = 1000;
myerror.Descripcion = ex.Message.ToString();
MyErrors.Add(myerror);
}
}
您的具体存储库将是这样的
public class CustomerRepository:BaseRepository
{
protected override DeleteEntityWithProcedure(int key, ObjectParameter error)
{
//execute procedure you needed
//context.proc_BorrarChofer(Key, error);
}
}
执行自定义存储库
var customerRepository = new CustomerRepository();
customerRepository.DeleteEntity(1);
答案 1 :(得分:0)
之前的答案很好用。我会按照适配器设计模式来做。这个允许我们集中您的工作,并允许您创建不同的实现,而不会影响您的前端实现。这种模式在这里很有用,因为我们将可用的接口转换为所需的接口。
首先我们创建界面
public interface IEntity
{
void DeleteEntity(int key, Error error);
}
然后我们根据需要创建尽可能多的存储库。例如:ChoferRepository或otroRepository:
public class ChoferRepository: IEntity
{
public void DeleteEntity(int key, Error error)
{
// todo: Add your custom code here.
// You coul implement not only any store procedure but any data source
}
}
public class OtroRepository: IEntity
{
public void DeleteEntity(int key, Error error)
{
// todo: Add your custom code here.
// You coul implement not only any store procedure but any data source
}
}
最后是我们的实施。
/// <summary>
/// This is just an example on how to implement your generic approach using the adapter pattern
/// </summary>
public class WebClient
{
IEntity ChoferEntity { get; set; }
IEntity OtraEntity { get; set; }
public WebClient()
{
//You can use as many repositories as you want knowing they all could have different data sources or different store procedures inside same data source
ChoferEntity.DeleteEntity(1, new Error());
OtraEntity.DeleteEntity(1, new Error());
}
}