我在我的類中有一個Update方法,它包含一個using語句來創建一個UnitOfWork的新實例,這是因爲我發送電子郵件是異步的,因爲EF和DbContext不是線程安全的必須創建每個請求都有一個新的dbContext。 我在我的UnitOfWork中使用了Autofac for DI,但我不知道如何在使用語句中使用我的新UnitOfWork構造函數,以確保它創建一個新實例。 請注意,這是一個控制檯應用程序。 感謝Autofac DI使用語句
UnitOfWork.cs
public class UnitOfWork : IUnitOfWork
{
private IEventLoggerService MailCheckerLog;
private readonly BBEntities ctx = new BBEntities();
private IEmailRepository emailRepository;
public UnitOfWork(IEventLoggerService MaLog, IEmailRepository emailRepo)
{
emailRepository = emailRepo;
MailCheckerLog = MaLog;
}
public IEmailRepository EmailRepository
{
get
{
if (this.emailRepository == null)
{
this.emailRepository = new EmailRepository(ctx);
}
return emailRepository;
}
}
public void Commit()
{
try
{
this.ctx.SaveChanges();
}
catch (Exception e)
{
MailCheckerLog.log("Unit Of Work Exception => Commit() => " + e.Message.ToString(), EventLogEntryType.Error);
}
}
}
EmailService.cs
public class EmailService : IEmailService
{
private IUnitOfWork unitOfWork;
private IEventLoggerService MailCheckerLog;
private ISMTPService SMTPService;
public EmailService(IEventLoggerService Log, IUnitOfWork uOfWork, ISMTPService SMTPS)
{
unitOfWork = uOfWork;
MailCheckerLog = Log;
SMTPService = SMTPS;
SMTPService.OnMailSendComplete += new EventHandler(SendCompletedC);
}
public void Update(tb_Email obj)
{
IUnitOfWork unitOfWork2;
using (unitOfWork2 = new UnitOfWork())
{
unitOfWork2.EmailRepository.Update(obj);
unitOfWork2.Commit();
}
}
}
更新時間:
我得到了它與下面的解決方案但不知道這是否是最好的工作way
個UnitOfWorkFactory.cs
public class UnitOfWorkFacotry : IUnitOfWorkFactory
{
private IUnitOfWork uow;
private IEmailRepository emailrepo;
private IEventLoggerService eventlog;
public UnitOfWorkFacotry(IEventLoggerService MaLog, IEmailRepository emailRep)
{
emailrepo = emailRep;
eventlog = MaLog;
}
public IUnitOfWork GetCurrent()
{
return uow ?? create();
}
public IUnitOfWork create()
{
return new UnitOfWork(eventlog, emailrepo);
}
}
EmailService.cs - > Update方法
public void Update(tb_Email obj)
{
IUnitOfWork unitOfWork2;
using (unitOfWork2 = unitOfWorkFactory.create())
{
unitOfWork2.EmailRepository.Update(obj);
unitOfWork2.Commit();
}
}
「ISMTPService」只是框架'SmtpClient'的包裝?如果是這樣,您應該知道即使使用異步方法也不能同時發送多個郵件,但代碼必須等待每個電話/郵件完成才能開始新的郵件。總之,如果你想使用這個庫,你最好使用同步方法。 – Igor
是的,以及發送電子郵件和事件的方法。 – user65248
但這正是我需要的,並且工作正常。代碼發送電子郵件到服務器,然後它會引發事件,如果發送成功或不成功,並基於我使用更新方法來更新數據庫。 – user65248