c#通用存储库类

本文关键字:存储 | 更新日期: 2023-09-27 18:04:35

我有各种存储库类,它们有一个DeleteEntity方法,像这样:

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名称?

谢谢! !

c#通用存储库类

在你的位置上,我将使用模板设计模式重新编写此代码

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

前面的答案很好。我会按照适配器设计模式来做。这使我们能够集中您的工作,并允许您在不影响前端实现的情况下创建不同的实现。此模式在这里很有帮助,因为我们将可用的接口转换为需要的接口。

首先创建接口

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