如何使用存储库模式在 Asp.net 核心中执行原始 SQL

问题描述

我正在尝试使用存储库模式在 ASP.net 核心中执行 SELECT sql 语句,我将语句传递给存储库接口的实现,然后我想获取从该语句生成的列表。 以下代码片段显示了 repo 接口

 public interface IRepository<T> where T : class
    {
        List<T> GetAll(Expression<Func<T,DateTime?>> filter = null);
        IQueryable<T> Find(Expression<Func<T,bool>> expression);
        void Add(T entity);
        void AddRange(IEnumerable<T> entities);
        void Update(T entity);
        void Updaterange(IEnumerable<T> entities);
        void Remove(T entity);
        void RemoveRange(IEnumerable<T> entities);
        List<T> sql(string statment);
    }

界面中sql方法的实现如图:

public class Repository<T> : IRepository<T> where T : class
    {

        private APPDBContext context;
        private readonly DbSet<T> dbSet;
      
        public Repository(APPDBContext _context) 
        {
            dbSet = _context.Set<T>();
            context = _context;
        }
        
         public List<T> sql(string state)
       {
           return dbSet.FromsqlRaw(state).ToList();
       }
}

要使用此方法,使用工作单元创建组件类,如下所示:

public class MyComponent : ComponentBase
    {
       
         public List<Entity> Search(string sql)
         {
             try
             {
               List<Entity> result = base.UnitOfWork.MyComponent.sql(sql);
               return result;
              
           }
           catch (Exception ex)
             {
                 return null;
             }
         }

    }

其中 ComponentBase 类如下所示:

public class ComponentBase
    {
        public UofW UnitOfWork { get; private set; }
        public ComponentBase() 
        {
            if(UnitOfWork == null)
                UnitOfWork = new UofW(new APPDBContext());
        }

        public int Save() 
        {
            int ret = UnitOfWork.Save();
            UnitOfWork.dispose();
            return ret;
        }
    }

UofW 类如图所示:

public class UofW : IUofW
    {
        private readonly APPDBContext context;

        public UofW(APPDBContext _context)
        {
            context = _context;
        }
          
        private IRepository<Entity> _MyComponent;
        public IRepository<Entity> MyComponent
        {
            get
            {
                if(_MyComponent == null)
                    _MyComponent = new Repository<Entity>(context);
                return _MyComponent;
            }
        }
         
         

        public int Save()
        {
            return context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.dispose();
                }
            }
            this.disposed = true;
        }

        public void dispose()
        {
            dispose(true);
            GC.SuppressFinalize(this);
        }

    }

IUofW 是:


    public interface IUofW : Idisposable
    {
        IRepository<Entity> MyComponent { get; }

        int Save();

       
    }

最后,我向控制器发送请求以形成我的语句并按如下所示执行它:


        [HttpPost("Search")]
        public ActionResult Search(Entity object)
        {
            var response = new Response<Entity>();
            var MyComponent = new MyComponent();
            if (object == null)
            {
                response.DidError = true;
                response.ErrorMessage = "invalid data";

                return StatusCode((int)HttpStatusCode.BadRequest,response);
            }

             try
             {
                string cndtn = "";
                 if (object.column1 != null && object.column1 != "") { if (cndtn == "") { cndtn = cndtn + ""; } else { cndtn = cndtn + " and "; } cndtn = cndtn + " column1='" + object.column1 + "' "; };
                if (object.column2 != null && object.column2 != "") { if (cndtn == "") { cndtn = cndtn + ""; } else { cndtn = cndtn + " and "; } cndtn = cndtn + " Brd_No='" + object.column2 + "' "; };
                if (object.column3 != null && object.column3 != "") { if (cndtn == "") { cndtn = cndtn + ""; } else { cndtn = cndtn + " and "; } cndtn = cndtn + " column3='" + object.column3 + "' "; };
            
                string sql = "SELECT * FROM MyTable";
                if(cndtn == "") { sql = sql + "";} else { sql = sql + " WHERE " + cndtn; }

                response.ModelList = MyComponent.Search(sql);
                
                return Ok(response);
            }
             catch (Exception ex)
             {
                 response.DidError = true;
                 response.ErrorMessage = ex.Message;

                 return StatusCode((int)HttpStatusCode.InternalServerError,response);
             }
        }

sql 语句的格式正确,但是当我将它传递给 Search 方法时,响应为空,这意味着我的语句没有执行...那么我的代码有什么问题,我该如何执行该语句??

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...