简体   繁体   English

模型应在哪里使用Uow和存储库模式

[英]Where should Model be using Uow and Repository Pattern

Having a repository pattern, in the controller one can do: 具有存储库模式,在控制器中可以执行以下操作:

public class ProductController : Controller {
    private IUow Uow;

    public ProductController(IUow uow) {
        Uow = uow;
    }


    [HttpGet]
    public ActionResult Edit(int id) {
        ViewData.Model = Uow.Products.Get(id);
        return View();
    }

    [HttpPost]
    public RedirectToRouteResult Edit(int id, ProductEditBindingModel model) {
        if (ModelState.IsValid) {
            Uow.Products.Update(id, model);
        }
        return RedirectToAction("Edit");
    }
}

Example simplified 示例简化

That is ok, but what happen when your business logic is a little bit complex? 没关系,但是当您的业务逻辑有点复杂时会发生什么? let's say using various methods from Unit of Work or other logic. 假设使用工作单元中的各种方法或其他逻辑。

For example, let's say we need to : Audit the edit, update an underlying index, add scores to user for updating the product, publish the update into the users 'wall', whatever you can imagine. 例如,假设我们需要进行以下操作:审核编辑,更新基础索引,为用户添加分数以更新产品,将更新发布到用户的“墙”中,无论您能想象到什么。

It doesn't feels right (and I'm sure it's not) to populate the controller with lots of code, so I feel natural to just use a Business Model like : 用大量代码填充控制器感觉不正确(而且我敢肯定这是不对的),所以我很自然地只使用像这样的业务模型:

[HttpPost]
public RedirectToRouteResult Edit(int id, ProductEditBindingModel model) {
    if (ModelState.IsValid) {
        Model.UpdateProduct(id, model);
    }
    return RedirectToAction("Edit");
}

The model, would need to access to the Uow so I think the Model should looks like something like : 该模型需要访问Uow,因此我认为该模型应类似于:

public class ProductModel {
    private IUow Uow;

    public ProductModel(IUow uow) {
        Uow = uow;
    }
    public void UpdateProduct(int id, ProductEditBindingModel model) {
        // all logic with access to the Unit of Work.
    }
}

and the product controller like : 和产品控制器类似:

public class ProductController : Controller {
    private IUow Uow;
    private ProductModel Model;
    ProductController(IUow uow) {
        Uow = uow;
        ProductModel = new ProductModel(uow);
    }
    ....
}

Is this ok? 这个可以吗? One thing is that ProductController now depends on ProductModel, so I think it would be wise to create an Interface (IProductModel) and inject the dependency, but that means the constructor of ProductController will need to receive IUow and IProductModel. 一件事是ProductController现在依赖于ProductModel,所以我认为创建接口(IProductModel)并注入依赖关系是明智的,但这意味着ProductController的构造函数将需要接收IUow和IProductModel。 What happens when you need to access more than one Model? 当您需要访问多个模型时会发生什么? Creating exactly one model for that controller alerts me of duplicating code. 为该控制器创建一个准确的模型会提醒我代码重复。 Using multiple models on Controller dependecy 'looks fine' but something make me feel there's a key concept or 'thing' I'm missing. 在Controller依赖项上使用多个模型“看起来不错”,但是有些使我感到缺少的关键概念或“事物”。

Another things that I'm wondering is, if we like to stick to a pattern, in a simple get scenario, I can do Uow.Products.Get(id) but sometimes I will be accessing the Uow directly and others the Model. 我想知道的另一件事是,如果我们喜欢坚持一种模式,在一个简单的获取场景中,我可以执行Uow.Products.Get(id)但有时我将直接访问Uow,而其他人则将访问Model。 Would be wise to always access the Model like ProductModel.Get(id) instead ? 始终像ProductModel.Get(id)一样访问模型是否明智? I don't like to decide whenever I need to access model and when I need to just use the repository. 我不想决定何时需要访问模型以及何时仅需要使用存储库。 looks like a bad strategy that doesn't scale up very well. 看起来是一个不好的策略,无法很好地扩展。

Another option that comes into my mind is using the Repository as a Model, but I quickly discarded because the ProductRepository does not know about the UserRepository so logic that must interact with both doesn't fit. 我想到的另一个选择是将存储库用作模型,但由于ProductRepository不了解UserRepository,因此我很快就放弃了,因此必须与两者交互的逻辑不适合。

I also don't like the idea of a ProductModel accessing the UserRepository, that set me back to the controller doing all the 'hard' work which I know it's bad. 我也不喜欢使用ProductModel访问UserRepository的想法,这使我回到控制器上来做所有的“艰苦”工作,我知道这很糟糕。

I hope you understand my situation, I tried to describe the best I can. 希望您了解我的处境,我力求尽我所能。 The question is : How should I manage complex action in a Uow / Repository Pattern in MVC, and where the proper place of the BusinessLogic Model, who is responsible for the call to, and how complex action that access more than one repository are managed, accessed, etc. 问题是:如何在MVC中的Uow /存储库模式中管理复杂的操作,以及应由谁负责调用的BusinessLogic Model的正确位置,以及如何管理访问多个存储库的复杂操作,访问等

I think your approach is almost ok. 我认为您的方法几乎可以。 The name ProductModel suggests that it's a model, and therefore it should not contain any business logic. 名称ProductModel暗示它是一个模型,因此它不应包含任何业务逻辑。 You need to create a ProductRepository to handle the business logic for the ProductModel . 您需要创建一个ProductRepository来处理ProductModel的业务逻辑。

Take a look at this article . 看一下这篇文章 Here the class Student is the model, and StudentRepository handles the business logic. 这里,类Student是模型,而StudentRepository处理业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC存储库模式和UOW - MVC Repository pattern and UOW EF5中的UOW和存储库模式 - UOW and Repository Pattern in EF5 无法从存储库转换为IRepository UOW存储库模式 - Cannot convert from Repository to IRepository UOW Repository pattern 在UOW类(通用存储库模式)中拥有所有表属性是否合适? - Is it good to have all table properties in UOW class (Generic repository pattern) UoW和存储库+服务层 - UoW & Repository + Service Layer 在具有存储库,服务层和使用模型绑定器的ASP.Net MVC场景中应该进行验证? - Where should be the validation in a ASP.Net MVC scenario having Repository, Service Layer and using Model Binder? 在存储库模式中,我应该使用数据库模型作为我的View模型还是应该为此创建单独的ViewModel? - In repository pattern, should I use database model as my View model or should I create separate ViewModel for that? WebAPI ODATA —使用标准控制器/ Ioc /存储库/ UoW排序体系结构与MVC一起使用 - WebAPI ODATA — Consuming With MVC Using Standard Controller/Ioc/Repository/UoW Sort of Architecture 在存储库模式中使用 ApplicationDbContext - Using ApplicationDbContext in Repository Pattern 具有MVC的经典ado.net的UOW +存储库 - UOW + Repository for classic ado.net with MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM