添加简单的业务逻辑ASP&gt将储存库; NET MVC 3 C# [英] Add simple business logic to repository in ASP>NET MVC 3 C#

查看:119
本文介绍了添加简单的业务逻辑ASP&gt将储存库; NET MVC 3 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于一个已经争论很多次计算器(我为此道歉),但没有一般的答案,因为话题从一个案件到另一个主体性曾经被赋予一个问题一个问题:我们可以添加业务根据存储库模式逻辑到存储库层?
我有一个的ViewModels MVC 3应用程序(这意味着我不使用的ViewData的话)。该模型是当然连接到数据库的LinqtoSQL EF。目前,我直接从包含所有的业务逻辑控制器访问实体和我需要的包裹在特定的ViewModels视图中的数据。
现在,我开始重构,我意识到,为了避免code复制的最好办法,除了优化的ViewModels,是所有查询委托给一个存储库,它与EF通信,并创建定制的方法通过使用控制器。
现在,考虑到​​我想库返回实际的对象,而不是前pressions,我在想,为了让我的code更清晰的委托小块的业务逻辑的存储库。
然而,对于松耦合的缘故,我想听听你的意见。在下面的code(目前位于控制器)所有的变量除
lprojectionPercactualValue 从数据库中获取。所以我想这块code移动到存储库,并调用与签名的方法:

 公共字符串的getColor(INT ITEMID,浮lprojectionPercactualValue);

该方法需要在 ITEMID ,以便检索特定于该项目的值。
你怎么看待这个设计决定是什么?它是更好地离开code控制器,移动到另一个方法仍然在控制器(创建的方法,甚至类专用)或将其移动到存储库的解释呢?

 如果(litem.Ascending ==真)
{
    如果(lprojectionPercactualValue< lminThreshold)
    {
        lcolor =红色;
    }
    否则,如果(lprojectionPercactualValue> lminThreshold和放大器;&安培; lprojectionPercactualValue< lmedThreshold)
    {
        lcolor =黄;
    }
    否则//(percValue> = item.Max_Threshold)
    {
        lcolor =绿色;
    }
}其他
{
    如果(lprojectionPercactualValue> lminThreshold)
    {
        lcolor =红色;
    }
    否则,如果(lprojectionPercactualValue< lminThreshold&放大器;&安培; lprojectionPercactualValue> lmedThreshold)
    {
        lcolor =黄;
    }
    否则//(percValue< = item.Max_Threshold)
    {
        lcolor =绿色;
    }
}


解决方案

不建议

如何库现在关于 lminThreshold ?如果这个值本身也需要来自查找表或配置文件是什么?

将其移动到业务层 - 如果你还没有一个,到控制器

I have a question concerning an issue that has already been disputed many times in stackoverflow (I apologize for this) but no general answer has ever been given because of the subjectivity of the topic from one case to another: can we add business logic to the repository layer according to the repository pattern? I have an MVC 3 application with ViewModels (that means I don't use the ViewData at all). The model is an LinqtoSQL EF of course connected to a database. Currently I am accessing the entities directly from the controllers which contain all the business logic and I wrap the data needed for the Views in specific ViewModels. Now I am starting to refactor and I realized that the best way to avoid code duplication, besides optimizing the ViewModels, is to delegate all the queries to a repository, which communicates with the EF, and create tailored methods to be used by the controller. Now, taking into account that I would like the repository to return actual objects and not expressions, I was thinking about delegating small pieces of business logic to the repository in order to make my code clearer. However for the sake of loose coupling, I would like to have your opinion. In the code shown below (which currently resides in the controller) all the variables except lprojectionPercactualValue are taken from the database. Therefore I wanted to move this piece of code to the repository and call a method with signature:

public string getColor (int ItemId, float lprojectionPercactualValue);

The method needs the ItemId in order to retrieve the values specific for that Item. What do you think about this design decision? Is it better to leave the code in the controller, move to another method still in the controller (create a method or even a class dedicated) or move it to the repository as explained?

if (litem.Ascending == true)
{
    if (lprojectionPercactualValue < lminThreshold)
    {
        lcolor = "RED";
    }
    else if (lprojectionPercactualValue > lminThreshold && lprojectionPercactualValue < lmedThreshold)
    {                                   
        lcolor = "YELLOW";
    }
    else //(percValue >= item.Max_Threshold)
    {
        lcolor = "GREEN";
    }
}

else
{
    if (lprojectionPercactualValue > lminThreshold)
    {
        lcolor = "RED";
    }
    else if (lprojectionPercactualValue < lminThreshold && lprojectionPercactualValue > lmedThreshold)
    {
        lcolor = "YELLOW";
    }
    else //(percValue <= item.Max_Threshold)
    {
        lcolor = "GREEN";
    }
}

解决方案

It is not recommended.

How does Repository now about the lminThreshold? What if this value itself needs to come from a lookup table or a config file?

Move it to business layer - and if you do not have one, to the controller.

这篇关于添加简单的业务逻辑ASP&gt将储存库; NET MVC 3 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆