Frage zur bereichsbezogenen Lebensdauer von DbContext in Entity Framework Core

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Frage zur bereichsbezogenen Lebensdauer von DbContext in Entity Framework Core

by Guest » 18 Jan 2025, 19:37

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?

Top