English 中文(简体)
1. 固定和衍生的财产,有自动制成
原标题:Flattened and derived properties with Automapper

I m从事一项工作。 MUVC站点,使用<0> 和<0>。 我在设计上取得了良好进展,并在设计上取得了良好进展(我认为这是我的第一份报告,是真实的D)。 但是,我开始看到一些我不满意的东西。

我的问题很可能与我缺乏自治和民主与发展的经验有关,因此,某些人可能比较容易回答,并向我指出了正确的方向。 我也非常有机会处理这一完全错误,但我的方向是根据我在这里所读的,有些是从搜寻,有些是从问我自己的问题。 但是,我也开始认识到,我看到的一些“子宫”相互矛盾。

我的田径模型只是欧洲4组织模版(Aemic)产生的POCOs。 这些应用在我的服务层使用,也通过服务层接触我的申请。 我没有哪一类DTO,仅是我的流行病领域模型、观察模型和测绘二者的自动地图,每张advice 我获得。 假设他们能够被一对一地绘制地图,这是所有罚款和dan。

我面临的问题是,当我不得不将我的领域模式统一为一种观点模式时,或者当我需要某种商业逻辑来界定一种观点模式的财产时。

两个例子:

我有两种模式,即用户和用户档案。 这是一种一对一的绘图,因此,这在逻辑上已经很平坦——它只是两个单独的模型。 我认为,模型需要用户和用户档案的特性。 从我所看到的情况来看,自动地图仪无法轻易做到这一点,因此,我的确扩大了我的用户POCO,为它增加了必要的用户档案特性:

public string UserName
{
    get { return UserProfile.UserName; }
}

我不是这种大狂热,因为它似乎违反了南盟,并且可能成为我必须做的更大痛苦之地。

Encapsulating business/2007/1

我还有一个案例,即用户财产是储存的,而是衍生的,在返还价值之前需要做一些逻辑。 例如,URL的形象将取决于是否在Facebook或Twitter上登记。 因此,我有这样的想法:

public string ProfileImageUrl
{
    get
    {
        if (User.TwitterId != null)
        {
            // return Twitter profile image URL
        }
        if (User.FacebookId != null)
        {
            // return Facebook profile image URL
        }
    }
}

我确信,这是自治政府负责的一点,但我不敢肯定,如果我想要保持这一流行病,这是否应该以领域模式加以推广。 因此,我不能确定属于哪里。

I m not completely stuck on having my domain model be anemic (I know that s its own debate), but I do anticipate this domain model being used by multiple applications with different view models and validation.

Thank in advance.

<><>UPDATE: 针对`jfar',我的主要问题就是,似乎像这一样,应该能够做的是“自动地图”。 否则,我就能够坚持这一选择。 我只是想有人把我的设计增加一倍,以确保这样做没有更好的办法。

My issue with the encapsulating business logic example, is that I m violating the anemia of my domain model. In some cases, this may even need to hit the repositories to pull certain data for business logic, and this seems like a bad design to me.

The other thing I m starting to wonder, is if I shouldn t have a DTO layer, but I honestly don t want to go that route either (and per the question I linked above, it seems to be more accepted not to use DTOs w/ DDD).

最佳回答
  1. Flattening: You can chain your mappings together using Automapper s ability to map to an existing object. Chain Example
  2. Encapsulatng Business Logic: I don t really see your problem. However, if you want Automapper to be responsible, take a look at before and after maps or custom resolvers. Custom Resolver Example
问题回答

Why are you so intent on keeping your domain model anemic? It looks to me like your domain objects are nothing more than DTOs.

属于贵国拥有必要信息归还正确URL的领域类别。 页: 1 用户Profile。 如果单一类别没有所需信息,说明服务情况。 你要么在你的工作上制造一种方法,将URL或Darin Dimitrov等复合物体退回。

Firestrand and Darin Dimitrov s answers are both good ways to accomplish flattening.

Define a model that regroups those two models:

public class UserWithProfile
{
    public User User { get; set; }
    public UserProfile Profile { get; set; }
}

之后,你的服务层回去了这一模式。 如果用户与用户Profile之间有1--1图谱,则可以确定以下模型:

public class UserWithProfile: User
{
    public UserProfile Profile { get; set; }
}

Then define a view model containing only what is needed by the given view:

public class SomeUserViewModel
{
    ... only properties needed by the given view
}

next define a mapping between your model and your view model:

Mapper.CreateMap<UserWithProfile, SomeUserViewModel>()
      ...

and finally in your controller use the service layer to fetch the model, map it to a view model and pass this view model to the view for rendering, pretty standard stuff.

I searched and read about flattening more, and the more I searched, to more it looked like Automapper should be handling this automagically. After playing around a bit, I found a way to get this to work, though I still can t find any examples of doing this, even with Automapper s documentation.

The key is to name the property on the destination object with the fully qualified name of the source object graph.

Source objects:

class User
{
    int Id { get; set; }
    FacebookUser FacebookUser { get; set; }
}

class FacebookUser
{
    string UserName { get; set; }
}

Destination objects:

class UserViewModel
{
    int Id { get; set; }
    string FacebookUserUserName { get; set; }
}

因此:

UserViewModel.Id -> User.Id
UserViewModel.FacebookUserUserName -> User.FacebookUser.UserName

也许这在我看来是显而易见的,也许对大多数人来说是这样。 现在我想到的是,这确实是有意义的——它确实是它能够工作的唯一途径。 直到现在,我才看不到。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签