数据实体>域对象>的ViewModels,每个大大不同的数据结构 [英] Data Entities > Domain Objects > ViewModels, each with drastically different data structures

查看:120
本文介绍了数据实体>域对象>的ViewModels,每个大大不同的数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是排序有关数据实体域对象,和的ViewModels之间的映射一个通用的问题。我可以不问正确的,但希望我能做出一些感觉吧。下面是一个简化的问题。

This is sort of a generic question in regards to mapping between data entities, domain objects, and ViewModels. I may not be asking it right but hopefully I can make some sense of it. Below is a simplified problem.

pretend我有1映射一个实体框架模型:1到我的数据库表,但我的域对象可能不相同,我的视图模型是截然不同的一次。作为一个伪例如:

Pretend I have an Entity Framework model which maps 1:1 to my database tables, but my domain objects may not be identical, and my ViewModel is drastically different again. As a pseudo-example:

数据库/ EF实体:


  • MembershipAccount

  • MembershipAccountExtraInfo

域:


  • 帐户

  • 简介

  • preferences

视图模型:


  • UserProfileModel

比方说,我需要显示它有一个UserProfileModel:用户名(从MembershipAccount 的),SignupDate(从MembershipAccount 的),全名(从MembershipAccountExtraInfo )和时区(从MembershipAccountExtraInfo 的)

Let's say I need to display a UserProfileModel which has: Username (from MembershipAccount), SignupDate (from MembershipAccount), FullName (from MembershipAccountExtraInfo), and TimeZone (from MembershipAccountExtraInfo)

我可能需要什么样的关系,在这里,和什么样的映射机制?它是常见的有类似的东西都需要一个MembershipAccount和MembershipAccountExtraInfo并返回一个账户的AccountMapper?我被困在当需要多个对象来创建一个单个域实体的映射,反之亦然一点。

What sort of relationships might I need here, and what sort of mapping mechanisms? Is it common to have something like an AccountMapper that takes both a MembershipAccount and MembershipAccountExtraInfo and returns an Account? I'm a bit stuck on the mapping when several objects are needed to create a single domain entity, and vice versa.

如果它可以帮助:我设计一个API,用于管理用户账户,用户配置文件,用户preferences等,但在数据库中的表是所有的地方。单个用户配置文件可能需要从数据跨越4-5桌和2个数据库的创建。没有1:我的数据库表和任何(逻辑)域对象之间的一对一映射

If it helps: I'm designing an API for managing User Accounts, User Profiles, User Preferences, etc. but the database tables are all over the place. A single User Profile might need to be created from data spanning 4-5 tables and 2 databases. There is no 1:1 mapping between my database tables and any (logical) domain objects.

谢谢!

推荐答案

我喜欢的工作让我的域对象尽可能接近它们重新present尽可能的对象。我的意思是,如果一个账户preferences,那么域帐户对象应包含 preferences 属性,最有可能的再$ p $由 preference 对象的集合psented。如果不出意外,这有助于用户轻松了解应用程序的数据结构。

I like to work keeping my domain objects as close to the objects that they represent as possible. What I mean by this is that if an account has preferences, then the domain Account object should contain a Preferences property, most likely represented by a collection of Preference objects. If nothing else, this helps the users understand the data structure of the application easily.

对于构建视图模型,这是最简单的有点...你所需要的任何东西刚添加的属性。什么类型的,你需要真的取决于你如何结构化你的域对象的属性。

As for constructing the view models, that's the easiest bit... you add just properties for anything that is required. What types of properties you would need would really depend on how you have structured your domain objects.

如果您认为有你在你的问题中提到的要求和你建模域紧密对象上,他们重新present的对象,然后通过它的声音,你只需要一个帐户的对象,因为这将包含 preference 简介里面的物体

If your view has the requirements that you mentioned in your question and you modelled your domain objects closely on the objects that they represent, then by the sounds of it, you would just need an Account object because that would contain the Preference and Profile objects inside it.

最后,唯一的映射,需要做可利用的实体框架的LinQ 查询完成。正是在这一点上,我加入了表和拉的任何数据,我需要为任何对象我的工作。这里是(使用 LINQ2SQL )的三个表从实例化数据对象的例子:

Finally, the only 'mapping' that needs to be done can be done with a LinQ query using the Entity Framework. It is at this point that I join the tables and pull whatever data that I need for whichever object I am working on. Here is an example of instantiating objects from data from three tables (using LinQ2SQL):

public AudioTracks GetAudioTracks(AudioTrackSearchOptions searchOptions)
{
    AudioTracks audioTracks;
    using (MidasDataContext dataContext = DataContext)
    {
        audioTracks = new AudioTracks(
            from audioTrack in dataContext.DbAudioTracks
            join masterTrack in dataContext.DbMasterTracks on audioTrack.MasterTrackId equals masterTrack.Id
            join masterTrackArtist in dataContext.DbDataLists on masterTrack.ArtistId equals masterTrackArtist.Id
            orderby string.Concat(masterTrack.Title, " (", audioTrack.Mix, ") - ", masterTrackArtist.Text)
            where (searchOptions.IsInactiveAudioTrackIncluded || audioTrack.IsActive)
            && (searchOptions.IsDeletedAudioTrackIncluded || !audioTrack.IsDeleted)
            select new AudioTrack(audioTrack.Id, masterTrack.Id, audioTrack.Isrc, masterTrack.Title, masterTrackArtist.Text, audioTrack.Mix, audioTrack.IsContentExplicit, audioTrack.IsActive, audioTrack.IsDeleted));
    }
    audioTracks.Sort(a => a.TitleWithMix);
    return audioTracks ?? new AudioTracks();
}


更新>>>


UPDATE >>>

延长我的 AudioTracks 例如向后工作,在 GetAudioTracks 方法是在一个名为的dataProvider 。它是从一个 GetAudioTracks名为方法,其中只是增加了用户的反馈和重试选项 DataController类类。这反过来又被称为 TracksModel 模式项目,该项目只包含的方法从<$ C某款$ C> DataController类类,涉及到不同类型的轨道中的应用。

Extending my AudioTracks example and working backwards, the GetAudioTracks method is in a project called DataProviders. It is called from a GetAudioTracks method in a DataController class which just adds user feedback and re-try options. That in turn is called by a TracksModel in the Models project which just contains a subsection of methods from the DataController class that relate to the various types of tracks in the application.

最后, AudioTracksViewModel 的ViewModels 项目调用 TracksModel.GetAudioTracks 在初始化方法时, AudioTracksView 是由用户加载的恰好。在 AudioTracksView 有一个的ListBox 上包含所有的左边 AudioTrack 符合用户搜索和/或过滤器选择对象。屏幕的右边有所选 AudioTrack 字段。这里是什么样子(如果该链接似乎断了,你可以在这里查看图像 ):

Finally, the AudioTracksViewModel in the ViewModels project calls the TracksModel.GetAudioTracks method upon initialisation which happens when the AudioTracksView is loaded by the user. The AudioTracksView has a ListBox on the left containing all of the AudioTrack objects that meet the users search and/or filter selections. The right of the screen has the fields for the selected AudioTrack. Here is what it looks like (if the link seems broken, you can view the image here):

与编辑按钮右边的更透明的字段是只读连接到收藏领域。编辑按钮打开一个对话框,让用户输入多个项目,然后将它们归纳在外地。所有在应用程序中的对象都具有或多或少的复杂性相似的看法。

The more transparent fields with an edit Button on the right are read only fields connected to collections. The edit Button opens a dialog to let the user enter multiple items, which are then summarised in the field. All of the objects in the application have similar views of more or less complexity.

这篇关于数据实体&GT;域对象&GT;的ViewModels,每个大大不同的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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