Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Create and Update Logic in Module Zero #2619


User avatar
0
bilalhaidar created

hi,

I am checking the code for Module Zero to follow your best practices in writing new custom code.

I noticed 2 ways of writing Create or Update code. I know both work great, but is there a preference to which one to use?

Thanks

First way:

public virtual async Task<IdentityResult> CreateAsync(TTenant tenant)
        {
            if (await TenantRepository.FirstOrDefaultAsync(t => t.TenancyName == tenant.TenancyName) != null)
            {
                return AbpIdentityResult.Failed(string.Format(L("TenancyNameIsAlreadyTaken"), tenant.TenancyName));
            }

            var validationResult = await ValidateTenantAsync(tenant);
            if (!validationResult.Succeeded)
            {
                return validationResult;
            }

            await TenantRepository.InsertAsync(tenant);
            return IdentityResult.Success;
        }

Second way:

[UnitOfWork]
        public virtual async Task AddAsync(ApplicationLanguage language)
        {
            if ((await GetLanguagesAsync(language.TenantId)).Any(l => l.Name == language.Name))
            {
                throw new AbpException("There is already a language with name = " + language.Name);
            }

            using (_unitOfWorkManager.Current.SetTenantId(language.TenantId))
            {
                await _languageRepository.InsertAsync(language);
                await _unitOfWorkManager.Current.SaveChangesAsync();
            }
        }

5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The main idea is, don't call SaveChanges on unitofwork if you are operating on a single entity or you don't need the id of a created entity.

    Otherwise all changes will be commited at the end of your method (for UnitofWork methods of course).

  • User Avatar
    0
    bilalhaidar created

    Thank you.

    But I meant that one method returns Task<IdentityResult> and the other results Task.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    This is not related to create/update logic. One method returns a value other does not. Maybe I didn't understand your question.

  • User Avatar
    0
    velu created

    Hi, Please provide sample program of Create and Update / Edit angluerjs Logic.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The downloaded AspNet Zero project has many Create, Update samples in it. User and Role pages for example. Did you have a problem when implementing it ? If so, please share your code and we will try to help.

    Thanks.