‫۵ سال و ۹ ماه قبل، جمعه ۲ آذر ۱۳۹۷، ساعت ۲۰:۱۲
نکته تکمیلی
با کمی تغییرات در بدنه متد processModelStateErrors، می‌توان برای سناریوهای master-detail که خطاهایی به شکل زیر از سمت سرور دریافت می‌شود نیز پیغام متناظر به کنترل‌های موجود در FormArray‌ها را نیز به درستی نمایش داد:
{
  'detail[0].title':['message'],
  'detail[0].detail[0].title':['message']
}
برای این منظور متد findFieldControl را به شکل زیر پیاده سازی خواهیم کرد:
  private findFieldControl(fieldName: string): AbstractControl {
    const path = fieldName.replace('[', '.').replace(']', '');
    return (
      this.form.get(path) || this.form.get(this.lowerCaseFirstLetter(path))
    );
  }
متد get استفاده شده در بدنه متد، امکان دریافت path متناظر با یک کنترل را نیز دارد؛ از این جهت صرفا لازم بود کلید دریافتی از سمت سرور را به شکل زیر تبدیل کنیم:
'detail.0.title'
'detail.0.detail.0.title'
و در نهایت اعمال تغییرات به متد processModelStateErrors به شکل زیر می‌باشد:
protected handleSubmitError(response: HttpErrorResponse) {
    this.messages = [];

    if (response.status === 400) {
      // handle validation errors
      const validationDictionary = response.error;
      for (const fieldName in validationDictionary) {
        if (validationDictionary.hasOwnProperty(fieldName)) {
          const messages = validationDictionary[fieldName];
          const control = this.findFieldControl(fieldName);
          if (control) {
            // integrate into angular's validation if we have field validation
            control.setErrors({
              model: { messages: messages }
            });
          } else {
            // if we have cross field validation, then show the validation error at the top of the screen
            this.messages.push(...messages);
          }
        }
      }
    } else {
      this.messages.push('something went wrong!');
    }
  }

‫۵ سال و ۹ ماه قبل، جمعه ۲ آذر ۱۳۹۷، ساعت ۱۹:۵۶
نکته تکمیلی
برای خلوت کردن قالب‌های مرتبط با فرم‌ها برای نمایش خطاهای اعتبارسنجی و همچنین برای جلوگیری از تکرار، می‌توان کامپوننت ValidationMessage را به شکل زیر نیز توسعه داد:
import { Component, Input, OnInit } from '@angular/core';
import { AbstractControl } from '@angular/forms';

@Component({
  selector: 'validation-message',
  template: `
    <ng-container *ngIf="control.invalid && control.touched">
      {{ message }}
    </ng-container>
  `
})
export class ValidationMessageComponent implements OnInit {
  @Input() control: AbstractControl;
  @Input() fieldDisplayName: string;
  @Input() rules: { [key: string]: string };

  get message(): string {
    return this.control.hasError('required')
      ? `${this.fieldDisplayName} را وارد نمائید.`
      : this.control.hasError('pattern')
      ? `${this.fieldDisplayName} را به شکل صحیح وارد نمائید.`
      : this.control.hasError('email')
      ? `${this.fieldDisplayName} را به شکل صحیح وارد نمائید.`
      : this.control.hasError('minlength')
      ? `${this.fieldDisplayName} باید بیشتر از  ${
          this.control.errors.minlength.requiredLength
        } کاراکتر باشد.`
      : this.control.hasError('maxlength')
      ? `${this.fieldDisplayName} باید کمتر از  ${
          this.control.errors.maxlength.requiredLength
        } کاراکتر باشد.`
      : this.control.hasError('min')
      ? `${this.fieldDisplayName} باید بیشتر از  ${
          this.control.errors.min.requiredLength
        } باشد.`
      : this.control.hasError('max')
      ? `${this.fieldDisplayName} باید کمتر از  ${
          this.control.errors.max.requiredLength
        } باشد.`
      : this.hasRule()
      ? this.findRule()
      : this.control.hasError('model')
      ? `${this.control.errors.model.messages[0]}`
      : '';
  }
  constructor() {}

  private hasRule() {
    return (
      this.rules &&
      Object.keys(this.control.errors).some(ruleKey =>
        this.rules[ruleKey] ? true : false
      )
    );
  }

  private findRule(): string {
    let message = '';
    Object.keys(this.control.errors).forEach(ruleKey => {
      if (this.rules[ruleKey]) {
        message += `${this.rules[ruleKey]} `;
      }
    });

    return message;
  }

  ngOnInit(): void {}
}

این کامپوننت، کنترل مورد نظر، یک نام نمایشی برای فیلد متناظر و یک شیء تحت عنوان rules را دریافت می‌کند. در بدنه پراپرتی message، به ترتیب اولویت validator، بررسی انجام شده و پیغام مناسب بازگشت داده خواهد شد. همچنین خطاهای سمت سروری هم که با کد "model" به لیست خطاهای کنترل اضافه شده اند نیز مورد بررسی قرار گرفته شده اند. 
 در اینجا اگر لازم باشد با یکسری قواعد سفارشی هم به صورت یکپارچه با اعتبارسنج‌های پیش فرض رفتار کنیم و از یک مسیر این پیغام‌ها نمایش داده شوند، می‌توان از خصوصیت rules بهره برد. به عنوان مثال:
 <mat-error *ngIf="form.controls['userName'].invalid && form.controls['userName'].touched" 
 class="mat-text-warn">
    <validation-message
        [control]="form.controls['userName']"
        fieldDisplayName="نام کاربری"
        [rules]="{rule1:'پیغام متناظر با rule1'}">
    </validation-message>
</mat-error>

به همراه یک Validator سفارشی
this.form = this.formBuilder.group({
      userName: [
        '',
        [Validators.required, UserNameValidators.rule1)]
      ],
      password: ['', Validators.required],
      rememberMe: [false]
    });

export class UserNameValidators{
   static rule1(control: AbstractControl) {
        if (control.value.indexOf(' ') >= 0) {
            return { rule1: true };
        }
        return null;
    }
}

با توجه به اینکه برای هر درخواست رسیده به اکشن متد تزئین شده با PermissionAuthorizeAttribute لازم است تا وهله سازی جدید از AuthorizationPolicy برای policyName دریافتی صورت پذیرد، شاید بهتر باشد برای بهبود کارایی سیستم، تغییرات زیر را اعمال کرد:
 public class AuthorizationPolicyProvider : DefaultAuthorizationPolicyProvider
    {
        private readonly LazyConcurrentDictionary<string, AuthorizationPolicy> _policies =
            new LazyConcurrentDictionary<string, AuthorizationPolicy>();

        public AuthorizationPolicyProvider(IOptions<AuthorizationOptions> options)
            : base(options)
        {
        }

        public override async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
        {
           //...
        }
    }
با توجه به مطلب «الگویی برای مدیریت دسترسی همزمان به ConcurrentDictionary»، مخزنی امن برای دسترسی همزمان به لیست سیاست‌های درخواست شده قبلی تحت عنوان ‎ _policies ایجاد شده است. سپس در بدنه متد GetPolicyAsync تغییرات زیر را اعمال کنید:
        public override async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
        {
          //...

            var policy = _policies.GetOrAdd(policyName, name =>
            {
                var permissionNames = policyName.Substring(PermissionAuthorizeAttribute.PolicyPrefix.Length).Split(',');

                return new AuthorizationPolicyBuilder()
                    .RequireClaim(CustomClaimTypes.Permission, permissionNames)
                    .Build();
            });

            return policy;
        }

‫۵ سال و ۱۰ ماه قبل، چهارشنبه ۲ آبان ۱۳۹۷، ساعت ۱۷:۰۲
با توجه به اینکه امکان حدس GUID‌های تولیدی بعدی ممکن می‌باشد؛ البته مشخصا این قضیه ضعف GUID نمی‌باشد و از روز اول هم با هدف Uniqueness طراحی شده است. حداقل دو راه حل زیر را می‌توان داشت:
  • استفاده از RandomNumberGenerator برای تولید رشته  تصادفی، که در این صورت حدس زدن رشته تولیدی بعدی سخت‌تر می‌باشد ولی با این حال برای بازه زمانی محدودی امکان تولید رشته یکتا را خواهد داشت.
  • در زمان درخواست توکن جدید، access_token منقضی شده را به همراه refresh_token به سرور ارسال کرده و اطلاعات کاربر را از آن توکن منقضی شده خارج کرده و از این طریق می‌توان بررسی کرد که refresh_token ارسالی برای کاربر درخواست کننده می‌باشد.
‫۶ سال قبل، پنجشنبه ۲۲ شهریور ۱۳۹۷، ساعت ۰۴:۵۴
معادل مطلب جاری برای EF Core

برای آماده سازی دیتابیس واقعی به منظور تست جامعیت با EF Core می‌توان به شکل زیر عمل کرد:
services.AddEntityFrameworkSqlServer()
                        .AddDbContext<ProjectNameDbContext>(builder =>
                            builder.UseSqlServer(
                                $@"Data Source=(LocalDB)\MSSQLLocalDb;Initial Catalog=IntegrationTesting;Integrated Security=True;MultipleActiveResultSets=true;AttachDbFileName={FileName}"));


private static string FileName => Path.Combine(
    Path.GetDirectoryName(
        typeof(TestingHelper).GetTypeInfo().Assembly.Location),
    "IntegrationTesting.mdf");
و در نهایت برای ساخت دیتابیس قبل از اجرای تست ها، به شکل زیر می‌بایست عمل کرد:
_serviceProvider.RunScopedService<ProjectNameDbContext>(context =>
{
    context.Database.EnsureDeleted();
    context.Database.EnsureCreated();
});

‫۶ سال قبل، پنجشنبه ۲۲ شهریور ۱۳۹۷، ساعت ۰۴:۴۷
نکته تکمیلی
برای مقدار دهی خودکار فیلد مرتبط با مباحث همزمانی برای تامین کننده SQLite می توان به شکل زیر عمل کرد:
هنگام ثبت رکورد جدید
public static void SetRowVersionOnInsert(this IUnitOfWork uow, string table)
{
    uow.ExecuteSqlCommand(
        $@"
            CREATE TRIGGER Set{table}RowVersion
            AFTER INSERT ON {table}
            BEGIN
                UPDATE {table}
                SET RowVersion = randomblob(8)
                WHERE Id = NEW.Id;
            END
            ");
}

هنگام ویرایش رکورد موجود
public static void SetRowVersionOnUpdate(this IUnitOfWork uow, string table)
{
    uow.ExecuteSqlCommand(
        $@"
            CREATE TRIGGER Set{table}RowVersion
            AFTER UPDATE ON {table}
            BEGIN
                UPDATE {table}
                SET RowVersion = randomblob(8)
                WHERE Id = NEW.Id;
            END
            ");
}

و استفاده از آن در هنگام کار با داده‌های تست:
_serviceProvider.RunScopedService<IUnitOfWork>(uow =>
{
    uow.SetRowVersionOnInsert(nameof(MeasurementUnit));
    
    uow.Set<MeasurementUnit>().Add(measurementUnit1);
    uow.Set<MeasurementUnit>().Add(measurementUnit2);
    uow.Set<MeasurementUnit>().Add(measurementUnit3);
    uow.SaveChanges();
});

‫۶ سال و ۱ ماه قبل، دوشنبه ۲۹ مرداد ۱۳۹۷، ساعت ۲۰:۲۸
نکته تکمیلی
در سناریوهای منفصل در زمان حذف یا به روز رسانی یک رکورد، در صورت عدم وجود چنین رکوردی در دیتابیس و بدون توجه به اینکه برای موجودیت مورد نظر ConcurrencyToken یا RowVersion ای تنظیم شده است یا خیر، نیز استثناء DbUpdateConcurrencyException  پرتاب خواهد شد.
var student = new Student() {
    StudentId = 50
};

using (var context = new SchoolContext()) {

    context.Remove<Student>(student);

    context.SaveChanges();
}
In the above example, a Student with StudentId = 50 does not exist in the database. So, EF Core will throw the following DbUpdateConcurrencyException:
Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. 
‫۶ سال و ۱ ماه قبل، پنجشنبه ۲۵ مرداد ۱۳۹۷، ساعت ۱۶:۱۶
نیازی به استفاده از Id نیست. مسیر زیر را در نظر بگیرید:
/// Example: "00001.00042.00005".
مسیر بالا متناظر با نودی در درخت می‌باشد که در عمق 2 بوده و فرزند 5 ام مربوط به نود 00001.00042 می‌باشد. اگر نیاز باشد فرزند جدیدی به نود 00001.00042 اضافه شود، باید ابتدا مسیر آخرین فرزند آن یعنی الگوی بالایی واکشی شده و سپس مسیر جدیدی برای نود جدید به شکل زیر تشکیل شود:
/// Example: "00001.00042.00006".
دقیقا مشابه به کاری می‌باشد که نوع داده hierarchyid موجود در Sql Server انجام می‌دهد. با این روش دقیقا مشخص می‌باشد که نود x در چه مکانی قرار داد.

مدیریت واحدهای سازمانی
یکسری متد کمکی هم برای مدیریت فیلد Path در نظر گرفته شده است.
    public class OrganizationalUnit : TrackableEntity<User>, IHasRowVersion, IPassivable
    {
        #region Constants

        /// <summary>
        /// Maximum depth of an UO hierarchy.
        /// </summary>
        public const int MaxDepth = 16;

        /// <summary>
        /// Length of a code unit between dots.
        /// </summary>
        public const int PathUnitLength = 5;

        /// <summary>
        /// Maximum length of the <see cref="Path"/> property.
        /// </summary>
        public const int MaxPathLength = MaxDepth * (PathUnitLength + 1) - 1;

        public const char HierarchicalDisplayNameSeperator = '»';

        #endregion

        #region Properties

        public string Name { get; set; }
        public string NormalizedName { get; set; }
        public string HierarchicalDisplayName { get; set; }
        /// <summary>
        /// Hierarchical Path of this organization unit.
        /// Example: "00001.00042.00005".
        /// It's changeable if OU hierarch is changed.
        /// </summary>
        public string Path { get; set; }
        public bool IsActive { get; set; } = true;
        public byte[] RowVersion { get; set; }

        #endregion

        #region Navigation Properties

        public OrganizationalUnit Parent { get; set; }
        public long? ParentId { get; set; }
        public ICollection<OrganizationalUnit> Children { get; set; } = new HashSet<OrganizationalUnit>();
        public ICollection<UserOrganizationalUnit> UserOrganizationalUnits { get; set; } =
            new HashSet<UserOrganizationalUnit>();

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates path for given numbers.
        /// Example: if numbers are 4,2 then returns "00004.00002";
        /// </summary>
        /// <param name="numbers">Numbers</param>
        public static string CreatePath(params int[] numbers)
        {
            if (numbers.IsNullOrEmpty())
            {
                return null;
            }

            return numbers.Select(number => number.ToString(new string('0', PathUnitLength))).JoinAsString(".");
        }

        /// <summary>
        /// Appends a child path to a parent path. 
        /// Example: if parentPath = "00001", childPath = "00042" then returns "00001.00042".
        /// </summary>
        /// <param name="parentPath">Parent path. Can be null or empty if parent is a root.</param>
        /// <param name="childPath">Child path.</param>
        public static string AppendPath(string parentPath, string childPath)
        {
            if (childPath.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(childPath), "childPath can not be null or empty.");
            }

            if (parentPath.IsNullOrEmpty())
            {
                return childPath;
            }

            return parentPath + "." + childPath;
        }

        /// <summary>
        /// Gets relative path to the parent.
        /// Example: if path = "00019.00055.00001" and parentPath = "00019" then returns "00055.00001".
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="parentPath">The parent path.</param>
        public static string GetRelativePath(string path, string parentPath)
        {
            if (path.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(path), "Path can not be null or empty.");
            }

            if (parentPath.IsNullOrEmpty())
            {
                return path;
            }

            if (path.Length == parentPath.Length)
            {
                return null;
            }

            return path.Substring(parentPath.Length + 1);
        }

        /// <summary>
        /// Calculates next path for given path.
        /// Example: if code = "00019.00055.00001" returns "00019.00055.00002".
        /// </summary>
        /// <param name="path">The path.</param>
        public static string CalculateNextPath(string path)
        {
            if (path.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(path), "Path can not be null or empty.");
            }

            var parentPath = GetParentPath(path);
            var lastUnitPath = GetLastUnitPath(path);

            return AppendPath(parentPath, CreatePath(Convert.ToInt32(lastUnitPath) + 1));
        }

        /// <summary>
        /// Gets the last unit path.
        /// Example: if path = "00019.00055.00001" returns "00001".
        /// </summary>
        /// <param name="path">The path.</param>
        public static string GetLastUnitPath(string path)
        {
            if (path.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(path), "Path can not be null or empty.");
            }

            var splittedPath = path.Split('.');
            return splittedPath[splittedPath.Length - 1];
        }

        /// <summary>
        /// Gets parent path.
        /// Example: if path = "00019.00055.00001" returns "00019.00055".
        /// </summary>
        /// <param name="path">The path.</param>
        public static string GetParentPath(string path)
        {
            if (path.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(path), "Path can not be null or empty.");
            }

            var splittedPath = path.Split('.');
            if (splittedPath.Length == 1)
            {
                return null;
            }

            return splittedPath.Take(splittedPath.Length - 1).JoinAsString(".");
        }

        #endregion
    }

البته یک ویو نمایشی برای حالت درختی هم بهتر است داشته باشید.


یکسری متد DomainService

       public virtual async Task<string> GetNextChildPathAsync(long? parentId)
        {
            var lastChild = await GetLastChildOrNullAsync(parentId).ConfigureAwait(false);
            if (lastChild == null)
            {
                var parentPath = parentId != null ? await GetPathAsync(parentId.Value).ConfigureAwait(false) : null;
                return OrganizationalUnit.AppendPath(parentPath, OrganizationalUnit.CreatePath(1));
            }

            return OrganizationalUnit.CalculateNextPath(lastChild.Path);
        }

        public async Task<string> GetNextChildHierarchicalDisplayNameAsync(string name, long? parentId)
        {
            var parent = parentId != null
                ? await _organizationalUnits.SingleOrDefaultAsync(a => a.Id == parentId.Value).ConfigureAwait(false)
                : null;

            return parent == null
                ? name
                : $"{parent.HierarchicalDisplayName} {OrganizationalUnit.HierarchicalDisplayNameSeperator} {name}";
        }

        public virtual async Task<OrganizationalUnit> GetLastChildOrNullAsync(long? parentId)
        {
            return await _organizationalUnits.OrderByDescending(c => c.Path)
                .FirstOrDefaultAsync(ou => ou.ParentId == parentId).ConfigureAwait(false);
        }

        public virtual async Task<string> GetPathAsync(long id)
        {
            Guard.ArgumentNotZero(id, nameof(id));
            var organizationalUnit = await _organizationalUnits.SingleOrDefaultAsync(ou => ou.Id == id).ConfigureAwait(false);
            if (organizationalUnit == null)
            {
                throw new KeyNotFoundException();
            }
            return organizationalUnit.Path;
        }

        public async Task<List<OrganizationalUnit>> FindChildrenAsync(long? parentId, bool recursive = false)
        {
            if (!recursive)
            {
                return await _organizationalUnits.Where(ou => ou.ParentId == parentId).ToListAsync().ConfigureAwait(false);
            }

            if (!parentId.HasValue)
            {
                return await _organizationalUnits.ToListAsync().ConfigureAwait(false);
            }

            var path = await GetPathAsync(parentId.Value).ConfigureAwait(false);

            return await _organizationalUnits.Where(
                ou => ou.Path.StartsWith(path) && ou.Id != parentId.Value).ToListAsync().ConfigureAwait(false);
        }

        public virtual async Task MoveAsync(long id, long? parentId)
        {
            Guard.ArgumentNotZero(id, nameof(id));
            var organizationalUnit = await _organizationalUnits.SingleOrDefaultAsync(ou => ou.Id == id).ConfigureAwait(false);
            if (organizationalUnit == null || organizationalUnit.ParentId == parentId)
            {
                return;
            }

            //Should find children before Path change
            var children = await FindChildrenAsync(id, true).ConfigureAwait(false);

            //Store old Path of OU
            var oldPath = organizationalUnit.Path;

            //Move OU
            organizationalUnit.Path = await GetNextChildPathAsync(parentId).ConfigureAwait(false);
            organizationalUnit.ParentId = parentId;

            //Update Children Paths
            foreach (var child in children)
            {
                child.Path = OrganizationalUnit.AppendPath(organizationalUnit.Path, OrganizationalUnit.GetRelativePath(child.Path, oldPath));
            }
        }



‫۶ سال و ۱ ماه قبل، پنجشنبه ۱۸ مرداد ۱۳۹۷، ساعت ۱۹:۵۰
مطلب تکمیلی جدیدی در راستای بهبود بحث مطرح شده در بخش آخر مطلب جاری، تهیه شده است.
بعد از انتشار مطلب «Defensive Programming - بازگشت نتایج قابل پیش بینی توسط متدها»، بخصوص بخش نظرات آن و همچنین R&D در ارتباط با موضوع مورد بحث، در نهایت قصد دارم نتایج بدست آماده را به اشتراک بگذارم.