如何在我的MVC3解决方案中将我的DAL移动到单独的项目中?

时间:2013-05-08 19:00:44

标签: asp.net-mvc-3 ado.net shared-libraries data-access-layer

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

以下是当前DAL(位于“Monitor”MVC项目的文件夹中)代码。我的问题是我有像IEnumerable这样的签名,位于Monitor.Models和位于Audit.Models中的IEnumerable。我是否需要使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;
            }

        } 

1 个答案:

答案 0 :(得分:1)

您有两种可能性:

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

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