0

i am using 3 tier architecture in my winform application so i have static class which handle the operation of equipment

 public static class Equipments 
{


    public static void AddEquipment(string name, decimal dimLength)
    {

            DBClassesDataContext db = new DBClassesDataContext();

            Equipment equipment = new Equipment();
            equipment.Name = name;
            equipment.DimLength = dimLength;

            db.Equipments.InsertOnSubmit(equipment);
            db.SubmitChanges();

    }



  public static void UpdateEquipment(int equipmentID, string name, decimal dimLength)
        {
                         DBClassesDataContext db = new DBClassesDataContext();

                Equipment oldEquipment;
                oldEquipment = db.Equipments.Where("EquipmentID = @0",equipmentID).SingleOrDefault();
                oldEquipment.Name = name;
                oldEquipment.DimLength = dimLength;

                db.SubmitChanges();}

so my questions are :

  1. Do i need to create instance of DBClassesDataContext in each method ?
    because when i done global static DBClassesDataContext it didn't work correctly.
  2. Is there any better way to handle DBClassesDataContext instead to create it each time inside the method (like create new DBClassesDataContext each time i run a method from this class)

Thanks

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75

2 Answers2

4

Do i need to create instance of DBClassesDataContext in each method?

You should do, absolutely - just like you should normally create a new SqlConnection each time you want to access the database in non-LINQ code. In general, avoid global state - it's almost always a bad idea.

There is any better way to handle DBClassesDataContext instead to create it each time inside the method

No - that's exactly the right approach. Why would you try to avoid just creating it each time?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Even though I'll probably get stoned to death for disagreeing with the Jon Skeet, I'll post this anyway.

You definitely don't need to create the instance in every single method, or at least not like this. There's a principle I like to follow called DRY - don't repeat yourself, and repeating the same line over and over, that can be avoided, clearly violates this principle.

You have multiple options here:

1.) define the methods as instance methods, maybe something like this:

internal class MyDbActions
{
    private MyDbContext _myDbContext;
    private MyDbContext Db
    {
        get
        {
            if (_myDbContext == null) _myDbContext = new MyDbContext();
            return _myDbContext;
        }
    }

    internal void Add(SomeClass c)
    {
        Db.Table.AddObject(c);
        Db.SubmitChanges();
        Db.Dispose();
    }
}

Or something like that, you get the idea. This can be modified to whatever you need.

2.) use can use dependency injection for your methods, so consider something like this:

public static class Equipments 
{
    public static void AddEquipment(DBClassesDataContext db, string name, decimal dimLength)
    {
            Equipment equipment = new Equipment();
            equipment.Name = name;
            equipment.DimLength = dimLength;

            db.Equipments.InsertOnSubmit(equipment);
            db.SubmitChanges();

    }
}

You'd manage your datacontext outside this class.

3.) you can utilize the Repository pattern, Unit of work pattern and IoC. I won't post the example code here, because it's quite lengthy, but here's one link to give you an idea:

Repository pattern with Linq to SQL using IoC, Dependency Injection, Unit of Work

Community
  • 1
  • 1
walther
  • 13,466
  • 5
  • 41
  • 67