创建包含ASP.NET MVC中的父类属性的ViewModel

时间:2013-05-20 17:46:29

标签: c# asp.net-mvc entity-framework ef-database-first

我试图了解如何创建一个ViewModel,它包含我的域模型中的类的属性以及父类的属性。

我希望ViewModel包含所有 LoadSession 属性 TradingPartner说明,但我不知道如何将这一切都映射到ViewModel中。任何帮助或建议将不胜感激。

这是我正在访问的主要类 LoadSession

public partial class LoadSession
{
    public LoadSession()
    {
        this.AcceptedTransactions = new HashSet<AcceptedTransaction>();
        this.RejectedTransactions = new HashSet<RejectedTransaction>();
    }

    public int LoadSessionId { get; set; }
    public int Import { get; set; }
    public string FilePath { get; set; }
    public string TradingPartnerBatchId { get; set; }
    public System.DateTime Started { get; set; }
    public int RecordsOnFile { get; set; }
    public int RecordsAfterGroupFilter { get; set; }
    public int RecordsAccepted { get; set; }
    public int RecordsRejected { get; set; }
    public System.DateTime Completed { get; set; }
    public bool Success { get; set; }
    public Nullable<int> Extract { get; set; }

    public virtual ICollection<AcceptedTransaction> AcceptedTransactions { get; set; }
    public virtual Extract Extract1 { get; set; }
    public virtual Import Import1 { get; set; }
    public virtual ICollection<RejectedTransaction> RejectedTransactions { get; set; }
}

Import属性是此导入类的外键(Import = ImportId):

public partial class Import
{
    public Import()
    {
        this.GroupPlans = new HashSet<GroupPlan>();
        this.ImportGroups = new HashSet<ImportGroup>();
        this.MatchingGroups = new HashSet<MatchingGroup>();
        this.LoadSessions = new HashSet<LoadSession>();
    }

    public int ImportId { get; set; }
    public string Description { get; set; }
    public int Format { get; set; }
    public int Interface { get; set; }

    public virtual Interface Interface1 { get; set; }
    public virtual Format Format1 { get; set; }
    public virtual ICollection<GroupPlan> GroupPlans { get; set; }
    public virtual ICollection<ImportGroup> ImportGroups { get; set; }
    public virtual ICollection<MatchingGroup> MatchingGroups { get; set; }
    public virtual ICollection<LoadSession> LoadSessions { get; set; }
}

Interface属性是此接口类的外键(Interface = InterfaceId):

public partial class Interface
{
    public Interface()
    {
        this.Extracts1 = new HashSet<Extracts1>();
        this.Imports = new HashSet<Import>();
    }

    public int InterfaceId { get; set; }
    public string Description { get; set; }
    public int TradingPartner { get; set; }

    public virtual ICollection<Extracts1> Extracts1 { get; set; }
    public virtual ICollection<Import> Imports { get; set; }
    public virtual TradingPartner TradingPartner1 { get; set; }
}

且TradingPartner属性是此 TradingPartner 类的外键(TradingPartner = TradingPartnerId):

public partial class TradingPartner
{
    public TradingPartner()
    {
        this.Interfaces = new HashSet<Interface>();
    }

    public int TradingPartnerId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Interface> Interfaces { get; set; }
}

2 个答案:

答案 0 :(得分:0)

嗯,这些都是你的域名对象......

创建一个存储库,将您的Domain对象转换为具有所需属性的视图模型...

我不确定您的观点需求是什么,但是根据您的陈述,您声明您需要加载会话+ TradingPartner.Description的属性所以创建这样的东西......

public class LoadSessionTradingPrtNrVM
{
    public LoadSession()
    {
        this.AcceptedTransactions = new HashSet<AcceptedTransaction>();
        this.RejectedTransactions = new HashSet<RejectedTransaction>();
    }

    public int LoadSessionId { get; set; }
    public int Import { get; set; }
    public string FilePath { get; set; }
    public string TradingPartnerBatchId { get; set; }
    public System.DateTime Started { get; set; }
    public int RecordsOnFile { get; set; }
    public int RecordsAfterGroupFilter { get; set; }
    public int RecordsAccepted { get; set; }
    public int RecordsRejected { get; set; }
    public System.DateTime Completed { get; set; }
    public bool Success { get; set; }
    public Nullable<int> Extract { get; set; }
    public string Description { get; set; }

    public virtual ICollection<AcceptedTransaction> AcceptedTransactions { get; set; }
    public virtual Extract Extract1 { get; set; }
    public virtual Import Import1 { get; set; }
    public virtual ICollection<RejectedTransaction> RejectedTransactions { get; set; }
}

要从域模型到ViewModel,您可以使用存储库或其他一些模式来获取从数据库获取的内容,并将其转换为您所需的视图。

这有点原始,但理论应该坚持......

public class DataRepository {

      LoadSessionTradingPrtNrVM TransformToVM(LoadSession inputA, TradingPartner inputB){
            LoadSessionTradingPrtNrVM newOBJ = new LoadSessioNTradingPrtNrVM();
            newOBJ.LoadSessionId = ipnutA.LoadSessionID;
            newOBJ.Import = inputA.Import
            //Here is the property from your Transform object
            newOBJ.Description = inputB.Description
            //...  Continue to transform one object into the other... 
            //You could add as many members from as many different objects as you want into 
            //Your view model following that pattern. 
      }
}

我没有机会通过C#编译器运行它,但你应该得到一般的想法。我相信有一个更优雅的模式可以完成同样的事情。但这是一个不错的解决方案。

答案 1 :(得分:0)

另一种选择是将域模型对象包含在视图模型中作为属性。例如:

// View model.
public class UserViewModel
{
    public AddressModel Address;  // Assuming "AddressModel" is a doman model.
    public string FirstName;
    public string LastName;
}

在视图中,您可以访问属性:

@Model.Address.AddressLine1
@Model.Address.City
// etc...

Html助手可以很好地处理这个问题,但如果您在视图中手动命名输入,请不要忘记调整这些名称以匹配。