Con risposta Applicazione 3-TIER ed Entity Framework

  • sabato 30 giugno 2012 16:19
     
     

    Buongiorno. Dopo vari tentativi sono riuscito a realizzare una piccola applicazione 3-TIER. Ho diviso il tutto in tre classi: VO, BLL e DAL.
    Adesso mi piacerebbe utilizzare Entity Framework all'interno della Data Access Layer. Premetto che ho già un realizzato una piccola applicazione WinForm con EF, quindi conosco le basi. Tuttavia non riesco ancora ad implementarlo in una classe separata dal resto del codice. Come si evince dalla VO utilizzo una singola Tabella Soggetti.
    Che consiglio mi date?

    // Value Object
    //
    using System;

    namespace Dbtest.VO
    {
     public class Soggetti
     {
      public int SoggettiID { get; set; }

      public string Cognome { get; set; }

      public string Nome { get; set; }

      public string Telefono { get; set; }

      public Soggetti()
      {
      }
     }
    }
    ---------------------------------------------------------
    // Business Logic Layer
    //
    using System;
    using System.Collections.Generic;
    using Dbtest.VO;
    using Dbtest.DAL;

    namespace Dbtest.BLL
    {
     ///<summary>
     ///The class that acts as a business logic layer.
     ///</summary>
        public class SoggettiBLL
        {
      /// <summary>
            /// The constructor of the business logic layer class.
            /// </summary>
            public SoggettiBLL()
            {
      }

      /// <summary>
            /// This method gets the list of the value object of a sql table.
            /// </summary>
            public List<Soggetti> SelectAll()
            {
                SoggettiDAL soggettiDAL = new SoggettiDAL();
                return soggettiDAL.SelectAll();
            }

      /// <summary>
            /// This method inserts a row in the sql table.
            /// </summary>
            /// <param name="soggetti">The value object of the sql table.</param>
            public int Insert(Soggetti soggetti)
            {
                SoggettiDAL soggettiDAL = new SoggettiDAL();
                return soggettiDAL.Insert(soggetti);
            }

      /// <summary>
            /// This method updates a row in the sql table.
            /// </summary>
      /// <param name="ID">The primary key value of the sql table.</param>
            /// <param name="soggetti">The value object of the sql table.</param>
            public int Update(int ID, Soggetti soggetti)
            {
                SoggettiDAL soggettiDAL = new SoggettiDAL();
                return soggettiDAL.Update(ID, soggetti);
            }

      /// <summary>
            /// This method deletes a row in the sql table.
            /// </summary>
      /// <param name="ID">The primary key value of the sql table.</param>
            public int Delete(int ID)
            {
                SoggettiDAL soggettiDAL = new SoggettiDAL();
                return soggettiDAL.Delete(ID);
            }
        }
    }
    --------------------------------------------------------------

    // Data Access Layer
    //
    using System;
    using Dbtest.VO;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Linq;

    namespace Dbtest.DAL
    {
     /// <summary>
        /// The class that communicates with the database.
        /// </summary>
        public class SoggettiDAL
        {
      private string connectionString = "data source=.; initial catalog=DBTEST; user id=sa; password=afrodite; Integrated Security=SSPI";

      /// <summary>
            /// The constructor of the data access layer class.
            /// </summary>
            public SoggettiDAL()
            {
            }


      /// <summary>
            /// This method inserts a row in the sql table.
            /// </summary>
            /// <param name="soggetti">The value object of the sql table.</param>
            public int Insert(Soggetti soggetti)
            {
                int rowsAffected = 0;
                SqlConnection sqlConnection = new SqlConnection(connectionString);
                try
                {
                    sqlConnection.Open();
                    string insertQuery = "INSERT INTO Soggetti([Cognome], [Nome], [Telefono]) VALUES(@Cognome, @Nome, @Telefono)";
        SqlCommand sqlCommand = new SqlCommand(insertQuery, sqlConnection);
        sqlCommand.Parameters.AddWithValue("@Cognome", soggetti.Cognome);
        sqlCommand.Parameters.AddWithValue("@Nome", soggetti.Nome);
        sqlCommand.Parameters.AddWithValue("@Telefono", soggetti.Telefono);
                    rowsAffected = sqlCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error", ex);
                }
                return rowsAffected;
            }

        }
    }

     

Tutte le risposte