Search code examples
asp.net-mvc-3ado.netshared-librariesdata-access-layer

How to want to move my DAL to separate project in my MVC3 solution?


I have MVC 3 application that uses a DAL (ADO.NET) that communicates to a set of tsql stored procedures? I want to add a new MVC project to my current solution. I need to have DAL in a separate project that the 2 MVC project ("Monitor" and "Audit") can share.

Here's the current DAL (which sits in a folder of the "Monitor" MVC project) code below. My issue is I have signature like IEnumerable located in the Monitor.Models and IEnumerable located in the Audit.Models. Do I need to make the DAL generic to avoid needing to make references to the models in the 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;
            }

        } 

Solution

  • You have two possibilities:

    • Either move your model into a separate library as well and then reference it from your MVC project and your DAL
    • Make your DAL completely generic and just push the values inside. I don't see an easy way here though since you have a lot of information in your DAL

    I would go with the first option. Just extract your models to a different project and then reuse that library in both DAL and MVC-projects