我在我的类中有一个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