<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DeleteEntityCommand.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Implements the delete entity command base class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Data.Commands
{
    using System;

    using Kephas.Data.Capabilities;
    using Kephas.Data.Resources;

    /// <summary>
    /// Base implementation of a <see cref="IDeleteEntityCommand"/>.
    /// </summary>
    [DataContextType(typeof(DataContextBase))]
    public class DeleteEntityCommand : SyncDataCommandBase<IDeleteEntityContext, IDataCommandResult>, IDeleteEntityCommand
    {
        /// <summary>
        /// Discards the changes in the data context.
        /// </summary>
        /// <param name="operationContext">The operation context.</param>
        /// <returns>
        /// A <see cref="IDataCommandResult"/>.
        /// </returns>
        public override IDataCommandResult Execute(IDeleteEntityContext operationContext)
        {
            var dataContext = operationContext.DataContext;
            var entity = operationContext.Entity;
            var entityInfo = dataContext.GetEntityInfo(entity);
            if (entityInfo == null)
            {
                throw new InvalidOperationException(Strings.DataContextBase_EntityNotAttached_Exception);
            }

            if (entityInfo.ChangeState == ChangeState.Added)
            {
                var localCache = this.TryGetLocalCache(dataContext);
                localCache?.Remove(entityInfo.Id);
            }
<commit_msg>Take advantage of the DetachEntity in DeleteEntityCommand<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DeleteEntityCommand.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Implements the delete entity command base class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Data.Commands
{
    using System;

    using Kephas.Data.Capabilities;
    using Kephas.Data.Resources;

    /// <summary>
    /// Base implementation of a <see cref="IDeleteEntityCommand"/>.
    /// </summary>
    [DataContextType(typeof(DataContextBase))]
    public class DeleteEntityCommand : SyncDataCommandBase<IDeleteEntityContext, IDataCommandResult>, IDeleteEntityCommand
    {
        /// <summary>
        /// Discards the changes in the data context.
        /// </summary>
        /// <param name="operationContext">The operation context.</param>
        /// <returns>
        /// A <see cref="IDataCommandResult"/>.
        /// </returns>
        public override IDataCommandResult Execute(IDeleteEntityContext operationContext)
        {
            var dataContext = operationContext.DataContext;
            var entity = operationContext.Entity;
            var entityInfo = dataContext.GetEntityInfo(entity);
            if (entityInfo == null)
            {
                throw new InvalidOperationException(Strings.DataContextBase_EntityNotAttached_Exception);
            }

            if (entityInfo.ChangeState == ChangeState.Added)
            {
                dataContext.DetachEntity(entityInfo);
            }

{'fraction_non_alphanumeric': 0.22286501377410467, 'fraction_numerical': 0.0, 'mean_word_length': 1.599141016463851, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4433447', 'n_tokens_mistral': 822, 'n_tokens_neox': 808, 'n_words': 187}