实体框架 - 业务对象/模型的额外动态计算属性 (ASP.NET MVC)

发布于 2024-08-24 05:35:52 字数 288 浏览 3 评论 0原文

假设我有包含以下字段的客户模型:Id、FirstName、LastName。我想在列表视图中显示客户列表。为此,我使用我的服务方法,该方法返回列表以在视图中进行迭代。

但现在我还想显示每个客户的一些附加信息,例如信用。这些信息并不存储在数据库的特定字段中,而是必须根据 Transacitons 中的数据进行计算。我应该在哪里进行这个计算?

我可以有一个名为 Credit for Customer 模型的额外字段,并在 getter 中执行此操作,但我不确定这是否是最好的方法,特别是在 od 模型内部我无权访问 EF 上下文。

Let's say that I have Customer model with following fields: Id, FirstName, LastName. I want to present list of Customers in List View. For this I use my service method which return List to iterate in view.

But now I want also to display some additional information for each customer, for example credit. This information is not stored in specific field in database, but has to be calculated based on data in Transacitons. Where should I do this calculation?

I can have extra field called Credit for Customer model, and do it in getter, but I'm not sure if this is the best way, especially that inside od Model I don't have access to EF context.

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

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

发布评论

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

评论(1

情独悲 2024-08-31 05:35:52

假设您的问题措辞正确,并且这实际上只是用于显示,而不是用于业务流程(这将是一个完全不同的问题)......

在您的演示模型中执行此操作。添加所需的输入,投影到它们上,然后计算输出。这是一个非常简单的例子。现实世界要复杂得多。

class Person // entity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class PersonPresentation
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName 
    {  
        get 
        { 
            return string.Format("{0} {1}", this.FirstName, this.LastName);
        }
    }
}

public ActionResult DisplayPeople()
{
    var model = from p in Repository.AllPeople()
                select new PersonPresentation
                {
                    FirstName = p.FirstName,
                    LastName = p.LastName
                };
    return View(model);
}

Presuming that your question is correctly worded and this really is just for display, not for business processes (which would be an entirely different question)...

Do it in your presentation model. Add the inputs you need, project onto them, and calculate the output. Here's a very trivial example. The real world is more complicated.

class Person // entity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class PersonPresentation
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName 
    {  
        get 
        { 
            return string.Format("{0} {1}", this.FirstName, this.LastName);
        }
    }
}

public ActionResult DisplayPeople()
{
    var model = from p in Repository.AllPeople()
                select new PersonPresentation
                {
                    FirstName = p.FirstName,
                    LastName = p.LastName
                };
    return View(model);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文