【问题标题】:Suggestion on Model Design模型设计建议
【发布时间】:2013-12-04 10:21:41
【问题描述】:

我有一个模型/POCO,它具有收入、资产比率、回报率等属性,这些属性是用户的绩效管理指标。这些属性中的每一个都是根据公式计算的。其中大多数需要查询不同的表来填充数据进行计算。目前我有两种方法:

public class Performance
{
  public decimal Revenue {get; set;}
  public decimal AssetRatio {get; set;}

  // And so on
}

case 1

public class Helper
{

public List<Performance> GetPerformance()
{   

var context = new SomeDbContext();

var revenue = context.Where(). some operation
var assetRatio = context.Where().. so on

 //Each will have collection of user and performance indicator type.
//Eg: revenue is a list of users and revenue 
 //Now I Join all tables based on userId and create List<Performance>
//And return List<Performance>

}
}

I find this approach tedious and I tried another approach

case 2

public class Performance
{

private SomeDbContext  _ctx = new SomeDataContext();  

public SomeDbContext(int userId)
{
  this.UserId = userId;
}

public int UserId {get; set;}
public decimal Revenue {get; set;}
public decimal AssetRatio {get; set;}

// And so on
// I have Private Methods that is dedicated to populate each associated property

private decimal getRevenue()
{
 decimal revenue = ctx. ../ and so on
}

}


//This class is passed list of user id  as list<int>
foreach (var user in UserList)
{
   someList.Add(new Performance(user));
}

现在我对两者都不满意。在一种方法中,我在一个助手类中做所有事情,但在另一种方法中,我每次为每个用户调用不同的表。如果有人能指导我一个更好的解决方案,我真的很感激。谢谢!

【问题讨论】:

    标签: asp.net-mvc oop poco


    【解决方案1】:

    注意访问数据库的次数。第一种方法可能会更好,因为您可以编写一个 linq 查询来读取和分组或汇总所有数据。

    更改您的连接字符串以添加

    Application Name=KrishApplication
    

    打开 SQL Management Studio 并使用 SQL Server Profiler 查看正在进行的 SQL 调用。

    (过滤应用程序名称,如 KrishApplication...)

    当您获得性能数据时,您可能希望将它们添加到您的经销商对象中。添加此属性...

    [NotMapped]
    public Performance Performance{get; set;}
    

    确保您可以在 Helper 类上设置过滤器。然后,您可以显示过去一个季度、一年、年初至今的绩效等。如果您需要过滤某些甚至只有一个经销商,那么您可以使用相同的帮助器类。

    或者...

    如果您可以在 SQL 中进行所有计算,那么这可能是最好的方法。您甚至可以编写一个存储过程或视图来一次性获取所有经销商及其绩效统计数据。

    create view DealersPlus as
    select DealerId, DealerName, (select sum(Amount) from tblDeal dd 
        where dd.DealerId = d.DealerId) Amount
    from tblDealers d
    

    并填充它

    var dealers = SomeDbContext.Database
        .SqlQuery<DealerPlus>("select * from DealersPlus")
        .ToList();
    
    • 列表项

    【讨论】:

    • 完全同意你。我正在考虑在数据库中创建视图。
    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多