0

我有使用与一组 tsql 存储过程通信的 DAL (ADO.NET) 的 MVC 3 应用程序?我想在我当前的解决方案中添加一个新的 MVC 项目。我需要在 2 MVC 项目(“监视器”和“审计”)可以共享的单独项目中拥有 DAL。

下面是当前的 DAL(位于“Monitor”MVC 项目的文件夹中)代码。我的问题是我的签名像 IEnumerable 位于 Monitor.Models 和 IEnumerable 位于 Audit.Models 中。我是否需要将 DAL 设为通用以避免需要引用 DAL 中的模型?

Ex: 
**//Is this bad practice?**
    using Monitor.Models;
    using Adit.Models;



namespace Monitor.DAL
{
    public class QuestionDAL
    {
        static ILog log = log4net.LogManager.GetLogger(typeof(QuestionDAL));
        private string _connectionString = WebConfigurationManager.ConnectionStrings["NexGenContext"].ToString();

        public IEnumerable<AgencyTerm> SearchAgencies(string ori, string name)
        {
            log.Debug("Executing: SearchAgencies(string ori, string name)");
            List<AgencyTerm> agencies = new List<AgencyTerm>();
            using (var conn = new SqlConnection(_connectionString))
            {
                var com = new SqlCommand();
                com.Connection = conn;
                com.CommandType = CommandType.StoredProcedure;
                string term = "Ori";

                if (!String.IsNullOrEmpty(ori)) 
                {
                   term = "Ori";
                   com.Parameters.Add(new SqlParameter
                   {
                        ParameterName = "@ORI",
                        Value = ori
                    });
                }
                if (!String.IsNullOrEmpty(name))
                {
                    term = "legal_name";
                    com.Parameters.Add(new SqlParameter
                    {
                        ParameterName = "@Name",
                        Value = name
                    });
                }
                com.CommandText = "Review_Get_Agency_List";
                var adapt = new SqlDataAdapter();
                adapt.SelectCommand = com;
                var dataset = new DataSet();
                adapt.Fill(dataset);

                agencies = (from c in dataset.Tables[0].AsEnumerable()
                            select new AgencyTerm()
                                         {
                                             label = c[term].ToString(),
                                             id = c["Agency_Id"].ToString()
                                         }).ToList<AgencyTerm>();

                return agencies;
            }

        }

        public IEnumerable<User> GetUsers()
        {
            log.Debug("Executing: GetUsers()");
            List<User> users = new List<User>();
            using (var conn = new SqlConnection(_connectionString))
            {
                var com = new SqlCommand();
                com.Connection = conn;
                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = "Review_Get_Users";
                var adapt = new SqlDataAdapter();
                adapt.SelectCommand = com;
                var dataset = new DataSet();
                adapt.Fill(dataset);

                users = (from c in dataset.Tables[0].AsEnumerable()
                            select new User()
                            {
                                User_ID = Convert.ToInt32(c["User_ID"]),
                                Department = c["Department"].ToString(),
                                Enabled = Convert.ToBoolean(c["Enabled"]),
                                Email = c["Email"].ToString(),
                                User_First_Name = c["User_First_Name"].ToString(),
                                User_Last_Name = c["User_Last_Name"].ToString(),
                                Location = c["Location"].ToString(),
                                User_Name = c["User_Name"].ToString()
                            }).ToList<User>();

                return users;
            }

        } 
4

1 回答 1

1

你有两种可能:

  • 将您的模型也移动到单独的库中,然后从您的 MVC 项目和 DAL 中引用它
  • 使您的 DAL 完全通用,只需将值推入内部。不过,我在这里看不到简单的方法,因为您的 DAL 中有很多信息

我会选择第一个选项。只需将您的模型提取到不同的项目,然后在 DAL 和 MVC 项目中重用该库

于 2013-05-08T19:11:35.380 回答