Grasshopper
Grasshopper

Reputation: 4977

One DataContext For Linq To SQL While Using Repository Pattern

I am fairly new to .NET C# development and recently started using LINQ to SQL for the majority of my data access layer. Unfortunately, I have been struggling with how to manage my DataContext so that I don't get the dreaded exceptions that result from entities either not being attached to a context or attempting to attach an entity to one context while it is attached to another. After much reading, I believe that the best solution for my application is to leave the DataContext open for the entire duration of my application.

In short, I am using the Repository pattern to implement the basic CRUD operations (i.e. Create, Read, Update, and Destroy) for all entities managed by my context. My implementation of the Repository is included below. In addition to the Repository, I have DAOs (Data Access Objects) for each entity that has more specific data access methods (i.e. CustomerDAO.getCustomerByName(string name), etc...). Each of my Windows forms has its' own instance of one or more DAOs (that extend Repository) and the DataContext in my repository is static. The problem that I am running into is that even though my DataContext in the repository class below is declared as static, I'm finding that each distinct DAO actually gets a different instance of the DataContext. For example, if I have 8 references to the CustomerDAO, they all have the same DataContext. But, if I create one WorkOrderDAO I get another instance of the DataContext and all future instances of WorkOrderDAO get this same DataContext. More specifically, I discovered this in the following scenario.

1) Use an instance of WorkOrderDAO to load all WorkOrders into ListView - Has one DataContext 2) Use an instance of WorkOrderJobsDAO to attempt to delete one of the jobs on the WorkOrder. This is a collection on the WorkOrder. Has a different DataContext so I can't attach

Is this a problem with how I have implemented the Repository below? The only thing that I can think of to solve this issue is to create a Singleton that the Repository uses to get its' DataContext. Can anyone make any recommendations here for how I should manage the Context?

public class Repository<T> : IRepository<T>
where T : class
{
    private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    protected static DomainClassesDataContext db = new DomainClassesDataContext();
    private static bool dataContextOptionsInitialized = false;

    public Repository()
    {
        if (!dataContextOptionsInitialized)
        {
            db.DeferredLoadingEnabled = true;
            dataContextOptionsInitialized = true;
        }
    }
    public void AddEntity(T entity)
    {
        GetTable.InsertOnSubmit(entity);
        SaveAll();
    }

    public void DeleteEntity(T entity, bool attach)
    {
        if(attach)
            GetTable.Attach(entity);

        GetTable.DeleteOnSubmit(entity);
        SaveAll();

    }

    public void UpdateEntity(T entity, bool attach)
    {
        if(attach)
            GetTable.Attach(entity, true);
        SaveAll();

    }

    public System.Data.Linq.Table<T> GetTable
    {
        get { return db.GetTable<T>(); }
    }

    public IEnumerable<T> All()
    {
        return GetTable;
    }

    public void SaveAll()
    {
        db.SubmitChanges();

    }
}

Upvotes: 1

Views: 1264

Answers (1)

Reonekot
Reonekot

Reputation: 412

Generic classes in C# gets "expanded" on compilation, so your Repository will be a different class than Repository, which is why the static variables are different instances between your DTOs. So yes you probably want to save it somewhere else, like a in a Singleton class.

Upvotes: 1

Related Questions