نظرات مطالب
ایجاد سرویس چندلایه‎ی WCF با Entity Framework در قالب پروژه - 3

بایدی وجود ندارد در این حالت و بهتر است که اینگونه باشد یا حتی مخلوطی از این دو در عمل:

Pros and Cons of Data Transfer Objects

In large projects with so many entities, DTOs add a remarkable level of (extra) complexity and work to do. In short, a pure, 100% DTO solution is often just a 100 percent painful solution

اشتراک‌ها
پشتیبانی Brotli در فایرفاکس 44
 recently, new formats struggled with matching the decoding rates of gzip, but lately a new contender named brotli has shown impressive results. It has been able to improve on gzip anywhere from 20% to 40% in terms of compression ratios while keeping up on the decoding rate. 
پشتیبانی Brotli در فایرفاکس 44
اشتراک‌ها
برنامه نویس 40 ساله

I think you might want to be a software developer for a long time, in the same way that some people are musicians for a long time, or artists for a long time, or roofers for a long time. If not, you can hit “back” in your browser. It’s cool, no harm no foul. But I think maybe you would like to be a twenty-year programmer, or forty-year, or more. 

برنامه نویس 40 ساله
مطالب
طراحی و پیاده سازی زیرساختی برای مدیریت خطاهای حاصل از Business Rule Validationها در ServiceLayer
بعد از انتشار مطلب «Defensive Programming - بازگشت نتایج قابل پیش بینی توسط متدها»، بخصوص بخش نظرات آن و همچنین R&D در ارتباط با موضوع مورد بحث، در نهایت قصد دارم نتایج بدست آماده را به اشتراک بگذارم.

پیش نیازها
در بخش نهایی مطلب «Defensive Programming - بازگشت نتایج قابل پیش بینی توسط متدها » پیشنهادی را برای استفاده از استثناءها برای bubble up کردن یکسری پیغام از داخلی‌ترین یا پایین‌ترین لایه، تا لایه Presentation، ارائه دادیم:
استفاده از Exception برای نمایش پیغام برای کاربر نهایی 
با صدور یک استثناء و مدیریت سراسری آن در بالاترین (خارجی ترین) لایه و نمایش پیغام مرتبط با آن به کاربر نهایی، می‌توان از آن به عنوان ابزاری برای ارسال هر نوع پیغامی به کاربر نهایی استفاده کرد. اگر قوانین تجاری با موفقیت برآورده نشده‌اند یا لازم است به هر دلیلی یک پیغام مرتبط با یک اعتبارسنجی تجاری را برای کاربر نمایش دهید، این روش بسیار کارساز می‌باشد و با یکبار وقت گذاشتن برای توسعه زیرساخت برای این موضوع، به عنوان یک Cross Cutting Concern تحت عنوان Exception Management، آزادی عمل زیادی در ادامه توسعه سیستم خود خواهید داشت. 

اگر مطالب پیش نیاز را مطالعه کنید، قطعا روش مطرح شده را انتخاب نخواهید کرد؛ به همین دلیل به دنبال راه حل صحیح برخورد با این سناریوها بودم که نتیجه آن را در ادامه خواهیم دید.

راه حل صحیح برای برخورد با این سناریوها بازگشت یک Result می‌باشد که در مطلب قبلی هم تحت عنوان OperationResult مطرح شد. 


کلاس Result
    public class Result
    {
        private static readonly Result SuccessResult = new Result(true, null);

        protected Result(bool succeeded, string message)
        {
            if (succeeded)
            {
                if (message != null)
                    throw new ArgumentException("There should be no error message for success.", nameof(message));
            }
            else
            {
                if (message == null)
                    throw new ArgumentNullException(nameof(message), "There must be error message for failure.");
            }

            Succeeded = succeeded;
            Error = message;
        }

        public bool Succeeded { get; }
        public string Error { get; }

        [DebuggerStepThrough]
        public static Result Success()
        {
            return SuccessResult;
        }

        [DebuggerStepThrough]
        public static Result Failed(string message)
        {
            return new Result(false, message);
        }

        [DebuggerStepThrough]
        public static Result<T> Failed<T>(string message)
        {
            return new Result<T>(default, false, message);
        }

        [DebuggerStepThrough]
        public static Result<T> Success<T>(T value)
        {
            return new Result<T>(value, true, string.Empty);
        }

        [DebuggerStepThrough]
        public static Result Combine(string seperator, params Result[] results)
        {
            var failedResults = results.Where(x => !x.Succeeded).ToList();

            if (!failedResults.Any())
                return Success();

            var error = string.Join(seperator, failedResults.Select(x => x.Error).ToArray());
            return Failed(error);
        }

        [DebuggerStepThrough]
        public static Result Combine(params Result[] results)
        {
            return Combine(", ", results);
        }

        [DebuggerStepThrough]
        public static Result Combine<T>(params Result<T>[] results)
        {
            return Combine(", ", results);
        }

        [DebuggerStepThrough]
        public static Result Combine<T>(string seperator, params Result<T>[] results)
        {
            var untyped = results.Select(result => (Result) result).ToArray();
            return Combine(seperator, untyped);
        }

        public override string ToString()
        {
            return Succeeded
                ? "Succeeded"
                : $"Failed : {Error}";
        }
    }

مشابه کلاس بالا، در فریمورک ASP.NET Identity کلاسی تحت عنوان IdentityResult برای همین منظور در نظر گرفته شده‌است.

پراپرتی Succeeded نشان دهنده موفقت آمیز بودن یا عدم موفقیت عملیات (به عنوان مثال یک متد ApplicationService) می‌باشد. پراپرتی Error دربرگیرنده پیغام خطایی می‌باشد که قبلا از طریق Message مربوط به یک استثناء صادر شده، در اختیار بالاترین لایه قرار می‌گرفت. با استفاده از متد Combine، امکان ترکیب چندین Result حاصل از عملیات مختلف را خواهید داشت. متدهای استاتیک Failed و Success هم برای درگیر نشدن برای وهله سازی از کلاس Result در نظر گرفته شده‌اند.

متد GetForEdit مربوط به MeetingService را در نظر بگیرید. به عنوان مثال وظیفه این متد بازگشت یک MeetingEditModel می‌باشد؛ اما با توجه به یکسری قواعد تجاری، به‌عنوان مثال «امکان ویرایش جلسه‌ای که پابلیش نهایی شده‌است، وجود ندارد و ...» لازم است خروجی این متد نیز در صورت Fail شدن، دلیل آن را به مصرف کننده ارائه دهد. از این رو کلاس جنریک Result را به شکل زیر خواهیم داشت:

    public class Result<T> : Result
    {
        private readonly T _value;

        protected internal Result(T value, bool succeeded, string error)
            : base(succeeded, error)
        {
            _value = value;
        }

        public T Value
        {
            get
            {
                if (!Succeeded)
                    throw new InvalidOperationException("There is no value for failure.");

                return _value;
            }
        }
    }
حال با استفاده از کلاس بالا امکان مهیا کردن خروجی به همراه نتیجه اجرای متد را خواهیم داشت.
در ادامه با استفاده از تعدادی متد الحاقی بر فراز کلاس Result، روش Railway-oriented Programming را که یکی از روش‌های برنامه نویسی تابعی برای مدیریت خطاها است، در سی شارپ اعمال خواهیم کرد. 
    public static class ResultExtensions
    {
        public static Result<TK> OnSuccess<T, TK>(this Result<T> result, Func<T, TK> func)
        {
            return !result.Succeeded ? Result.Failed<TK>(result.Error) : Result.Success(func(result.Value));
        }

        public static Result<T> Ensure<T>(this Result<T> result, Func<T, bool> predicate, string message)
        {
            if (!result.Succeeded)
                return Result.Failed<T>(result.Error);

            return !predicate(result.Value) ? Result.Failed<T>(message) : Result.Success(result.Value);
        }

        public static Result<TK> Map<T, TK>(this Result<T> result, Func<T, TK> func)
        {
            return !result.Succeeded ? Result.Failed<TK>(result.Error) : Result.Success(func(result.Value));
        }

        public static Result<T> OnSuccess<T>(this Result<T> result, Action<T> action)
        {
            if (result.Succeeded) action(result.Value);

            return result;
        }

        public static T OnBoth<T>(this Result result, Func<Result, T> func)
        {
            return func(result);
        }

        public static Result OnSuccess(this Result result, Action action)
        {
            if (result.Succeeded) action();

            return result;
        }

        public static Result<T> OnSuccess<T>(this Result result, Func<T> func)
        {
            return !result.Succeeded ? Result.Failed<T>(result.Error) : Result.Success(func());
        }

        public static Result<TK> OnSuccess<T, TK>(this Result<T> result, Func<T, Result<TK>> func)
        {
            return !result.Succeeded ? Result.Failed<TK>(result.Error) : func(result.Value);
        }

        public static Result<T> OnSuccess<T>(this Result result, Func<Result<T>> func)
        {
            return !result.Succeeded ? Result.Failed<T>(result.Error) : func();
        }

        public static Result<TK> OnSuccess<T, TK>(this Result<T> result, Func<Result<TK>> func)
        {
            return !result.Succeeded ? Result.Failed<TK>(result.Error) : func();
        }

        public static Result OnSuccess<T>(this Result<T> result, Func<T, Result> func)
        {
            return !result.Succeeded ? Result.Failed(result.Error) : func(result.Value);
        }

        public static Result OnSuccess(this Result result, Func<Result> func)
        {
            return !result.Succeeded ? result : func();
        }

        public static Result Ensure(this Result result, Func<bool> predicate, string message)
        {
            if (!result.Succeeded)
                return Result.Failed(result.Error);

            return !predicate() ? Result.Failed(message) : Result.Success();
        }

        public static Result<T> Map<T>(this Result result, Func<T> func)
        {
            return !result.Succeeded ? Result.Failed<T>(result.Error) : Result.Success(func());
        }


        public static TK OnBoth<T, TK>(this Result<T> result, Func<Result<T>, TK> func)
        {
            return func(result);
        }

        public static Result<T> OnFailure<T>(this Result<T> result, Action action)
        {
            if (!result.Succeeded) action();

            return result;
        }

        public static Result OnFailure(this Result result, Action action)
        {
            if (!result.Succeeded) action();

            return result;
        }

        public static Result<T> OnFailure<T>(this Result<T> result, Action<string> action)
        {
            if (!result.Succeeded) action(result.Error);

            return result;
        }

        public static Result OnFailure(this Result result, Action<string> action)
        {
            if (!result.Succeeded) action(result.Error);

            return result;
        }
    }
OnSuccess برای انجام عملیاتی در صورت موفقیت آمیز بودن نتیجه یک متد، OnFailed برای انجام عملیاتی در صورت عدم موفقت آمیز بودن نتیجه یک متد و OnBoth در هر صورت، عملیات مورد نظر شما را اجرا خواهد کرد. به عنوان مثال:
[HttpPost, AjaxOnly, ValidateAntiForgeryToken, ValidateModelState]
public virtual async Task<ActionResult> Create([Bind(Prefix = "Model")]MeetingCreateModel model)
{
    var result = await _service.CreateAsync(model);

    return result.OnSuccess(() => { })
                 .OnFailure(() => { })
                 .OnBoth(r => r.Succeeded ? InformationNotification("Messages.Save.Success") : ErrorMessage(r.Error));

}

یا در حالت‌های پیچیده تر:

var result = await _service.CreateAsync(new TenantAwareEntityCreateModel());

return Result.Combine(result, Result.Success(), Result.Failed("نتیجه یک متد دیگر به عنوان مثال"))
    .OnSuccess(() => { })
    .OnFailure(() => { })
    .OnBoth(r => r.Succeeded ? Json("OK") : Json(r.Error));


ترکیب با الگوی Maybe یا Option

قبلا مطلبی در رابطه با الگوی Maybe در سایت منتشر شده‌است. در نظرات آن مطلب، یک پیاده سازی به شکل زیر مطرح کردیم:
    public struct Maybe<T> : IEquatable<Maybe<T>>
        where T : class
    {
        private readonly T _value;

        private Maybe(T value)
        {
            _value = value;
        }

        public bool HasValue => _value != null;
        public T Value => _value ?? throw new InvalidOperationException();
        public static Maybe<T> None => new Maybe<T>();


        public static implicit operator Maybe<T>(T value)
        {
            return new Maybe<T>(value);
        }

        public static bool operator ==(Maybe<T> maybe, T value)
        {
            return maybe.HasValue && maybe.Value.Equals(value);
        }

        public static bool operator !=(Maybe<T> maybe, T value)
        {
            return !(maybe == value);
        }

        public static bool operator ==(Maybe<T> left, Maybe<T> right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(Maybe<T> left, Maybe<T> right)
        {
            return !(left == right);
        }

        /// <inheritdoc />
        /// <summary>
        ///     Avoid boxing and Give type safety
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Maybe<T> other)
        {
            if (!HasValue && !other.HasValue)
                return true;

            if (!HasValue || !other.HasValue)
                return false;

            return _value.Equals(other.Value);
        }

        /// <summary>
        ///     Avoid reflection
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj is T typed)
            {
                obj = new Maybe<T>(typed);
            }

            if (!(obj is Maybe<T> other)) return false;

            return Equals(other);
        }

        /// <summary>
        ///     Good practice when overriding Equals method.
        ///     If x.Equals(y) then we must have x.GetHashCode()==y.GetHashCode()
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return HasValue ? _value.GetHashCode() : 0;
        }

        public override string ToString()
        {
            return HasValue ? _value.ToString() : "NO VALUE";
        }
    }

متد الحاقی زیر را در نظر بگیرید:
public static Result<T> ToResult<T>(this Maybe<T> maybe, string message)
    where T : class
{
    return !maybe.HasValue ? Result.Failed<T>(message) : Result.Success(maybe.Value);
}

فرض کنید خروجی متدی که در لایه سرویس مورد استفاده قرار می‌گیرد، Maybe باشد. در این حالت می‌توان با متد الحاقی بالا آن را به یک Result تبدیل کرد و در اختیار لایه بالاتر قرار داد. 
Result<Customer> customerResult = _customerRepository.GetById(model.Id)
    .ToResult("Customer with such Id is not found: " + model.Id);

همچنین متدهای الحاقی زیر را نیز برای ساختار داده Maybe می‌توان در نظر گرفت:

        public static T GetValueOrDefault<T>(this Maybe<T> maybe, T defaultValue = default)
            where T : class
        {
            return maybe.GetValueOrDefault(x => x, defaultValue);
        }

        public static TK GetValueOrDefault<T, TK>(this Maybe<T> maybe, Func<T, TK> selector, TK defaultValue = default)
            where T : class
        {
            return maybe.HasValue ? selector(maybe.Value) : defaultValue;
        }

        public static Maybe<T> Where<T>(this Maybe<T> maybe, Func<T, bool> predicate)
            where T : class
        {
            if (!maybe.HasValue)
                return default(T);

            return predicate(maybe.Value) ? maybe : default(T);
        }

        public static Maybe<TK> Select<T, TK>(this Maybe<T> maybe, Func<T, TK> selector)
            where T : class
            where TK : class
        {
            return !maybe.HasValue ? default : selector(maybe.Value);
        }

        public static Maybe<TK> Select<T, TK>(this Maybe<T> maybe, Func<T, Maybe<TK>> selector)
            where T : class
            where TK : class
        {
            return !maybe.HasValue ? default(TK) : selector(maybe.Value);
        }

        public static void Execute<T>(this Maybe<T> maybe, Action<T> action)
            where T : class
        {
            if (!maybe.HasValue)
                return;

            action(maybe.Value);
        }
    }

پیشنهادات
  • استفاده از الگوی Specification برای زمانیکه منطقی قرار است هم برای اعتبارسنجی درون حافظه‌ای استفاده شود و همچنین برای اعمال فیلتر برای واکشی داده‌ها؛ در واقع دو Use-case استفاده از این الگو حداقل یکجا وجود داشته باشد. استفاده از این مورد برای Domain Validation در سناریوهای پیچیده بسیار پیشنهاد می‌شود.
  • استفاده از Domain Eventها برای اعمال اعتبارسنجی‌های مرتبط با قواعد تجاری تنها در شرایط inter-application communication و در شرایط inner-application communication به صورت صریح، اعتبارسنجی‌های مرتبط با قواعد تجاری را در جریان اصلی برنامه پیاده سازی کنید. 

با تشکر از آقای «محسن خان»
اشتراک‌ها
24.Visual Studio 2017 15.9 منتشر شد

Issues Fixed in 15.9.24

  • Fixed a bug in the C++ linker missing imports when using umbrella LIBs with difference casing on postfix of DLL name.
  • Fixed a bug in the ARM64 C++ compiler where the wrong values could be restored after setjmp.
  • Fixed C++ compiler bug for proper folding of inline variable dynamic initializers.
  • Made a change that enables Enterprise IT administrators and deployment engineers to configure tools like Microsoft Update client & SCCM to determine applicability of VS2017 updates hosted on Microsoft Update Catalog & WSUS.

Security Advisory Notices

24.Visual Studio 2017 15.9 منتشر شد
مطالب
آموزش Linq - بخش ششم : عملگرهای پرس و جو قسمت دوم
در ادامه‌ی سری آموزشی LINQ، عملگر‌های پرس و جوی مرتب سازی، گروه بندی و مجموعه را بررسی خواهیم کرد.

عملگرهای مرتب سازی  Ordering Operators
این عملگر‌ها عناصر توالی ورودی را به خروجی ارسال می‌کنند؛ با این تفاوت که توالی خروجی مرتب شده، توالی ورودی است.

عملگر OrderBy

این عملگر توالی ورودی را بر اساس کلیدی که مشخص می‌کنیم مرتب می‌کند.
مثال:
در این مثال کلید معرفی شده‌ی توسط عبارت Lambda، یک رشته است.
string[] ingredients = { "Sugar", "Egg", "Milk", "Flour", "Butter" };
var query = ingredients.OrderBy(x => x);
foreach (var item in query)
{
   Console.WriteLine(item);
}
خروجی مثال بالا:
 Butter
Egg
Flour
Milk
Sugar
همان طور که ملاحظه می‌کنید، عملگر OrderBy به‌صورت پیش فرض مرتب سازی عناصر را صعودی انجام داده است.
عبارت Lambda استفاده شده می‌تواند یک خاصیت از عناصر تشکیل دهنده‌ی توالی ورودی باشد.
مثال:
Ingredient[] ingredients =
{
   new Ingredient {Name = "Sugar", Calories = 500},
   new Ingredient {Name = "Egg", Calories = 100},
   new Ingredient {Name = "Milk", Calories = 150},
   new Ingredient {Name = "Flour", Calories = 50},
   new Ingredient {Name = "Butter", Calories = 200},
};
var query = ingredients.OrderBy(x => x.Calories);
foreach (var item in query)
{
   Console.WriteLine(item.Name + " " + item.Calories);
}
خروجی مثال بالا:
Flour 50
Egg 100
Milk 150
Butter 200
Sugar 500
همانطور که مشاهده می‌کنید توالی خروجی، بر اساس خصوصیت کالری توالی ورودی مرتب شده و در خروجی نمایش داده شده است.

پیاده سازی توسط عبارت‌های جستجو
در عبارت‌های پرس و جو، کلمه کلیدی orderby برای مرتب سازی توالی استفاده می‌شود:
مثال:
var query = from i in ingredients
orderby i.Calories
select i;

عملگر ThenBy

این عملگر می‌تواند به صورت زنجیره‌ای، یک یا چندین بار بعد از عملگر OrderBy در پرس و جو، مورد استفاده قرار گیرد. توسط عملگر ThenBy می‌توان سطوح دیگری از مرتب سازی را اعمال کرد. در مثال زیر ابتدا توالی ورودی را بر اساس کالری و بعد از آن بر اساس نام مرتب خواهیم کرد.
مثال :
Ingredient[] ingredients =
{
   new Ingredient {Name = "Sugar", Calories = 500},
   new Ingredient {Name = "Milk", Calories = 100},
   new Ingredient {Name = "Egg", Calories = 100},
   new Ingredient {Name = "Flour", Calories = 500},
   new Ingredient {Name = "Butter", Calories = 200},
};
var query = ingredients
                     .OrderBy(x => x.Calories)
                     .ThenBy(x => x.Name);
foreach (var item in query)
{
   Console.WriteLine(item.Name + " " + item.Calories);
}
خروجی مثال بالا:
Egg 100
Milk 100
Butter 200
Flour 500
Sugar 500
در اینجا در توالی ورودی، Milk قبل از Egg قرار دارد. ولی به خاطر استفاده از عملگر ThenBy، در مواردی که کالری عناصر یکسان است، بر اساس نام عناصر توالی، مرتب سازی انجام شده است.

پیاده سازی توسط عبارت‌های جستجو
var query = from i in ingredients
orderby i.Calories, i.Name
select i;
همانطور که مشاهده می‌کنید برای مرتب سازی بر اساس خصوصیات دیگر کافیست نام خصوصیت را بعد از اولین عنصر، به وسیله‌ی کاما (,) قید کنید.

عملگر OrderByDescending
این عملگر همچون عملگر OrderBy عمل می‌کند؛ اما توالی ورودی را بر اساس کلید داده شده، به صورت نزولی مرتب می‌کند.
مثال:
Ingredient[] ingredients =
{
   new Ingredient {Name = "Sugar", Calories = 500},
   new Ingredient {Name = "Egg", Calories = 100},
   new Ingredient {Name = "Milk", Calories = 150},
   new Ingredient {Name = "Flour", Calories = 50},
   new Ingredient {Name = "Butter", Calories = 200},
};
var query = ingredients.OrderByDescending(x => x.Calories);
foreach (var item in query)
{
   Console.WriteLine(item.Name + " " + item.Calories);
}
خروجی مثال بالا:
Sugar 500
Flour 500
Butter 200
Milk 100
Egg 100

پیاده سازی توسط عبارت‌های جستجو

جایگزین عملگر OrderByDescending در عبارت‌های جستجو، کلمه‌ی کلیدی descending می‌باشد:
var query = from i in ingredients
orderby i.Calories descending
select i;

عملگر ThenByDescending

این عملگر بصورت زنجیره‌ای بعد از عملگر‌های دیگر می‌آید و مرتب سازی را به‌صورت نزولی انجام می‌دهد.
مثال:
Ingredient[] ingredients =
{
   new Ingredient {Name = "Flour", Calories = 500},
   new Ingredient {Name = "Sugar", Calories = 500},
   new Ingredient {Name = "Egg", Calories = 100},
   new Ingredient {Name = "Milk", Calories = 100},
   new Ingredient {Name = "Butter", Calories = 200}
};
var query = ingredients
                    .OrderBy(x => x.Calories)
                    .ThenByDescending(x => x.Name);
foreach (var item in query)
{
   Console.WriteLine(item.Name + " " + item.Calories);
}
خروجی مثال بالا:
Milk 100
Egg 100
Butter 200
Sugar 500
Flour 500
در این مثال در توالی ورودی، Flour قبل از Sugar آمده است. اما به خاطر عملگر ThenOrderByDescending ترتیب قرار گیری آنها در توالی خروجی تغییر کرده است.

پیاده سازی توسط عبارت‌های جستجو
در عبارت‌های جستجو نیز برای رسیدن به خروجی مشابه کد بالا از کلمه‌ی کلیدی descending استفاده می‌کنیم.
var query = from i in ingredients
orderby i.Calories, i.Name descending
select i;

عملگر Reverse

عملگر Reveres توالی ورودی را دریافت کرده و آن را معکوس و سپس توالی خروجی را تولید می‌کند. اولین عنصر توالی ورودی، آخرین عنصر توالی خروجی می‌باشد.
مثال:
char[] letters = { 'A', 'B', 'C' };
var query = letters.Reverse();
foreach (var item in query)
{
   Console.WriteLine(item);
}
خروجی مثال بالا:
C
B
A

پیاده سازی توسط عبارت‌های جستجو

معادل این عملگر، کلمه‌ی کلیدی دیگری در عبارت‌های جستجو وجود ندارد و ترکیب دو روش می‌تواند خروجی دلخواه را تولید کند.


عملگر‌های گروه بندی Grouping Operator

عملگر GroupBy

این عملگر یک توالی ورودی را دریافت کرده و یک توالی گروه بندی شده را به خروجی ارسال می‌کند. پایه‌ای‌ترین امضاء متد GroupBy، یک عبارت Lambda می‌باشد .این عبارت، کلید گروه بندی توالی ورودی را مشخص می‌کند.
مثال: در این مثال قصد داریم مواد غذایی مختلف را بر اساس کالری آنها گروه بندی کنیم.
Ingredient[] ingredients =
{
   new Ingredient {Name = "Sugar", Calories = 500},
   new Ingredient {Name = "Lard", Calories = 500},
   new Ingredient {Name = "Butter", Calories = 500},
   new Ingredient {Name = "Egg", Calories = 100},
   new Ingredient {Name = "Milk", Calories = 100},
   new Ingredient {Name = "Flour", Calories = 50},
   new Ingredient {Name = "Oats", Calories = 50}
};
IEnumerable<IGrouping<int, Ingredient>> query = ingredients.GroupBy(x => x.Calories);
foreach (IGrouping<int, Ingredient> group in query)
{
   Console.WriteLine("Ingredients with {0} calories", group.Key);
   foreach (Ingredient ingredient in group)
   {
     Console.WriteLine(" -{0}", ingredient.Name);
   }
}
در مثال فوق خروجی تابع GroupBy یک لیست قابل شمارش از نوع IGrouping می‌باشد. هر عنصر IGrouping شامل یک کلید (در اینجا کالری مواد غذایی) و لیستی از مواد غذایی که کالری‌های یکسانی دارند، می‌باشد.
خروجی مثال بالا:
 Ingredients with 500 calories
 -Sugar
 -Lard
 -Butter
Ingredients with 100 calories
 -Egg
 -Milk
Ingredients with 50 calories
 -Flour
 -Oats

پیاده سازی توسط عبارت‌های جستجو
در بخش پنجم این سری آموزشی، روش گروه بندی توسط عبارت‌های جستجو توضیح داده شده است.


عملگرهای مجموعه Set Operators
این عملگر‌ها شامل موارد زیر می‌باشند:
• Concat
• Union
• Distinct
• Intersect
• Except

عملگر Concat

این عملگر دو توالی را با هم ادغام می‌کند؛ بطوریکه عناصر توالی دوم، بعد از عناصر توالی اول به توالی خروجی اضافه می‌شوند.
مثال:
string[] applePie = { "Apple", "Sugar", "Pastry", "Cinnamon" };
string[] cherryPie = { "Cherry", "Sugar", "Pastry", "Kirsch" };
IEnumerable<string> query = applePie.Concat(cherryPie);
foreach (string item in query)
{
   Console.WriteLine(item);
}
خروجی مثال بالا :
Apple
Sugar
Pastry
Cinnamon
Cherry
Sugar
Pastry
Kirsch
توجه داشته باشید که در این حالت عناصر تکراری حذف نخواهند شد.

پیاده سازی توسط عبارت‌های جستجو
معادل این عملگر، کلمه‌ی کلیدی جدیدی در عبارت‌های جستجو وجود ندارد و ترکیب دو روش می‌تواند خروجی دلخواه را تولید کند.

عملگر Union
این عملگر همچون عملگر Concat رفتار می‌کند؛ با این تفاوت که عناصر تکراری در توالی خروجی حضور نخواهند داشت.
مثال:
string[] applePie = { "Apple", "Sugar", "Pastry", "Cinnamon" };
string[] cherryPie = { "Cherry", "Sugar", "Pastry", "Kirsch" };
IEnumerable<string> query = applePie.Union(cherryPie);
foreach (string item in query)
{
   Console.WriteLine(item);
}
خروجی مثال بالا:
Apple
Sugar
Pastry
Cinnamon
Cherry
Kirsch
همانطور که مشاهده می‌کنید، عناصر Sugar و Pastry که در توالی دوم تکرار شده بودند، در خروجی وجود ندارند.

پیاده سازی توسط عبارت‌های جستجو
معادل این عملگر، کلمه‌ی کلیدی جدیدی در عبارت‌های جستجو وجود ندارد و ترکیب دو روش می‌تواند خروجی دلخواه را تولید کند.

عملگر  Distinct
این عملگر عناصر تکراری توالی را حذف می‌کند. این عملگر هم می‌تواند بر روی یک توالی اجرا شود و هم می‌تواند بصورت زنجیره‌ای بعد از عملگر‌های دیگر بکار برود.
مثال: استفاده از این عملگر بر روی یک توالی:
string[] applePie = { "Apple", "Sugar", "Apple", "Sugar", "Pastry", "Cinnamon" };
IEnumerable<string> query = applePie.Distinct();
foreach (string item in query)
{
   Console.WriteLine(item);
}
در مثال بالا، عناصر تکراری در توالی ورودی را، از طریق عملگر Distinct حذف کرده‌ایم.
خروجی مثال بالا:
Apple
Sugar
Pastry
Cinnamon

مثال: بکارگیری عملگر Distinct بهمراه عملگر Concat برای شبیه سازی عملیات Union
string[] applePie = { "Apple", "Sugar", "Pastry", "Cinnamon" };
string[] cherryPie = { "Cherry", "Sugar", "Pastry", "Kirsch" };
IEnumerable<string> query = applePie.Concat(cherryPie).Distinct();

foreach (string item in query)
{
   Console.WriteLine(item);
}
خروجی مثال بالا:
Apple
Sugar
Pastry
Cinnamon
Cherry
Kirsch
همانطور که مشاهده می‌کنید خروجی این مثال با حالت استفاده از Union تفاوتی ندارد.

پیاده سازی توسط عبارت‌های جستجو
معادل این عملگر، کلمه‌ی کلیدی جدیدی در عبارت‌های جستجو وجود ندارد و ترکیب دو روش می‌تواند خروجی دلخواه را تولید کند.

عملگر Intersect
این عملگر عناصر مشترک بین دو توالی را باز می‌گرداند.
مثال:
string[] applePie = { "Apple", "Sugar", "Pastry", "Cinnamon" };
string[] cherryPie = { "Cherry", "Sugar", "Pastry", "Kirsch" };
IEnumerable<string> query = applePie.Intersect(cherryPie);
foreach (string item in query)
{
   Console.WriteLine(item);
}
خروجی مثال بالا:
Sugar
Pastry
نکته: عناصر تکراری فقط یکبار در خروجی ظاهر خواهند شد.

پیاده سازی توسط عبارت‌های جستجو
معادل این عملگر، کلمه‌ی کلیدی جدیدی در عبارت‌های جستجو وجود ندارد و ترکیب دو روش می‌تواند خروجی دلخواه را تولید کند.

عملگر Except
این عملگر عناصری از توالی اول را انتخاب می‌کند که در توالی دوم همتایی نداشته باشند.
مثال:
string[] applePie = { "Apple", "Sugar", "Pastry", "Cinnamon" };
string[] cherryPie = { "Cherry", "Sugar", "Pastry", "Kirsch" };
IEnumerable<string> query = applePie.Except(cherryPie);
foreach (string item in query)
{
   Console.WriteLine(item);
}
خروجی مثال فوق:
Apple
Cinnamon
نکته: هیچ عنصری از توالی دوم در خروجی وجود نخواهد داشت.

پیاده سازی توسط عبارت‌های جستجو
معادل این عملگر، کلمه‌ی کلیدی جدیدی در عبارت‌های جستجو وجود ندارد و ترکیب دو روش می‌تواند خروجی دلخواه را تولید کند.
نظرات اشتراک‌ها
گپ و گفتی با مهندسان طراح دات نت در مورد آینده این فریم ورک

Q: What's the near-term roadmap for .NET tooling?

A: While tentative, here are some short term plans:

  • Sep 2016 – Preview 3 of VS 2015 with early .CSProj support for .NET Core
  • Nov 2016 – .NET Core 1.2, ASP.NET Core 1.2, Entity Framework Core 1.2, SignalR, .NET Standard 2.0, etc.  
به نظرم این زمانبندی ای که داده اصلا درست نیست اولا که قبلا این موارد برای 2017 برنامه ریزی شده بود خصوصا سیگنال آر ثانیا الان NET Standard ورژنش 1.6 چطور تا چندماه این همه میخواد ترقی رتبه کنه ! ثالثا هنوز زمان بندی‌ها تو گیت هاب مثل گذشته است فکر نکنم بشه روی این مطلب حساب کرد
کسی در مورد زمانبندی‌ها اطلاعات جدیدی داره ؟
در کل امیدوارم من اشتباه کنم !
اشتراک‌ها
اضافه شدن پشتیبانی از فشرده سازی Brotli به NET Core.

In 2015, two engineers at Google designed a new compression algorithm called Brotli that can have a better compression without spending more time. Brotli is already supported by the most browsers such as Google Chrome, Mozilla Firefox, Opera, and Microsoft Edge.

services.AddResponseCompression(options =>
{
   options.Providers.Add<BrotliCompressionProvider>();
   options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" });
});

app.UseResponseCompression();

اضافه شدن پشتیبانی از فشرده سازی Brotli به NET Core.