簡體   English   中英

在 class 中自動填充屬性

[英]Auto populate properties in class

假設我有一個 class 名稱用戶,是否有任何方法可以在 Visual Studio 中填充 class 屬性和默認值,如下所示。 我覺得一遍又一遍地重復輸入屬性很乏味。

在 Visual Studio 或任何可以執行此操作的擴展中可能有默認快捷方式來執行此操作?

情況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
}
);

案例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

我不太確定你想在這里實現什么

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
}

因為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示例這是實體定義

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

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

我們有一個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;}
   }

使用AutoMapper ,您可以做到這一點

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

然后你可以像這樣使用映射器配置

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

這將自動將User的屬性值填充到UserViewModel 或者,您可以像這樣查看 model

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

這一次不是所有的UserUserViewModel的屬性都是一對一匹配的,你必須設置一個自定義的映射器配置,即

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

這樣,您可以通過連接UserFirstNameLastName來配置自動UserUserViewModel映射到 map FullName屬性值

好吧,我已經建立了一個可以為你解決這個問題的庫,叫做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; }
    }

現在簡單使用

  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