Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Orientated Linq2Sql query class

For my internship I'm creating a program that communicates with a database on the background. The program is layered in the MVC(Model-View-Controller) way.

For the View I want to access the data through something I called the DataAccesLayer(DAL). Because that view has minimal knowledge I want it to pass an ID for the query I want to call. The calling will be done inside the DAL. Then with the ID I want to ask a class that holds the queries to return the query to then execute it inside the DAL. Picture for visualizing.

enter image description here

The problem I have now is how to execute the query in my Read function. The code for DAL is the following:

public class DataAccesLayer
{
    private Queries queryloader;
    private RoadsoftDigitacV8DataContext db;

    public DataAccesLayer()
    {
        queryloader = new Queries();
        db = new RoadsoftDigitacV8DataContext();
    }

    public List Read(int ID)
    {
        IQueryable query;
        query = queryloader.GetQuery(ID);

        return query.ToList();
        
    }

}

The Code for the Queries class:

public class Queries
{
    private Dictionary<int, IQueryable object> queryDict;
    private ErrorLoggerWinLog logger;

    public Queries()
    {
        logger = ErrorLoggerWinLog.Instance();
        queryDict = new Dictionary<int, IQueryable object>();
        queryDict.Add(1, from d in db.Drivers
                         select d);
    }

    public object GetQuery(int ID)
    {
        var query;
        if(queryDict.TryGetValue(ID, out query) == false)
        {
            logger.WriteLine("Queries", "Could not find the query specified", ErrorLoggerWinLog.loggerlevel.Error);
        }
        return query;
    }
}

I'm wondering, is this possible? Right now it doesn't seem to work. I'm probably forgetting something or missing something important. Does anyone have any experience with this kind of set-up, or should be looking at a totally different solution?

Edit: Right now it doesn't seem to execute the query, like I'm missing a command in the read function. The Datacontext is filled though, that's being done in a different section of the program.

Edit2: Right now I'm looking into the IRepository Pattern, it's a great learning experience, Thank you all who took time to comment and anwser!

like image 749
Freakz0rz Avatar asked Aug 29 '13 09:08

Freakz0rz


People also ask

What a LINQ query returns in LINQ to object?

Language-Integrated Query (LINQ) makes it easy to access database information and execute queries. By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.

What type of object can you query using LINQ?

You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. The collection may be user-defined or may be returned by a . NET API. In a basic sense, LINQ to Objects represents a new approach to collections.

Can we use LINQ to query against a DataTable?

Can we use linq to query against a DataTable? Explanation: We cannot use query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable.

What is the key function of the LINQ to SQL classes?

LINQ to SQL Methods and Database Stored Procedures LINQ to SQL supports stored procedures and user-defined functions. In LINQ to SQL, you map these database-defined abstractions to client objects so that you can access them in a strongly typed manner from client code.


1 Answers

At any time your queryDict has only one element; the only time that GetQuery() doesn't log an error message and doesn't return null is when you pass 1 to it. as a consequence Read(1) returns a list of all Drivers, otherwise throws a NullReferenceException because, well, query is null.

You should use something like this:

    public class DriversDAL
    {
        private RoadsoftDigitacV8DataContext db;

        public DriversDAL()
        {
            db = new RoadsoftDigitacV8DataContext();
        }

        public Driver GetDriver(int ID)
        {
            return db.Drives.Single(d => d.ID == ID);
        }

    }

If you want a generic solution, you should use a generic dao and the Repository pattern:

How To Create Generic Data Access Object (DAO) CRUD Methods with LINQ to SQL

like image 86
Ahmed KRAIEM Avatar answered Oct 26 '22 12:10

Ahmed KRAIEM