如何使用存储库设计模式更新创建和修改的字段?

发布于 2024-07-09 18:07:02 字数 1767 浏览 8 评论 0原文

我目前正在开发我的第一个 asp.net mvc 应用程序。 我正在尝试吸收我看到的所有示例项目的精华,并尝试使用存储库设计模式

我有一个名为 IUserRepository 的接口,如下所示:

public interface IUserRepository
{
    IUser Add(IUser user);
    IQueryable<IUser> Fetch();
    IUser Update(IUser obj);
    void Delete(IUser obj);
}

我的 IUser 接口有一堆属性以及 CreatedBy、CreatedDate、ModifiedBy 和 ModifiedDate 的属性。

我的 UserRepository 如下所示:

public class UserRepository : IUserRepository
{
    private GimliDataContext db = null;

    public UserRepository()
    {
        db = new GimliDataContext();
    }

    public UserRepository(string connection)
    {
        db = new GimliDataContext(connection);
    }

    public IUser Add(IUser obj)
    {
        db.Users.InsertOnSubmit((User)obj);
        db.SubmitChanges();
        return obj;
    }

    public IQueryable<IUser> Fetch()
    {
        return db.Users.Select(b => b as IUser);
    }

    public IUser Update(IUser obj)
    {
        var user = db.Users.Where(u => u.UserId == obj.UserId).Single();
        user.Name = obj.Name;
        user.Password = obj.Password;
        user.Email = obj.Email;
        user.IsLocked = obj.IsLocked;
        user.IsDisabled = obj.IsDisabled;
        db.SubmitChanges();
        return user;
    }

    public void Delete(IUser obj)
    {
        var user = db.Users.Where(u => u.UserId == obj.UserId).Single();
        db.Users.DeleteOnSubmit(user);
        db.SubmitChanges();
    }
}

在我当前的 Web 应用程序中,当我的用户登录时,我将我的客户业务主体存储在当前线程上,当我的业务对象更新时,我只需使用当前业务主体的 Identity 属性上的 Name 属性,它就可以工作伟大的。

那么我的问题是什么? 我是否应该在存储库的 Add 或 Update 方法中执行相同的操作,或者实际上将 IUser 引用传递给当前 User 并从那里获取 UserId 会更好吗?

感谢任何意见!

I am currently working on my first asp.net mvc application. I am trying to drink the kool-aid of all the the sample projects I see and trying to use the repository design pattern.

I have a interface called IUserRepository that looks like the following:

public interface IUserRepository
{
    IUser Add(IUser user);
    IQueryable<IUser> Fetch();
    IUser Update(IUser obj);
    void Delete(IUser obj);
}

My IUser interface has a bunch of properties and properties for CreatedBy, CreatedDate, ModifiedBy and ModifiedDate.

My UserRepository looks like the following:

public class UserRepository : IUserRepository
{
    private GimliDataContext db = null;

    public UserRepository()
    {
        db = new GimliDataContext();
    }

    public UserRepository(string connection)
    {
        db = new GimliDataContext(connection);
    }

    public IUser Add(IUser obj)
    {
        db.Users.InsertOnSubmit((User)obj);
        db.SubmitChanges();
        return obj;
    }

    public IQueryable<IUser> Fetch()
    {
        return db.Users.Select(b => b as IUser);
    }

    public IUser Update(IUser obj)
    {
        var user = db.Users.Where(u => u.UserId == obj.UserId).Single();
        user.Name = obj.Name;
        user.Password = obj.Password;
        user.Email = obj.Email;
        user.IsLocked = obj.IsLocked;
        user.IsDisabled = obj.IsDisabled;
        db.SubmitChanges();
        return user;
    }

    public void Delete(IUser obj)
    {
        var user = db.Users.Where(u => u.UserId == obj.UserId).Single();
        db.Users.DeleteOnSubmit(user);
        db.SubmitChanges();
    }
}

In my current web applications when my users login I stash my customer business principal on the current thread and when my business objects update I just user the Name property on the Identity property of the current business principal and it works great.

So what's my question? Should I just do the same thing within the Add or Update methods of my repository OR would it be better to actually pass in an IUser reference to the current User and just grab the UserId from there?

Appreciate any input!

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

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

发布评论

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

评论(1

夏日落 2024-07-16 18:07:02

首先,我建议使您的存储库界面通用。 尝试使用以下方法代替 IUserRepository:

public interface IRepository<T>
{
    T Add(T entity);
    IQueryable<T> Fetch();
    T Update(T entity);
    void Delete(T entity);
}

这样您就可以将此接口用于任何域模型。

就您的问题而言(CreadedBy 和 ModifiedBy 属性),就个人而言,这听起来像是业务逻辑,并且更适合在 Service 类中。 但我会像你首先说的那样做。 获取当前经过身份验证的用户的用户 ID 并将其放在那里,而不是传递另一个 IUser 来获取其 ID。 但如果它在服务中,您就可以模拟不同的服务进行单元测试,而无需实际登录系统(让模拟的服务提供模拟的 IUser)。

我自己对此有点陌生,所以如果有更好的建议,我也想阅读。

First I would suggest making your repository interface generic. Instead of your IUserRepository try this:

public interface IRepository<T>
{
    T Add(T entity);
    IQueryable<T> Fetch();
    T Update(T entity);
    void Delete(T entity);
}

That way you can use this interface for any of your domain models.

As far as your question is concerned (CreadedBy and ModifiedBy props), personally, that sounds like business logic and would be better suited inside of a Service class. But I would do it like you first said. Take the currently authenticated user's user id and just put it in there, instead of passing in another IUser to get the ID of. But if it was in a service you would then be able to mock up a different service for unit testing, with out the hassle of having to actually log into the system (having the mocked service supply a mock IUser).

I myself am a bit new to this so if there are any better suggestions I would also like to read about them.

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