我在这里使用正确的工作单位吗? (实体框架4 POCO)

时间:2010-10-24 20:29:21

标签: c# .net entity-framework entity-framework-4 unit-of-work

我找到了一些如何使用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和IDIpospos的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;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

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

  • 您不应让存储库处理TemplateEntities。这样做的原因是,当您在一个事务中需要两个存储库时,就会出现问题。您应该将TemplateEntities的责任移到与TransactionScope相同的级别;
  • TransactionScope应该移到更高的级别。优选地,TemplateEntities应该在TransactionScope;
  • 中实例化
  • 如果它不包含功能,则不必创建Save包装器。如果您在void SaveChanges()界面上指定IUnitOfWork,则会获取SaveChanges的{​​{1}};
  • 就个人而言,我不会TemplateEntities而是string GetUserNameByEmail(...),因为这也符合您的目的,并且您的优势在于,当您以后需要时,不会有两种方法可以通过电子邮件地址进行搜索User GetUserByEmail(...);
  • 您可能需要考虑将User GetUserByEmail(...)设为私有,或至少是ctx之类的私人制定者;
  • 您可以使用下面的示例创建一个抽象存储库。从长远来看,这将为您节省大量的沉闷:

-

public TemplateEntities Ctx { get; private set; }