i have a problem, i would use same dbContext in multiple methods, example:
public class Communication
{
public Response AddCommunication(Commnucation myComm)
{
using(MYDB dbContenxt = new MYDB())
{
DBCOMMUNICATION dbComm = new DBCOMMUNICATION
{
ID = myComm.Id,
NAME = myComm.Name,
SUBJECT = myComm.Subject,
MESSAGE = myComm.Message
};
if(myComm.Images != null && myComm.Images.Count > 0)
{
foreach(var image in Images)
{
IMAGES dbImages = new IMAGES
{
ID_COMMUNICATION = myComm.Id,
NAME = image.Name,
VALUE = image.Value
};
dbContext.IMAGES.add(dbImages);
}
}
if(myComm.Attachments != null && myComm.Attachments.Count > 0)
{
foreach(var image in Images)
{
same work .......
}
}
dbContext.SaveChanges();
}
.......
.......
}
}
i would extract methods that use foreach loop, example:
if(myComm.Images != null && myComm.Images.Count > 0)
{
AddImages(myComm);
}
and in the method AddImages i would use the same dbContext for add but use SaveChanges only in AddCommunication. Which is the best practice to do this?? Use a Singleton Pattern for dbContext? Please help me. Thanks and sorry for my bad english.
Aucun commentaire:
Enregistrer un commentaire