0

In a 3 tier architecture (C# , ADO.NET), are datasets and datatables the only option to return data from the Data Layer to the Presentation Layer?. I've been working with Datatables but when I will ttry to do this

public User getUserByP(User user)
    {
        User t = new User();
        using(SqlConnection con = new SqlConnection(Conexion.Cn))
        {
            con.Open();
            SqlCommand command = new SqlCommand("spLogIn_User", con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@user", User.user);
            command.Parameters.AddWithValue("@password", User.Pass);
            SqlDataReader reader =  command.ExecuteReader();
            while (reader.Read())
            {
                t.IdUser = reader.GetInt32(0);
                t.Name = reader.GetString(1);
                t.LastName = reader.GetString(2);
                t.Access = reader.GetString(3);
                t.user = reader.GetString(4);
                t.Pass = reader.GetString(5);
            }
        }
        return t;
    }

I ill have an error cause there is no communication between Data Layer and Presentation Layer. This is posible in MVC (i think) but no here. So if a just want return even 1 result , datatble or dataset are only option?

  • No, of course they are not the only options. There are many others like Linq-To-Sql, Linq-To-Entities, Dapper.Net, manually filling custom classes etc.pp – Tim Schmelter May 06 '16 at 12:35
  • How is your question related to `DataSet`/`DataTable` at all? You are not using one. What is the exact error and it's message and where is it raised? – Tim Schmelter May 06 '16 at 12:36
  • This was a example , if i want return an obejct from de datalayer to my presentation layer , it wont wok cause i cant have Entities in my Presentation layer unless i make a reference from de presentation dll to de data dll. – Lucas Rodr May 06 '16 at 21:37
  • Contrary to what most developers seem to ~think, the answer is a big NO. In fact, DataSets/DataTables are very early 2000's, maybe mid-2000's. There are such better ways now. Since Entity-Framework has come out, I now try to make object-libraries (Poco's), and I use either DataReaders or EF to populate the Poco's. – granadaCoder Feb 23 '17 at 20:06

1 Answers1

1

No. There are other alternatives to retrieve data from databases into your data layer.

  • Linq to sql
  • Entity Framework
  • nHibernate
  • Dapper.Net

If on the other hand, you want to know how to access the data layer from the presentation layer without additional dependencies, you could do the following.

  1. In the data layer, always return a simple object instead of an specialized type (datatable/dataset) to avoid unnecessary dependencies to system.data or other dlls

  2. Add a business layer. In a 3 tier application, this layer uses the data from the data layer and adds validation, business rules and logic to access and manipulate that data.

  3. Reference the business layer in the presentation layer to display the information desired.

For more details on how to achieve a 3-tier/layer architecture refer to this explanation

Community
  • 1
  • 1
RaulMonteroc
  • 176
  • 14
  • I was thinking that (about point 2), but it doesnt violates the 3 tier rule?. As i said in MVC , we can have Entities references in our views but here its possible? – Lucas Rodr May 06 '16 at 21:41
  • 1
    I have updated my answer. Please check it again. Basically, there is no problem referencing a business layer inside the presentation layer, that's how the 3 layer/tier architecture is defined. For more details, please check out this link http://stackoverflow.com/a/13786596/2332687 – RaulMonteroc May 12 '16 at 12:48