Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Entities - 3-tier Architecture

In the past few months I've learned alot about Linq-To-Entities and the 3-tier architecture with a DAO/DAL/Repository. Now I've a few things in my mind that keeps bugging me. I've those three questions that you'll see below.

There are alot of ways to make the repository work but what way is "the" way of making the repository work in ways of performance.

1) Initialize a datacontext in the constructor

public class Repository : IRepository
{

    private Datacontext context;

    public Repository()
    {
        context = new Datacontext();
    }

    public IList<Entity> GetEntities()
    {
        return (from e in context.Entity
                select e).ToList();
    }
}

2) Use "Using"

public class Repository : IRepository
{
    public IList<Entity> GetEntities()
    {
        using (Datacontext context = new Datacontext())
        {
            return (from e in context.Entity
                    select e).ToList();
        }

    }
}

3) In another way (please comment)

I'll put your suggestion here for others to comment


Also it seems some people say the repository should return an IQueryable to the businesslayer while others say it's better to return an IList. What is your oppinion on this?


The above code samples in the first question are pointing to the Repository, but what is the bestway to implement the repository in the businesslayer (Initialize in the constructor, use "Using"??)

like image 681
Julian Avatar asked Oct 24 '22 06:10

Julian


1 Answers

Either works I think. The main thing is that you should be making your object contexts fairly short lived (IMHO). Therefore I think you have two choices: -

  1. Create / destroy the context within a single method call e.g. the using statement as per your second example.

  2. Create / destroy the context when you create / destroy the repository - in which case your repository should both implement IDisposable and itself be wrapped in a using statement, and should be short lived. The benefit of this approach is that your repository methods simply do the query, there is no using (new ObjectContext()) polluting the method; the flip-side is that the reposibility passes onto the client to dispose of the Repository. Using this mechanism means that you can also compose queries within IQueryable<> (provided that you execute the query before disposing of the repository). For example:

public class Repository : IDisposable { DataHubContext context = new DataHubContext();

public IQueryable<Payment> GetPayments()
{
    return context.Payments;
}

public void Dispose()
{
    context.Dispose();
}

}

Formatting has gone a bit funny in SO - sorry....And then in your calling code: -

public class ClientCode
{
    public void DisplayPaymentsOnScreen()
    {
        Payment[] payments;

        using (var repository = new Repository())
        {
            payments = repository.GetPayments().Where(p => p.Amount > 100).ToArray();
        }

        // Do stuff with the data here...
    }
}
like image 151
Isaac Abraham Avatar answered Nov 02 '22 11:11

Isaac Abraham