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

发布于 2024-09-29 00:54:30 字数 4869 浏览 5 评论 0原文

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

I found some examples of how to create unit of work with ef4, i haven't used di/ioc and i would like to keep things simple and this an example (90% inspired) and i think it's ok but since i am looking at a pattern to use from now on i would like to ask an opinion one last time.

 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();
    }
}

And finally

    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;
                    }
                }
            }
        }
    }

After getting rid if the IUnitOfWork and IDisposal the CreateUser looks like this:

        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;
                    }
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-10-06 00:54:30

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

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

-

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();
    }

    /* ... */
}

This looks basically OK. A few suggestions though:

  • You should not let the repository dispose the TemplateEntities. The reason for this is that when you need two repositories within one transaction, you have a problem. You should move the responsibility of disposing the TemplateEntities to the same level as the TransactionScope;
  • The TransactionScope should be moved to a higher level. Preferably, the TemplateEntities should be instantiated within a TransactionScope;
  • You don't have to create the Save wrapper if it does not contain functionality. If you specify the void SaveChanges() on the IUnitOfWork interface, this will pick up the SaveChanges of the TemplateEntities;
  • Personally I would not have string GetUserNameByEmail(...) but rather User GetUserByEmail(...) because then this will also serve your purpose and you have the advantage of not having two methods that search by e-mail address when you later need the User GetUserByEmail(...);
  • You may want to think about making ctx private, or at least a private setter like public TemplateEntities Ctx { get; private set; };
  • You could create an abstract repository with methods like the example below. This will save you a lot of dull typing in the long run:

-

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();
    }

    /* ... */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文