none
Make one class that acts as a tunnel for other classes method RRS feed

  • Question

  • Well I have  a bunch of solution in a project. And one of those is for data access. I'm creating a different class for each of the tables of the database and each class will only handle that table (I'm trying).

    This is the main DA class

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using MySql.Data.MySqlClient;
    
    
    
    namespace AcessoDeDados
    {
        public static class AcessoBD
        {
         // the first AccessoAutor is a type
       public static AcessoAutor AcessoAutor { get; set; }
    
    
    
        }
    
    }

    This is the class I want to "privatize" from the public and make it only available though the AcessoBD class

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using MySql.Data.MySqlClient;
    
    namespace AcessoDeDados
    {
        public class AcessoAutor
        {
            //nomeCompleto, numeroBI, idCategoriaAutor
    
    
       
            static string conStringMySQL = "BibliotecaConnectionString";
    
    private AcessoAutor(){
    }
    
      public IList<string> GetAuthorCategories(){
                IList<string> categorias = new List<string>();
    
    //code here
    return categorias;
    }
    
        }
    }


    So I made the constructor private and I'm trying to access it like this in the main 

     static void Main(string[] args)
            {
    
    var resultado1 = AcessoBD.AcessoAutor.GetAuthorCategories();
    
      foreach (var cat in resultado1)
                {
                    Console.WriteLine(cat);
                }
    
    Console.ReadKey();
    
    }

    but it returns null, idk why.

    But if I make the constructor public and create a new instance

     AcessoAutor a= new AcessoAutor();
                var resultado= a.GetAuthorCategories();
    
                foreach (var cat in resultado)
                {
                    Console.WriteLine(cat);
                }
    it works and give the result well. There's no problem on the connection, as I said it work one way, but other it doesn't.

    I could make it static, but if I did, I would only be able to access from the class name.

    I want to make something like the first approach, the first class, will have properties of the other and though these properties I will call the methods.

    Any guesses?

    PS: If u think u need more info, just ask.


    BP-LP 2005/2016 @ll rights reserved



    Tuesday, December 11, 2018 9:36 AM

Answers

  • If I understand you want to have only one instance to access into DB layer. But there (public static AcessoAutor AcessoAutor { get; set; })

    is only property declaration. You need create instance somewhere. It was every time null without creating instance.

    For creating data layer, you can use two models. First is repository. It could be as large hammer in small or low complexity project. Second is DAO pattern. It is very useful for small projects. There are methods which takes CRUD operation (i.e. GetByPrimaryKey(), GetAll(), GetByCustomers(), Insert(), Update(), Delete()). These methods works with some objects. It is depend on what you need. It could be DTO object or business entity. 

    In very small project I write data layer by another way. There is something like Application layer which is gateway to your system and there are methods which define system behavior. In this layer I create extension classes for data layer classes. It means data layer classes are empty in data layer. These extension classes are in application layer and can create DTO objects which go out of system. 

    This approach takes my hands free when project began to be more complex with business logic. Then I write classes in data layer by implementation of contract defined in business layer. It means I save my data layer for business complexity and I extend data layer in higher layer (application layer) for purpose application layer only. 

    Tuesday, December 11, 2018 11:27 AM

All replies

  • The data access layer (DAL) is for the access handling not necessarily for the data transfer objects (DTO) . Thus:

    1) Classes in the DAL can should be normal visible for unit and integration testing.

    2) While static methods sounds here sometimes reasonable, a factory method, service locator or dependency injection should be used be the consumer. Again to simplify unit testing.

    3) DTO can be simple (plain) objects or contain complex business logic. This depends on your architecture. But again: their constructors should be public / internal visible. Cause the object-oriented approach should be to create a DTO yourself and call the DAL with it, to persist it.

    Tuesday, December 11, 2018 10:52 AM
  • The data access layer (DAL) is for the access handling not necessarily for the data transfer objects (DTO) . Thus:

    1) Classes in the DAL can should be normal visible for unit and integration testing.

    2) While static methods sounds here sometimes reasonable, a factory method, service locator or dependency injection should be used be the consumer. Again to simplify unit testing.

    3) DTO can be simple (plain) objects or contain complex business logic. This depends on your architecture. But again: their constructors should be public / internal visible. Cause the object-oriented approach should be to create a DTO yourself and call the DAL with it, to persist it.


    I'm a noobie on N-tier projects.
    In other words, since I already have de DAL, I should create another class/project to be the DTO, where I should map a sort of copy o DB, right?

    BP-LP 2005/2016 @ll rights reserved

    Tuesday, December 11, 2018 11:13 AM
  • If I understand you want to have only one instance to access into DB layer. But there (public static AcessoAutor AcessoAutor { get; set; })

    is only property declaration. You need create instance somewhere. It was every time null without creating instance.

    For creating data layer, you can use two models. First is repository. It could be as large hammer in small or low complexity project. Second is DAO pattern. It is very useful for small projects. There are methods which takes CRUD operation (i.e. GetByPrimaryKey(), GetAll(), GetByCustomers(), Insert(), Update(), Delete()). These methods works with some objects. It is depend on what you need. It could be DTO object or business entity. 

    In very small project I write data layer by another way. There is something like Application layer which is gateway to your system and there are methods which define system behavior. In this layer I create extension classes for data layer classes. It means data layer classes are empty in data layer. These extension classes are in application layer and can create DTO objects which go out of system. 

    This approach takes my hands free when project began to be more complex with business logic. Then I write classes in data layer by implementation of contract defined in business layer. It means I save my data layer for business complexity and I extend data layer in higher layer (application layer) for purpose application layer only. 

    Tuesday, December 11, 2018 11:27 AM
  • hmm, start by reading Martin Fowler Catalog of Patterns of Enterprise Application Architecture: Repository and Data Transfer Object.

    btw, important thing: You don't have a DTO and DAL class per table. You have it per entity.

    Tuesday, December 11, 2018 11:28 AM
  • hmm, start by reading Martin Fowler Catalog of Patterns of Enterprise Application Architecture: Repository and Data Transfer Object.

    btw, important thing: You don't have a DTO and DAL class per table. You have it per entity.


    One table doesn't = one entity?


    BP-LP 2005/2016 @ll rights reserved

    Tuesday, December 11, 2018 11:46 AM
  • If I understand you want to have only one instance to access into DB layer. But there (public static AcessoAutor AcessoAutor { get; set; })

    is only property declaration. You need create instance somewhere. It was every time null without creating instance.

    Yes

    Ohhh.

    I did this 

          public static AcessoAutor AcessoAutor { get; set; } = new AcessoAutor();

    and it worked.

    But I also want to exclude the ability to create a new instance without being static, so I made the constructor internal.
    And it's still working.


    BP-LP 2005/2016 @ll rights reserved


    Tuesday, December 11, 2018 11:56 AM
  • Is it necessary to produce DTO object in DAL? I think that not. Why? What does it mean that DAL produce DTO object? It means there is interface which works as facade. Each DTO object is defined inside DAL. 

    There are two points of view from business layer, or two approaches how to see DAL from BAL. 

    First is DAL implements facade and BAL consumes it. It means when I implement BAL I see into DAL and use what DAL can provide. What do I need to do when DAL doesn't have function which I need? I need to open DAL project and implement it and then use it by BAL. In this approach BAL consume DTO produced by DAL.

    Second approach is oposit. It means when I implement BAL I don't see into DAL but I define contract (by interface) in BAL to say DAL what BAL needs to provide from DAL. When I don't have some function in contract I can continue implement BAL because I can add new function into contract without changing DAL. But it means all objects which I need from DAL are defined in BAL. So DAL doesn't define any object to produce. 

    Second approach decrease coupling between DAL and BAL. When I use first approach I'm limited to create rich domain model because I need to map DTO into business entities and then back to DTO. 

    So if there is easy function to only get record from database by some filter and I know I will not to use this function in business layer I can extend data layer class in application layer and implement this function in there because I needn't business layer to give record from database. I can easily use data layer extension in application layer to produce DTO which are defined in application layer and which goes out of the system. This is very similar to second approach but for application layer. 



    • Edited by Petr B Tuesday, December 11, 2018 12:17 PM
    Tuesday, December 11, 2018 12:02 PM
  • One DAO per table basis.

    https://en.wikipedia.org/wiki/Data_access_object

    https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    The DAL using DAO and DTO pattern. DTO(s) are kept in classlib project and all tiers have reference to Entities.

    And you are most likely using Layered and not n-tier.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    using System;
    using System.Collections.Generic;
    using Entities;
    
    namespace DAL.DAO
    {
        public interface IDAOStudent
        {
            DTOStudent GetStudentById(Int32 id);
            List<DTOStudent> GetStudents();
            void CreateStudent(DTOStudent dto);
            void UpdateStudent(DTOStudent dto);
            void DeleteStudent(Int32 id);
        }
    }
    
    ======================================
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Entity;
    using System.Data.Entity.Core.EntityClient;
    using System.Data.Entity.Core.Objects;
    using System.Data.Entity.Infrastructure;
    using System.Data.SqlClient;
    using Entities;
    using DAL.Model;
    
    namespace DAL.DAO
    {
        public class DAOStudent : IDAOStudent
        {
            public DTOStudent GetStudentById(Int32 id)
            {
                var dto = new DTOStudent();
                using (var context = new CUDataEntities())
                {
                    var student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
    
                    if (student != null)
                    {
                        dto.StudentID = student.StudentID;
                        dto.FirstName = student.FirstName;
                        dto.LastName = student.LastName;
                        dto.EnrollmentDate = student.EnrollmentDate;
    
                        var enrolllments =  new DAOEnrollment().GetEntrollmentsByStudentId(id).ToList();
                        var courses = new DAOCourse().GetCoursesByStudentCourseId(student.StudentID).ToList();
    
                        dto.EnrollsandCourses = (from a in enrolllments
                                      join b in courses on a.CourseID equals b.CourseID
                        select new  DTOEnrollandCourse()
                         { Title = b.Title, Credits = b.Credits, Grade = a.Grade }).ToList();
                    }
                }
    
                return dto;
            }
            public void CreateStudent(DTOStudent dto)
            {
                using (var context = new CUDataEntities())
                {
                    var student = new Student
                    {
                        FirstName = dto.FirstName,
                        LastName = dto.LastName,
                        EnrollmentDate = dto.EnrollmentDate
                    };
    
                    context.Students.Add(student);
                    context.SaveChanges();
                }
            }
    
            public void DeleteStudent(int id)
            {
                Student student;
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
                }
    
                using (var newContext = new CUDataEntities())
                {
                    newContext.Entry(student).State = System.Data.Entity.EntityState.Deleted;
                    newContext.SaveChanges();
                }
            }
    
            public List<DTOStudent> GetStudents()
            {
               
                var dtos = new List<DTOStudent>();
    
                using (var context = new CUDataEntities())
                {
                    //var adapter = (IObjectContextAdapter)context;
                    //var objectContext = adapter.ObjectContext;
                    
                    //var entityConn = objectContext.Connection as EntityConnection;
                    //var dbConn = entityConn.StoreConnection as SqlConnection;
    
                    //dbConn.Open();
    
                    var students = context.Students.ToList();
    
                    foreach(var stud in students)
                    {
                        var dto = new DTOStudent
                        {
                            StudentID = stud.StudentID,
                            FirstName = stud.FirstName,
                            LastName = stud.LastName,
                            EnrollmentDate = stud.EnrollmentDate
                        };
    
                        dtos.Add(dto);
                    }
                }
    
                return dtos;
            }
    
            public void UpdateStudent(DTOStudent dto)
            {
                var student = new Student();
    
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == dto.StudentID)).SingleOrDefault();
                }
    
                if (student != null)
                {
                    student.FirstName = dto.FirstName;
                    student.LastName = dto.LastName;
                    student.EnrollmentDate = dto.EnrollmentDate;
                } 
    
                using (var dbcontext = new CUDataEntities())
                {
                    if (student != null)
                    {
                        dbcontext.Entry(student).State = EntityState.Modified;
                        dbcontext.SaveChanges();
                    }
                }
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    
    namespace Entities
    {
       [DataContract] 
        public class DTOStudent
        {
            
            private DTOResponse dtor = new DTOResponse();
    
            [DataMember]
            public Int32 StudentID { get; set; }
    
            [DataMember]
            public string LastName { get; set; }
    
            [DataMember]
            public string FirstName { get; set; }
    
            [DataMember]
            public DateTime? EnrollmentDate { get; set; }
    
            [DataMember]
            public virtual ICollection<DTOEnrollandCourse> EnrollsandCourses { get; set; }
    
            [DataMember]
            public DTOResponse DtoResponse
            {
                get { return dtor; }
                set { dtor = value; }
            } 
        }
    }


    • Edited by DA924x Tuesday, December 11, 2018 12:09 PM
    Tuesday, December 11, 2018 12:06 PM
  • Yes, I'm suing a layered 

    N-Tier / 3-Tier

    Segregates functionality into separate segments in much the same way as the layered style, but with each segment being a tier located on a physically separate computer.

    I really need to store on physically separated computers?


    BP-LP 2005/2016 @ll rights reserved

    Tuesday, December 11, 2018 12:16 PM
  • Yes, I'm suing a layered 

    N-Tier / 3-Tier

    Segregates functionality into separate segments in much the same way as the layered style, but with each segment being a tier located on a physically separate computer.

    I really need to store on physically separated computers?


    BP-LP 2005/2016 @ll rights reserved

    No, it's only one computer you are dealing with application layered. N-tier is normally for distributed processing over physical boundaries. PL on one machine, BLL on another and DAL on another machine.

    https://docs.microsoft.com/en-us/visualstudio/data-tools/n-tier-data-applications-overview?view=vs-2017

    Also think about this, like MVP.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    http://polymorphicpodcast.com/shows/mv-patterns/

     
    • Edited by DA924x Tuesday, December 11, 2018 1:04 PM
    Tuesday, December 11, 2018 1:03 PM
  • I do want to make mention on the repository pattern, which is domain logic and not data persistence logic in Domain Driven Design.

    https://martinfowler.com/eaaCatalog/repository.html

    https://programmingwithmosh.com/entity-framework/common-mistakes-with-the-repository-pattern/

    https://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx

    <copied>

    A DAO is much closer to the underlying storage , it's really data centric. That's why in many cases you'll have DAOs matching db tables or views 1 on 1. A DAO allows for a simpler way to get data from a storage, hiding the ugly queries. But the important part is that they return data as in object state.

    A repository sits at a higher level. It deals with data too and hides queries and all that but, a repository deals with** business/domain objects**. That's the difference. A repository will use a DAO to get the data from the storage and uses that data to restore a business object. Or it will take a business object and extract the data that will be persisted. If you have an anemic domain, the repository will be just a DAO. Also when dealing with querying stuff, most of the time a specialized query repository is just a DAO sending DTOs to a higher layer.

    <end>

    https://www.infoworld.com/article/3117713/application-development/design-patterns-that-i-often-avoid-repository-pattern.html

    The repository pattern I never use, particular the generic repository becuase it's to generic. 

    I don't use the repository object for data persistence, becuase it no more than a glorified DAO when the repository object is anemic  in not acting with domain behavior.  

    I have seen the repository being used properly in large  scale Web based solutions from a DDD aspect, which doesn't do data persistence and calls upon the data mapping layer.


    • Edited by DA924x Thursday, December 13, 2018 8:16 AM
    Thursday, December 13, 2018 7:54 AM
  • 3) DTO can be simple (plain) objects or contain complex business logic. This depends on your architecture. But again: their constructors should be public / internal visible. Cause the object-oriented approach should be to create a DTO yourself and call the DAL with it, to persist it.

    A DTO's job is below,  and other object act upon the DTO as it passes through the layers.

    https://en.wikipedia.org/wiki/Data_transfer_object

    <copied>

    The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behavior except for storage, retrieval, serialization and deserialization of its own data (mutators, accessors, parsers and serializers). In other words, DTOs are simple objects that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.

    <end>

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5


    • Edited by DA924x Thursday, December 13, 2018 8:07 AM
    Thursday, December 13, 2018 8:04 AM
  • 3) DTO can be simple (plain) objects or contain complex business logic. This depends on your architecture. But again: their constructors should be public / internal visible. Cause the object-oriented approach should be to create a DTO yourself and call the DAL with it, to persist it.

    A DTO's job is below,  and other object act upon the DTO as it passes through the layers.

    https://en.wikipedia.org/wiki/Data_transfer_object

    <copied>

    The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behavior except for storage, retrieval, serialization and deserialization of its own data (mutators, accessors, parsers and serializers). In other words, DTOs are simple objects that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.

    <end>

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5


    Good, my DTO only retrieves/stores data.


    Bro, Idk why, but IF isn't create models nor de .edmx with MySQL databases (at least in my VS).
    I found something called Linq Connect it works perfectly on Mapping the DB, but the free version only supports 10 tables per project

    I know there's Dapper, Autofac and stuff.

    I'm not good with code first approach and right now I can't spend time learning (I know is not that hard, but I really don't have time) so I need something that does this part form me .

    Can you recommend me a ORM that will map all or at least 30 database tables?
    And it need to be free (The company here is small and can't afford nothing extra at the moment)


    BP-LP 2005/2016 @ll rights reserved

    Thursday, December 13, 2018 8:48 AM
  • Can you recommend me a ORM that will map all or at least 30 database tables?
    And it need to be free (The company here is small and can't afford nothing extra at the moment)

    There is the EF DB first approach for an exiting database, which is the example code I showed you using a DAL using the  DAO and DTO patterns being used that is using EF DB first. Ef will not work for any table that doesn't have a primary-key.

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/performance/perf-whitepaper

    Thursday, December 13, 2018 9:37 AM

  • There is the EF DB first approach for an exiting database, which is the example code I showed you using a DAL using the  DAO and DTO patterns being used that is using EF DB first. Ef will not work for any table that doesn't have a primary-key.

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/performance/perf-whitepaper

    The problem is that, my VS in unable to use EF with MySQL.

    BP-LP 2005/2016 @ll rights reserved

    Thursday, December 13, 2018 9:41 AM

  • There is the EF DB first approach for an exiting database, which is the example code I showed you using a DAL using the  DAO and DTO patterns being used that is using EF DB first. Ef will not work for any table that doesn't have a primary-key.

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/performance/perf-whitepaper

    The problem is that, my VS in unable to use EF with MySQL.

    BP-LP 2005/2016 @ll rights reserved

    And why is this that EF doesn't work with MySQL?  What are the symptoms?
    Thursday, December 13, 2018 11:32 AM


  • And why is this that EF doesn't work with MySQL?  What are the symptoms?

    If I try to create a new Model using DB First it doesn't show the database tables part, in that part the window closes doesn't allowing me to choose the tables.
    If I try to create a DbContext Generator (EF5 and 6) it gives me an erro saying that I have to change the name of a $.edm$ or something manually.
    If I try to create a new DataSet, I can't query the data.

    All that on the last versions of the MySQL Connector (v.8.0.13)

    So, I've downloaded the 6.9.5 that seems to work, it shows me the window to choose the tables, but give  "System.Data.StrongTypingException: The value for column 'IsPrimaryKey'" error on both Code First and DB First if I try to use the ADO.NET Entity Data Model

    I've went to StackOverFlow ,but no result, those solutions didn't work for me.


    Only way I manage to use was with v.6.9.5 using Code First following this, but what I really want is something to map that for me. 

    And I just installed those two extensions, but I can't figure out how to use or if they really work
    Supposely


    BP-LP 2005/2016 @ll rights reserved

    Thursday, December 13, 2018 1:41 PM
  • Oh, I remember you now. 

    Did you try positing to MSDN EF forum?

    If I were you, I would use ADO.NET, MySQL Command objects, parmterized T-SQL, a datareader and CRUD using the DTO pattern.

    It either that or using Dapper.

    What version of Visual Stuido and the .NET Framework or you using?

    Thursday, December 13, 2018 4:50 PM
  • Oh, I remember you now. 

    Did you try positing to MSDN EF forum?

    If I were you, I would use ADO.NET, MySQL Command objects, parmterized T-SQL, a datareader and CRUD using the DTO pattern.

    It either that or using Dapper.

    What version of Visual Stuido and the .NET Framework or you using?


    I didn't know MSDN had a EF forum.

    I'm using ADO.NET and command objects, but I don't know how to return a list of an object e.g Customer, Products with data reader, instead of that I return. And I also don't see any help on Google that fits my problem.

    And I'm also trying to use Dapper, I went to dapper-tutorial.net found a MySQL sample and it works, also that Dapper Crud extension seems to work too, but it just need a few adjustments.
    So... Finally I'm using dapper right now, but I will have to create the classes myself and use the Dapper extensions to map, now I just need to see how to create the relationship via c# code.


    VS17 Enterprise v15.8.6
    .Net Framework 4.5.2.

    BP-LP 2005/2016 @ll rights reserved

    Friday, December 14, 2018 8:19 AM
  • I'm using ADO.NET and command objects, but I don't know how to return a list of an object e.g Customer, Products with data reader, instead of that I return. And I also don't see any help on Google that fits my problem.

    You are being shown in the link on how to do it, which would be in a reader loop .

    https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlDataReader.htm

    All ADO.NET DataReaders work the same no matter if MS SQL Server or MySQL is being used where you can specify  the column name instead oh the ordinal position.

    https://stackoverflow.com/questions/28325813/sqldatareader-get-value-by-column-name-not-ordinal-number/28325878

    var dtos = new List<dto>().

    while loop on reader

      var dto = new dto().

       dto.name = (string)rdr['name']

        dtos.Add(dto)

    end while loop

    return dtos

    You don't see the reader loop in this EF example, but the loop is being done behind the scenes. EF and Linq just hides what you being shown manually how to do it with objects in a collection being returned. 

    That is  all EF is really doing is using an object of off the virtual model,  and EF works with a single object or a collection of the object in a tubular form, the collection. 

       public List<DtoProject> GetProjectsByUserId(string userid)
            {
                var dtos = new List<DtoProject>();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    
                    dtos = (from a in context.Projects.Where(a => a.UserId.Contains(userid))
                        select new DtoProject
                        {
                            ProjectId = a.ProjectId,
                            ClientName = a.ClientName,
                            ProjectName = a.ProjectName,
                            Technology = a.Technology,
                            ProjectType = a.ProjectType,
                            UserId = a.UserId,
                            StartDate = a.StartDate,
                            EndDate = a.EndDate,
                            Cost = a.Cost
                        }).ToList();
                }
    
                return dtos;
            }



    • Edited by DA924x Friday, December 14, 2018 9:57 AM
    Friday, December 14, 2018 9:55 AM
  • I'm using ADO.NET and command objects, but I don't know how to return a list of an object e.g Customer, Products with data reader, instead of that I return. And I also don't see any help on Google that fits my problem.

    You are being shown in the link on how to do it, which would be in a reader loop .

    https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlDataReader.htm

    All ADO.NET DataReaders work the same no matter if MS SQL Server or MySQL is being used where you can specify  the column name instead oh the ordinal position.

    https://stackoverflow.com/questions/28325813/sqldatareader-get-value-by-column-name-not-ordinal-number/28325878

    var dtos = new List<dto>().

    while loop on reader

      var dto = new dto().

       dto.name = (string)rdr['name']

        dtos.Add(dto)

    end while loop

    return dtos

    You don't see the reader loop in this EF example, but the loop is being done behind the scenes. EF and Linq just hides what you being shown manually how to do it with objects in a collection being returned. 

    That is  all EF is really doing is using an object of off the virtual model,  and EF works with a single object or a collection of the object in a tubular form, the collection. 




    Thanks, bro. 
    Thank you very much( Idk if people still use this rsrsrsrs).

    BP-LP 2005/2016 @ll rights reserved

    Friday, December 14, 2018 10:47 AM
  • Thanks, bro. 
    Thank you very much( Idk if people still use this rsrsrsrs).

    People still use ADO.NET and DB Command Objects using  inline T-SQL or stored procedure with a datareader, becuase using a datareader is the fastest way possible to read data in a forward cursor. All ORM(s) like EF use a datareader.

    They use a datatable too or a collection of objects.

    https://www.codeproject.com/Articles/36847/Three-Layer-Architecture-in-C-NET

    Friday, December 14, 2018 1:18 PM
  • Thanks, bro. 
    Thank you very much( Idk if people still use this rsrsrsrs).

    People still use ADO.NET and DB Command Objects using  inline T-SQL or stored procedure with a datareader, becuase using a datareader is the fastest way possible to read data in a forward cursor. All ORM(s) like EF use a datareader.

    They use a datatable too or a collection of objects.

    https://www.codeproject.com/Articles/36847/Three-Layer-Architecture-in-C-NET


    I was talking about the "Thank you very much" srsrsrs

    About the ADO.NET, yeah, I know, ADO is very fast.

    BP-LP 2005/2016 @ll rights reserved

    Friday, December 14, 2018 1:26 PM