简体   繁体   English

在 class 中自动填充属性

[英]Auto populate properties in class

Suppose that I have a class name User, is there any ways to populate the class properties and default value in Visual Studio as follow.假设我有一个 class 名称用户,是否有任何方法可以在 Visual Studio 中填充 class 属性和默认值,如下所示。 I feel it is tedious to repeat typing the properties again and again.我觉得一遍又一遍地重复输入属性很乏味。

Probably there is default shortcut to do this in Visual Studio or any extension that can do this?在 Visual Studio 或任何可以执行此操作的扩展中可能有默认快捷方式来执行此操作?

Case 1情况1

var user = db.User.Where(x => x.UserID).Select(o => new User
{
   UserId = o.UserId, // auto populate
   Username = o.Username, // auto populate
   ........  // auto populate all properties inside User class
}
);

case 2案例2

var user = new User();
user.UserID = "",  // auto populate with type default value
user.Username = "",  // auto populate with type default value
........ // auto populate all properties inside User class
........ // auto populate all properties inside User class

I'm not quite sure what are you trying to achieve here我不太确定你想在这里实现什么

var user = db.User.Where(x => x.UserID).Select(o => new User
{
  UserId = o.UserId, // auto populate
  Username = o.Username, // auto populate
  ........  // auto populate all properties inside User class
}

since both new User and db.User are the same type of class/entity.因为new Userdb.User都是相同类型的类/实体。 But let's say you have an entity model User and a DTO/View model called UserViewModel and you want automatically to map the values of User to UserViewModel you can use automapper https://docs.automapper.org/en/stable/Getting-started.html Example this is the Entity definition But let's say you have an entity model User and a DTO/View model called UserViewModel and you want automatically to map the values of User to UserViewModel you can use automapper https://docs.automapper.org/en/stable/Getting-started .html示例这是实体定义

public class User 
{
   public int Id {get; set;}
  
   public string FirstName {get; set;}

   public string LastName {get; set;}
 
   public int Age {get; set;}
}

And we have a UserViewModel to which you want to map the data from User我们有一个UserViewModel你想 map 来自User的数据

  public class UserViewModel 
  {
     public int Id {get; set;}
  
     public string FirstName {get; set;}

     public string LastName {get; set;}
 
     public int Age {get; set;}
   }

With AutoMapper you can do this使用AutoMapper ,您可以做到这一点

 var configuration = new MapperConfiguration(config =>
 {
     config.CreateMap<User, UserViewModel>();
 }

and then you can use the mapper configuration like this然后你可以像这样使用映射器配置

var user = db.User.Where(x => x.UserID).First();
var userViewModel = configuration.CreateMapper().Map<UserViewModel>(user);

This will automatically populate the property values of User to UserViewModel .这将自动将User的属性值填充到UserViewModel Alternatively you can have a view model like this或者,您可以像这样查看 model

public class UserViewModel 
{
     public int Id {get; set;}
  
     public string FullName {get; set;}
 
     public int Age {get; set;}
}

This time not all properties of User and UserViewModel match one to one and you will have to set a custom mapper configuration ie这一次不是所有的UserUserViewModel的属性都是一对一匹配的,你必须设置一个自定义的映射器配置,即

var configuration = new MapperConfiguration(config =>
 {
     config.CreateMap<User, UserViewModel>()
           .ForMember(uvm => uvm.FullName, o => o.MapFrom($"{u.FirstName} {u.LastName}") );
 }

This way you are configuring the automatic User to UserViewModel mapping to map the FullName property value by concatenating FirstName and LastName from User这样,您可以通过连接UserFirstNameLastName来配置自动UserUserViewModel映射到 map FullName属性值

Well I have build a library that could solve this problem for you, called FastDeepCloner好吧,我已经建立了一个可以为你解决这个问题的库,叫做FastDeepCloner

   public class User 
    {

        public virtual string Name { get; set; } = "sdjh";

        public virtual int PasswordLength { get; set; } = 6;

        public Circular Test { get; set; } = new Circular();

    }
    
    public class CloneToTest
    {
        [FastDeepCloner.FastDeepClonerColumn("Name")] 
        [FastDeepCloner.FastDeepClonerColumn("LastName")] // Or
        public string FullName { get; set; }
        
        // You see here the type could be difrrent then the orginal type. 
        // FastDeepCloner will try to convert it, if it fail then a default value will be inserted insted
        public string PasswordLength { get; set; }
        
        // You could add a path insted, remember this only work on none list items.
        [FastDeepClonerColumn("Test.myBar.Id")]
        public int Id { get; set; }

        public Circular Test { get; set; }
    }

Now simple use现在简单使用

  var user = new User() { Name = "alen toma" };
        var cloneTo =new  CloneToTest();

        FastDeepCloner.DeepCloner.CloneTo(user, cloneTo);
  
        Assert.AreEqual(user.Name, cloneTo.FullName);

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

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