首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >存储库模式和UOW模式C#的更新实体

存储库模式和UOW模式C#的更新实体
EN

Software Engineering用户
提问于 2019-06-05 06:10:42
回答 1查看 2.4K关注 0票数 -1

在一个模拟解决方案中,我创建了3个模拟项目来实现分层架构设计,主要是表示层(Web) -域模型层-基础结构层。我还没有添加应用程序服务层(Web )。

为了简单起见,我只有两个域模型实体: Customer.cs和Category.cs。在域模型层中,我还放置了存储库接口(包括自定义的和通用的)。

在基础设施层,我有dbcontext、Work (包括它的接口)和存储库类(它们实现了存储库接口)。

下面是我的通用存储库接口

代码语言:javascript
运行
AI代码解释
复制
public interface IRepositoryGeneric<TEntity> where TEntity : class
{
    // CRD
    void Add(TEntity entity);
    void Remove(TEntity entity);
    TEntity Get(object Id);
    IEnumerable<TEntity> GetAll();

    void Save();
}

泛型仓库类

代码语言:javascript
运行
AI代码解释
复制
public class RepositoryGeneric<T> : IRepositoryGeneric<T> where T : class
{
    protected readonly AppDbContext db;
    public RepositoryGeneric(AppDbContext db)
    {
        this.db = db;
    }

    public void Add(T entity)
    {
        db.Set<T>().Add(entity);
    }

    public T Get(object Id)
    {
        return db.Set<T>().Find(Id);
    }

    public IEnumerable<T> GetAll()
    {
        return db.Set<T>().ToList();
    }

    public void Remove(T entity)
    {
        db.Set<T>().Remove(entity);

    }

    public void Save()
    {
        db.SaveChanges();
    }
}

工作单元接口

代码语言:javascript
运行
AI代码解释
复制
public interface IUnitOfWork : IDisposable
{
    IRepositoryCustomer Customers { get; }
    IRepositoryCategory Categories { get; }

    int Save();
}

工级单位

代码语言:javascript
运行
AI代码解释
复制
public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext db;
    public UnitOfWork()
    {
        db = new AppDbContext();
    }

    private IRepositoryCustomer _Customers;
    public IRepositoryCustomer Customers
    {
        get
        {
            if (this._Customers == null)
            {
                this._Customers = new RepositoryCustomer(db);
            }
            return this._Customers;
        }
    }

    private IRepositoryCategory _Categories;
    public IRepositoryCategory Categories
    {
        get
        {
            if (_Categories == null)
            {
                _Categories = new RepositoryCategory(db);
            }
            return _Categories;
        }
    }

    public void Dispose()
    {
        db.Dispose();
    }

    public int Save()
    {
        return db.SaveChanges();
    }
}

我了解到,在我的通用存储库接口中不应该有更新方法。但是如果没有更新方法,我现在只能在我的演示MVC web控制器中使用编辑方法。下面,我只提取了编辑操作方法。错误出现在那一行粗体中。

代码语言:javascript
运行
AI代码解释
复制
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "CategoryID,CategoryName,Description")] Category category)
    {
        if (ModelState.IsValid)
        {
            **db.Entry(category).State = EntityState.Modified;**
            db.Save();
            return RedirectToAction("Index");
        }
        return View(category);
    }

最后,我的类是否在正确的层中创建?例如,IUnitOfWork.cs应该在基础设施层吗?

我的整个解决方案源代码都可以在我的github - https://github.com/ngaisteve1/LayeredArchitectureDesignCSharp上找到。

EN

回答 1

Software Engineering用户

回答已采纳

发布于 2019-06-05 14:28:05

通用存储接口

代码语言:javascript
运行
AI代码解释
复制
public interface IRepositoryGeneric<TEntity> where TEntity : class
{
    // CRD
    void Add(TEntity entity);
    void Remove(TEntity entity);
    TEntity Get(object Id);
    IEnumerable<TEntity> GetAll();

    void UpdateState(TEntity entity);
    void Save();
}

存储库泛型类

代码语言:javascript
运行
AI代码解释
复制
public class RepositoryGeneric<T> : IRepositoryGeneric<T> where T : class
{
    protected readonly AppDbContext db;
    public RepositoryGeneric(AppDbContext db)
    {
        this.db = db;
    }

    public void Add(T entity)
    {
        db.Set<T>().Add(entity);
    }

    public T Get(object Id)
    {
        return db.Set<T>().Find(Id);
    }

    public IEnumerable<T> GetAll()
    {
        return db.Set<T>().ToList();
    }

    public void Remove(T entity)
    {
        db.Set<T>().Remove(entity);

    }

    public void UpdateState(T entity)
    {
        db.Entry(entity).State = EntityState.Modified;
    }

    public void Save()
    {
        db.SaveChanges();
    }
}

Web控制器

代码语言:javascript
运行
AI代码解释
复制
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "CategoryID,CategoryName,Description")] Category category)
    {
        if (ModelState.IsValid)
        {
            db.Categories.UpdateState(category);
            db.Commit();
            return RedirectToAction("Index");
        }
        return View(category);
    }
票数 0
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/392920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文