简体   繁体   English

MVC“创建用户”方法

[英]MVC 'create user' method

At the moment, I have the need to register a new user, and at the same time, create an 'account portfolio' record, and then assign him to that portfolio. 目前,我需要注册一个新用户,同时创建一个“帐户投资组合”记录,然后将他分配给该投资组合。

So, 3 database writes. 因此,有3个数据库写入。

I have a Twitter-Bootstrap UI, using MVC4, which then goes to a Service, Logic and then Data access layer, which uses Entity Framework 5 to save to SQL Server. 我有一个使用MVC4的Twitter-Bootstrap UI,然后转到服务,逻辑和数据访问层,后者使用Entity Framework 5保存到SQL Server。

At the moment, when the user clicks 'Register', I create a 'User' data transfer object, which basically has details about the user. 此刻,当用户单击“注册”时,我将创建一个“用户”数据传输对象,该对象基本上包含有关用户的详细信息。 I then create a 'Portfolio' object, which just has 'Description' and 'Id'. 然后,我创建一个“ Portfolio”对象,该对象仅具有“ Description”和“ Id”。 I have created a service method in my 'User' service class called 'RegisterNewUserWithPortfolio'. 我在“用户”服务类中创建了一个服务方法,称为“ RegisterNewUserWithPortfolio”。 It takes the two objects. 它需要两个对象。 It then calls a Logic service class method in my UserLogic, with the same name and parameters. 然后,它使用相同的名称和参数在UserLogic中调用Logic服务类方法。

It then calls a 'Save' method in the UserData class, passing it the UserDto, which gets back the new Id of the User it just saved. 然后,它在UserData类中调用“保存”方法,将其传递给UserDto,该方法将获取刚刚保存的User的新ID。

public class PersonDto
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
}

It then calls the Save method in the AccountPortfolioData class, passing it the PortfolioDto, which is saved, and again, returns the Id. 然后,它在AccountPortfolioData类中调用Save方法,将其传递给它保存的PortfolioDto,然后再次返回ID。

I then call a method in the UserData class called 'AssignUserToPortfolio', taking the UserId, and the PortfolioId... and that then gets saved. 然后,我在UserData类中调用一个名为“ AssignUserToPortfolio”的方法,并使用UserId和PortfolioId ...,然后将其保存。

I now have a new user, linked to a Portfolio. 我现在有一个新用户,已链接到投资组合。

I can't do that in a EF Transaction, because I am calling different Data saving classes (As each of my entities has it's own class, to help keep them apart and easier to manage). 我无法在EF事务中执行此操作,因为我要调用不同的数据保存类(由于我的每个实体都有其自己的类,因此有助于使它们分开并易于管理)。

First, am I doing this right? 首先,我这样做正确吗? Is there a better way? 有没有更好的办法? And if so, is there a way I can put the whole group of calls into a transaction? 如果是这样,有什么办法可以将整个呼叫组都放入事务中?

Note, my Dto objects are just custom, dumb objects used to pass the data between the layers. 请注意,我的Dto对象只是用于在图层之间传递数据的自定义哑对象。 I am also not passing EF models to the UI. 我也没有将EF模型传递给UI。 I am trying to keep it all layered and separated. 我试图将其全部分层和分离。

The question isn't very clear, but from what I uderstand the easiest way would to map ER relationships for your User and Portfolio class 问题不是很清楚,但是据我了解,最简单的方法是为您的User和Portfolio类映射ER关系

Thus 从而

Public class User{

//
//
//
 public virtual ICollection<Portfolio> Portfolios
}

Public Class Portfolio{
//
public int UserId{get; set;}

[ForeignKey("UserId")
public virtual User user{get; set}
} 

Entity Framework would automatically map the relationships. 实体框架将自动映射关系。 Hope this gets you started. 希望这可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM