gpt4 book ai didi

c# - 我在这里使用正确的工作单元吗? ( Entity Framework 4 POCO)

转载 作者:太空狗 更新时间:2023-10-29 17:58:41 26 4
gpt4 key购买 nike

我找到了一些关于如何使用 ef4 创建工作单元的示例,我没有使用过 di/ioc,我想保持简单,这是一个示例(90% 的灵感),我认为这没问题,但因为我我正在寻找一种从现在开始使用的模式,我想最后一次征求意见。

 public interface IUnitOfWork
{
void Save();
}

public partial class TemplateEntities : ObjectContext, IUnitOfWork
{
....
public void Save()
{
SaveChanges();
}
}
public interface IUserRepository
{
User GetUser(string username);
string GetUserNameByEmail(string email);
void AddUser(User userToAdd);
void UpdateUser(User userToUpdate);
void DeleteUser(User userToDelete);
//some other
}
public class UserRepository : IUserRepository, IDisposable
{
public TemplateEntities ctx;
public UserRepository(IUnitOfWork unit)
{
ctx = unit as TemplateEntities;
}
public User GetUser(string username)
{
return (from u in ctx.Users
where u.UserName == username
select u).SingleOrDefault();
}
public string GetUserNameByEmail(string email)
{
return (from u in ctx.Users
where u.Email == email
select u.UserName).SingleOrDefault();
}
public void AddUser(User userToAdd)
{
ctx.Users.AddObject(userToAdd);
}
public void UpdateUser(User userToUpdate)
{
ctx.Users.Attach(userToUpdate);
ctx.ObjectStateManager.ChangeObjectState(userToUpdate, System.Data.EntityState.Modified);
}
public void DeleteUser(User userToDelete)
{
ctx.Users.Attach(userToDelete);
ctx.ObjectStateManager.ChangeObjectState(userToDelete, System.Data.EntityState.Deleted);
}
public void Dispose()
{
if (ctx != null)
ctx.Dispose();
}
}

最后

    public class BogusMembership : MembershipProvider
{
public MembershipCreateStatus CreateUser(string username, string password, string email, bool autoemail, string fullname)
{
IUnitOfWork ctx = new TemplateEntities();
using (UserRepository rep = new UserRepository(ctx))
{
using (TransactionScope tran = new TransactionScope())
{
if (rep.GetUser(username) != null)
return MembershipCreateStatus.DuplicateUserName;
if (requiresUniqueEmail && !String.IsNullOrEmpty(rep.GetUserNameByEmail(email)))
return MembershipCreateStatus.DuplicateEmail;
User userToCreate = new User
{
UserName = username,
PassWord = EncodePassword(password),
FullName = fullname,
Email = email,
AutoEmail = autoemail
};
try
{
rep.AddUser(userToCreate);
ctx.Save();
tran.Complete();
return MembershipCreateStatus.Success;
}
catch
{
return MembershipCreateStatus.UserRejected;
}
}
}
}
}

在摆脱 IUnitOfWork 和 IDisposal 之后,CreateUser 看起来像这样:

        public MembershipCreateStatus CreateUser(string username, string password, string email, bool autoemail, string fullname)
{
using (TransactionScope tran = new TransactionScope())
{
using (TemplateEntities ctx = new TemplateEntities())
{
UserRepository rep = new UserRepository(ctx);
//OtherRepository rep2 = new OtherRepository(ctx);
if (rep.GetUser(username) != null)
return MembershipCreateStatus.DuplicateUserName;
if (requiresUniqueEmail && !String.IsNullOrEmpty(rep.GetUserNameByEmail(email)))
return MembershipCreateStatus.DuplicateEmail;
User userToCreate = new User
{
UserName = username,
PassWord = EncodePassword(password),
FullName = fullname,
Email = email,
AutoEmail = autoemail
};
try
{
rep.AddUser(userToCreate);
ctx.SaveChanges();
tran.Complete();
return MembershipCreateStatus.Success;
}
catch
{
return MembershipCreateStatus.UserRejected;
}
}
}
}

最佳答案

这看起来基本没问题。不过有一些建议:

  • 您不应让存储库处理 TemplateEntities。这样做的原因是,当您在一个事务中需要两个存储库时,就会遇到问题。您应该将处置 TemplateEntities 的职责移至与 TransactionScope 相同的级别;
  • TransactionScope 应移至更高级别。 TemplateEntities 最好在 TransactionScope 中实例化;
  • 如果 Save 包装器不包含功能,则您不必创建它。如果您在 IUnitOfWork 接口(interface)上指定 void SaveChanges(),这将选取 TemplateEntitiesSaveChanges ;
  • 就我个人而言,我不会使用 string GetUserNameByEmail(...),而是使用 User GetUserByEmail(...),因为这样也可以达到您的目的,而且您有当您以后需要 User GetUserByEmail(...);
  • 时,没有两种按电子邮件地址搜索的方法的优点
  • 您可能需要考虑将 ctx 设为私有(private),或者至少将其设为私有(private) setter,例如 public TemplateEntities Ctx { get;私有(private)集; ;
  • 您可以使用如下例所示的方法创建一个抽象存储库。从长远来看,这将为您省去很多沉闷的打字:

-

public interface IRepository<TEntity>
{
void Delete(TEntity entity);

/* ... */
}

public abstract class AbstractRepository<TEntity> : IRepository<TEntity>
{
public TemplateEntities ctx;

public AbstractRepository(IUnitOfWork unit)
{
ctx = unit as TemplateEntities;
}

protected abstract ObjectSet<TEntity> Entites { get; }

public virtual void Delete(TEntity entity)
{
Entities.Attach(entity);
ctx.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Deleted);
}

/* ... */
}

public interface IUserRepository : IRepository<User>
{
User GetUser(string username);

/* ... */
}

public class UserRepository : AbstractRepository<User>, IUserRepository
{
public UserRepository(IUnitOfWork unit)
: base(unit)
{
}

protected override ObjectSet<User> Entites
{
get { return ctx.Users; }
}

public User GetUser(string username)
{
return (from u in ctx.Users
where u.UserName == username
select u).SingleOrDefault();
}

/* ... */
}

关于c# - 我在这里使用正确的工作单元吗? ( Entity Framework 4 POCO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4010294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com