Open Closed

How to extend a RegisterDto entity that does not inherit ihasextraproperties!!! #901


User avatar
0
lataing created
  • ABP Framework version: v4.2.0
  • UI type: MVC

How do I want to add attributes to these two entities?

Volo.Abp.Account.RegisterDto Volo.Abp.Account.Public.Web.Pages.Account.RegisterModel.PostInput

The following method cannot be used. ObjectExtensionManager.Instance.AddOrUpdateProperty<RegisterDto, string>("Title");


14 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    you can add extra property to the current Tenant object. Open YourProjectModuleExtensionConfigurator.cs and replace the ConfigureExtraProperties() method with below:

        private static void ConfigureExtraProperties()
        {
            OneTimeRunner.Run(() =>
            {
                ObjectExtensionManager.Instance.Modules()
                    .ConfigureSaas(x =>
                    {
                        x.ConfigureTenant(tenant =>
                        {
                            tenant.AddOrUpdateProperty<string>(
                                "MyExtraProperty",
                                property =>
                                {
                                    //validation rules
                                    property.Attributes.Add(new RequiredAttribute());
                                    property.Attributes.Add(
                                        new StringLengthAttribute(64)
                                        {
                                            MinimumLength = 4
                                        }
                                    );
    
                                    //...other configurations for this property
                                }
                            );
                        });
                    });
            });
        }
    

    Ref: https://docs.abp.io/en/abp/latest/Module-Entity-Extensions

  • User Avatar
    0
    lataing created

    you can add extra property to the current Tenant object. Open YourProjectModuleExtensionConfigurator.cs and replace the ConfigureExtraProperties() method with below:

        private static void ConfigureExtraProperties() 
        { 
            OneTimeRunner.Run(() => 
            { 
                ObjectExtensionManager.Instance.Modules() 
                    .ConfigureSaas(x => 
                    { 
                        x.ConfigureTenant(tenant => 
                        { 
                            tenant.AddOrUpdateProperty<string>( 
                                "MyExtraProperty", 
                                property => 
                                { 
                                    //validation rules 
                                    property.Attributes.Add(new RequiredAttribute()); 
                                    property.Attributes.Add( 
                                        new StringLengthAttribute(64) 
                                        { 
                                            MinimumLength = 4 
                                        } 
                                    ); 
     
                                    //...other configurations for this property 
                                } 
                            ); 
                        }); 
                    }); 
            }); 
        } 
    

    Ref: https://docs.abp.io/en/abp/latest/Module-Entity-Extensions

    I still don't understand. I need to add properties to registerdto. Why configure tent object?

  • User Avatar
    0
    alper created
    Support Team Director

    because it will also be shown on the register page and I guess you want that.

  • User Avatar
    0
    lataing created

    Can you give me an example of how to access this new property? I don't understand. thank you.

    I've read the document carefully for many times, and I don't know how to access it so that it can be displayed on the UI, and set and get values.

    https://docs.abp.io/en/abp/latest/Module-Entity-Extensions

    I have written how to add a new property, but I don't know how to set the referenced property.

    How do I set and get it? registerDto.SetProperty("PhoneNumber", "0123456789"); Methods like this are not available. Can you give me some examples?

    https://docs.abp.io/en/abp/latest/Module-Entity-Extensions#create-update-forms https://docs.abp.io/en/abp/latest/Module-Entity-Extensions#data-table

  • User Avatar
    0
    lataing created

    Can someone help me with this problem? thank you.

  • User Avatar
    0
    alper created
    Support Team Director

    this is how you can get and set an extra property

    //SET AN EXTRA PROPERTY
    var user = await _identityUserRepository.GetAsync(userId);
    user.SetProperty("Title", "My custom title value!");
    await _identityUserRepository.UpdateAsync(user);
    
    //GET AN EXTRA PROPERTY
    var user = await _identityUserRepository.GetAsync(userId);
    return user.GetProperty<string>("Title");
    
    

    see https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities

  • User Avatar
    0
    lataing created

    My registerdto doesn't inherit the ihasextrapropertie interface and doesn't contain the setproperty method. When I call it, I report an error. Don't I need to set anything?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi @lataing

    You are right, The RegisterDto doesn't inherit ExtensibleEntityDto. I will handle it in next version.

    You can create a new RegisterAsync method to temporarily solve the problem.

    Volo.Abp.Account.Public.Web.Pages.Account.RegisterModel.PostInput

    Please refer to https://docs.abp.io/en/abp/latest/UI/AspNetCore/Customization-User-Interface

  • User Avatar
    0
    lataing created

    Hello, @maliming. I see that every module has an extension method like this.

    The attribute is extended as follows, but I don't know how to set the value for the SocialSecurityNumber attribute or how to get the value.

    No unit test, case or document was found. Can you explain it for me?

    Another problem is that if I can't add properties to RegisterDto, I need to rewrite AccountAppService instead of just the RegisterAsync method.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    https://docs.abp.io/en/abp/latest/Object-Extensions https://docs.abp.io/en/abp/latest/Module-Entity-Extensions

    Another problem is that if I can't add properties to RegisterDto, I need to rewrite AccountAppService instead of just the RegisterAsync method.

    You can add new method and new Dto.

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

  • User Avatar
    0
    rahul.patel@parsus.com created

    Hi,

    I'm bringing this one back up because after going through ABP's extensive Extra Properties feature, I still don't see how we can use it to map some obvious properties like Name and Surname during registration without modifying module code. In provided AccountAppService.RegisterAsync the extra properties are mapped like this:

                input.MapExtraPropertiesTo(user);
    

    So even after adding Name and Surname as extra properties to RegisterDto those properties will not get mapped to the IdentityUser object.

    Only option to avoid modifying source code seems to be to override the AccountAppService by calling the base method, map add'l properties after create, and do a follow up UserManager.UpdateAsync.

            public override async Task<IdentityUserDto> RegisterAsync(RegisterDto input)
            {
                var baseResult = await base.RegisterAsync(input);
    
                // Handle extra properties
                var user = await UserManager.GetByIdAsync(baseResult.Id);
                user.Surname = input.GetSurname();
                user.Name = input.GetName();
    
                await UserManager.UpdateAsync(user);
    
                var result = ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
                return result;
            }
    

    It would be nice if there was a way to make MapExtraPropertiesTo extension method also map to regular properties. After checking the source, it seems that this is only possible using AutoMapper MapExtraProperties which would still require modifying module source code.

    Thoughts?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi @rahul.patel

    Please create a new question. Thanks.

  • User Avatar
    0
    alper created
    Support Team Director

    MapExtraPropertiesTo is being used only for custom fields. see the usage

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11