سؤال

I have question about how to inject value with no source property defined. so I have 2 class :

User.cs

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
}

and UserViewModel.cs

public class UserViewModel
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string RoleName { get; set; }
}

how to inject the RoleName? I need to call my service to check to database what user's role. and I dont wanna the service called in get property like this :

public string RoleName
    {
        get { return Roles.GetRolesForUser(UserName).FirstOrDefault(); }
    }

I want to achieve this using value injecter. Thank you in advance

هل كانت مفيدة؟

المحلول

if you really really want to do this with a valueinjection you could do like this:

public class SetRole : NoSourceValueInjection
     {
         protected override void Inject(object target)
         {
             dynamic t = target;
             t.RoleName = Roles.GetRolesForUser(t.UserName).FirstOrDefault();
         }
     }

uvm.InjectFrom(user)
   .InjectFrom<SetRole>();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top