Passing Entity Context to Other Methods and Objects

I was wondering what is the best way to pass context between classes. Should I use the ref parameter or just pass the context as a parameter? Preferred constructor, but in case of static method the best approach? That is, performance, security, design, etc. Is there success in passing context as a parameter? Are conflicts possible if different threads work on the context at the same time using links?

Main.cs

static void Main(string[] args)
{
    var context = new MyEntities();

    var myClass = new MyClass(context);
    myClass.AddPerson();
    // or
    Person.AddPerson(ref context);
}

Myclass.cs

public class MyClass
{
    public void MyClass(MyEntities context) { }

    public void AddPerson()
    {
        context.People.AddObject(new Person());
    }
}

MySecondClass.cs

public partial class Person
{
    public static AddPerson(ref MyEntities context)
    {
        // Do something
    }
}
+5
source share
2 answers

ref . ( , ). ref, " " ( ?). , , - .

+3

ref , , . AKA:

static void Main(string[] args)
{
    var context = new MyEntities();
    Person.AddPerson(ref context);

    // context is now null
}

:

public partial class Person
{
    public static AddPerson(ref MyEntities context)
    {
        context = null;
    }
}

. , , , ++.

+6

All Articles