0

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

我想要一个包含所有LoadSession属性TradingPartner Description的ViewModel ,但我不确定如何在 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类的外键 (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类的外键 (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; }
}
4

2 回答 2

0

好吧,这些都是您的域对象,对...

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

我不确定您的视图需要什么,但是从您的陈述中您声明您想要加载会话的属性 + 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; }
}

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

这有点原始,但理论应该成立......

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# 编译器运行此程序,但您应该了解总体思路。我确信有一个更优雅的模式可以完成同样的事情。但这是一个不错的解决方案。

于 2013-05-20T18:14:25.477 回答
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 助手可以很好地处理这个问题,但是如果您在视图中手动命名输入,请不要忘记调整这些名称以匹配。

于 2013-05-20T20:39:46.223 回答