Frage zur bereichsbezogenen Lebensdauer von DbContext in Entity Framework CoreC#

Ein Treffpunkt für C#-Programmierer
Guest
 Frage zur bereichsbezogenen Lebensdauer von DbContext in Entity Framework Core

Post by Guest »

Code: Select all

public class CommandObject
{
private readonly MyContext myContext;

public CommandObject(MyContext myContext)
{
this.myContext = myContext;
}

public void CreateFoo()
{
myContext.Foos.Add(new Foo());

myContext.SaveChanges();
}
}

public class SeparateFooService
{
private readonly CommandObject commandObject;

public SeparateFooService(CommandObject commandObject)
{
this.commandObject = commandObject;
}

public void CreateTwoFoos()
{
commandObject.CreateFoo();
commandObject.CreateFoo();
}
}

public class TransactionalFooService
{
private readonly MyContext myContext;
private readonly CommandObject commandObject;

public TransactionalFooService(MyContext myContext, CommandObject commandObject)
{
this.myContext = myContext;
this.commandObject = commandObject;
}

public void CreateTwoFoos()
{
using (var transaction = myContext.Database.BeginTransaction())
{
commandObject.CreateFoo();
commandObject.CreateFoo();

transaction.Commit();
}
}
}
Meine Frage betrifft myContext und commandObject in TransactionalFooService. Unter der Annahme, dass myContext für einen Gültigkeitsbereich konfiguriert ist, sind dann sowohl myContext, der eine Abhängigkeit von TransactionalFooService darstellt, als auch myContext, der eine Abhängigkeit von commandObject (dem commandObject in TransactionalFooService) darstellt, identisch?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post