Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor your C# code with primary constructors #1128

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ namespace Smartstore.Core.Catalog.Attributes
{
internal class ProductAttributeOptionMap : IEntityTypeConfiguration<ProductAttributeOption>
{
public void Configure(EntityTypeBuilder<ProductAttributeOption> builder)
{
public void Configure(EntityTypeBuilder<ProductAttributeOption> builder) =>
// INFO: DeleteBehavior.Cascade required otherwise System.InvalidOperationException when deleting ProductAttributeOptionsSet.
builder.HasOne(c => c.ProductAttributeOptionsSet)
.WithMany(c => c.ProductAttributeOptions)
.HasForeignKey(c => c.ProductAttributeOptionsSetId)
.OnDelete(DeleteBehavior.Cascade);
}
}

/// <summary>
Expand Down Expand Up @@ -108,29 +106,25 @@ public ProductVariantAttributeValueType ValueType
public int Quantity { get; set; }

/// <inheritdoc/>
public ProductVariantAttributeValue Clone()
{
var value = new ProductVariantAttributeValue
{
Alias = Alias,
Name = Name,
MediaFileId = MediaFileId,
Color = Color,
PriceAdjustment = PriceAdjustment,
WeightAdjustment = WeightAdjustment,
IsPreSelected = IsPreSelected,
DisplayOrder = DisplayOrder,
ValueTypeId = ValueTypeId,
LinkedProductId = LinkedProductId,
Quantity = Quantity
};

return value;
}

object ICloneable.Clone()
{
return Clone();
}
public ProductVariantAttributeValue Clone() =>
new ProductVariantAttributeValue
{
Alias = Alias,
Name = Name,
MediaFileId = MediaFileId,
Color = Color,
PriceAdjustment = PriceAdjustment,
WeightAdjustment = WeightAdjustment,
IsPreSelected = IsPreSelected,
DisplayOrder = DisplayOrder,
ValueTypeId = ValueTypeId,
LinkedProductId = LinkedProductId,
Quantity = Quantity
};




object ICloneable.Clone() => Clone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ namespace Smartstore.Core.Catalog.Attributes
{
internal class ProductAttributeOptionsSetMap : IEntityTypeConfiguration<ProductAttributeOptionsSet>
{
public void Configure(EntityTypeBuilder<ProductAttributeOptionsSet> builder)
{
builder.HasOne(c => c.ProductAttribute)
public void Configure(EntityTypeBuilder<ProductAttributeOptionsSet> builder) => builder.HasOne(c => c.ProductAttribute)
.WithMany(c => c.ProductAttributeOptionsSets)
.HasForeignKey(c => c.ProductAttributeId);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,14 @@ public AttributeControlType AttributeControlType
/// <summary>
/// Gets a value indicating whether the attribute has a list of values.
/// </summary>
public bool IsListTypeAttribute()
public bool IsListTypeAttribute() => AttributeControlType switch
{
return AttributeControlType switch
{
AttributeControlType.TextBox or
AttributeControlType.MultilineTextbox or
AttributeControlType.Datepicker or
AttributeControlType.FileUpload => false,
_ => true, // All other attribute control types support values.
};
}
AttributeControlType.TextBox or
AttributeControlType.MultilineTextbox or
AttributeControlType.Datepicker or
AttributeControlType.FileUpload => false,
_ => true, // All other attribute control types support values.
};

private ICollection<ProductVariantAttributeValue> _productVariantAttributeValues;
/// <summary>
Expand All @@ -144,19 +141,16 @@ public RuleSetEntity RuleSet
set => _ruleSet = value;
}

public ProductVariantAttribute Clone()
public ProductVariantAttribute Clone() => new()
{
return new()
{
ProductId = ProductId,
ProductAttributeId = ProductAttributeId,
TextPrompt = TextPrompt,
CustomData = CustomData,
IsRequired = IsRequired,
AttributeControlTypeId = AttributeControlTypeId,
DisplayOrder = DisplayOrder,
};
}
ProductId = ProductId,
ProductAttributeId = ProductAttributeId,
TextPrompt = TextPrompt,
CustomData = CustomData,
IsRequired = IsRequired,
AttributeControlTypeId = AttributeControlTypeId,
DisplayOrder = DisplayOrder,
};

object ICloneable.Clone()
=> Clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,9 @@ where idx > 0
/// <summary>
/// Sets the assigned media file identifiers.
/// </summary>
public void SetAssignedMediaIds(int[] ids)
{
public void SetAssignedMediaIds(int[] ids) =>
AssignedMediaFileIds = ids?.Length > 0
? string.Join(',', ids)
: null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ namespace Smartstore.Core.Catalog.Attributes
/// Represents a product variant attribute selection.
/// </summary>
/// <remarks>This class can parse strings of XML or JSON format.</remarks>
public class ProductVariantAttributeSelection : AttributeSelection
/// <remarks>
/// Creates product variant attribute selection from string.
/// Use <see cref="AttributeSelection.AttributesMap"/> to access parsed attributes afterwards.
/// </remarks>
/// <remarks>Automatically differentiates between XML and JSON.</remarks>
/// <param name="rawAttributes">XML or JSON attributes string.</param>
public class ProductVariantAttributeSelection(string rawAttributes)
: AttributeSelection(rawAttributes, "ProductVariantAttribute")
{
const string GiftCardAttributeName = "GiftCardInfo";

/// <summary>
/// Creates product variant attribute selection from string.
/// Use <see cref="AttributeSelection.AttributesMap"/> to access parsed attributes afterwards.
/// </summary>
/// <remarks>Automatically differentiates between XML and JSON.</remarks>
/// <param name="rawAttributes">XML or JSON attributes string.</param>
public ProductVariantAttributeSelection(string rawAttributes)
: base(rawAttributes, "ProductVariantAttribute")
{
}

/// <summary>
/// Gets the gift card information.
/// </summary>
Expand All @@ -40,10 +36,7 @@ public GiftCardInfo GetGiftCardInfo()
/// Adds gift card infomation to be taken into account when serializing attributes.
/// </summary>
/// <param name="giftCard">Gift card information.</param>
public void AddGiftCardInfo(GiftCardInfo giftCard)
{
AddCustomAttributeValue(GiftCardAttributeName, giftCard);
}
public void AddGiftCardInfo(GiftCardInfo giftCard) => AddCustomAttributeValue(GiftCardAttributeName, giftCard);

protected override object ToCustomAttributeValue(string attributeName, object value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ namespace Smartstore.Core.Catalog.Attributes
{
internal class ProductVariantAttributeValueMap : IEntityTypeConfiguration<ProductVariantAttributeValue>
{
public void Configure(EntityTypeBuilder<ProductVariantAttributeValue> builder)
{
public void Configure(EntityTypeBuilder<ProductVariantAttributeValue> builder) =>
builder.HasOne(c => c.ProductVariantAttribute)
.WithMany(c => c.ProductVariantAttributeValues)
.HasForeignKey(c => c.ProductVariantAttributeId);
}
.WithMany(c => c.ProductVariantAttributeValues)
.HasForeignKey(c => c.ProductVariantAttributeId);
}

/// <summary>
Expand Down Expand Up @@ -111,24 +109,21 @@ public ProductVariantAttributeValueType ValueType
/// </summary>
public int Quantity { get; set; }

public ProductVariantAttributeValue Clone()
public ProductVariantAttributeValue Clone() => new()
{
return new()
{
ProductVariantAttributeId = ProductVariantAttributeId,
Name = Name,
Alias = Alias,
MediaFileId = MediaFileId,
Color = Color,
PriceAdjustment = PriceAdjustment,
WeightAdjustment = WeightAdjustment,
IsPreSelected = IsPreSelected,
DisplayOrder = DisplayOrder,
ValueTypeId = ValueTypeId,
LinkedProductId = LinkedProductId,
Quantity = Quantity
};
}
ProductVariantAttributeId = ProductVariantAttributeId,
Name = Name,
Alias = Alias,
MediaFileId = MediaFileId,
Color = Color,
PriceAdjustment = PriceAdjustment,
WeightAdjustment = WeightAdjustment,
IsPreSelected = IsPreSelected,
DisplayOrder = DisplayOrder,
ValueTypeId = ValueTypeId,
LinkedProductId = LinkedProductId,
Quantity = Quantity
};

object ICloneable.Clone()
=> Clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,17 @@ public void AddVariant(ProductVariantQueryItem item)
}
}

public void AddGiftCard(GiftCardQueryItem item)
{
_giftCards.Add(item);
}
public void AddGiftCard(GiftCardQueryItem item) => _giftCards.Add(item);

public void AddCheckoutAttribute(CheckoutAttributeQueryItem item)
{
_checkoutAttributes.Add(item);
}
public void AddCheckoutAttribute(CheckoutAttributeQueryItem item) => _checkoutAttributes.Add(item);

public string GetGiftCardValue(int productId, int bundleItemId, string name) =>
_giftCards.FirstOrDefault(x =>
x.ProductId == productId &&
x.BundleItemId == bundleItemId &&
x.Name.EqualsNoCase(name))
?.Value;

public string GetGiftCardValue(int productId, int bundleItemId, string name)
{
return _giftCards.FirstOrDefault(x =>
x.ProductId == productId &&
x.BundleItemId == bundleItemId &&
x.Name.EqualsNoCase(name))
?.Value;
}

public GiftCardInfo GetGiftCardInfo(int productId, int bundleItemId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ namespace Smartstore.Core.Catalog.Attributes
{
internal class SpecificationAttributeOptionMap : IEntityTypeConfiguration<SpecificationAttributeOption>
{
public void Configure(EntityTypeBuilder<SpecificationAttributeOption> builder)
{
public void Configure(EntityTypeBuilder<SpecificationAttributeOption> builder) =>
builder.HasOne(c => c.SpecificationAttribute)
.WithMany(c => c.SpecificationAttributeOptions)
.HasForeignKey(c => c.SpecificationAttributeId);
}
.WithMany(c => c.SpecificationAttributeOptions)
.HasForeignKey(c => c.SpecificationAttributeId);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
namespace Smartstore.Core.Catalog.Attributes
{
[Important]
internal class ProductVariantAttributeCombinationHook : AsyncDbSaveHook<ProductVariantAttributeCombination>
internal class ProductVariantAttributeCombinationHook(SmartDbContext db, IRequestCache requestCache)
: AsyncDbSaveHook<ProductVariantAttributeCombination>
{
private readonly SmartDbContext _db;
private readonly IRequestCache _requestCache;

public ProductVariantAttributeCombinationHook(SmartDbContext db, IRequestCache requestCache)
{
_db = db;
_requestCache = requestCache;
}
private readonly SmartDbContext _db = db;
private readonly IRequestCache _requestCache = requestCache;

public override Task<HookResult> OnAfterSaveAsync(IHookedEntity entry, CancellationToken cancelToken)
=> Task.FromResult(HookResult.Ok);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@
namespace Smartstore.Core.Catalog.Attributes
{
[Important]
internal class ProductVariantAttributeHook : AsyncDbSaveHook<ProductVariantAttribute>
internal class ProductVariantAttributeHook(SmartDbContext db) : AsyncDbSaveHook<ProductVariantAttribute>
{
private readonly SmartDbContext _db;

public ProductVariantAttributeHook(SmartDbContext db)
{
_db = db;
}
private readonly SmartDbContext _db = db;

protected override Task<HookResult> OnDeletedAsync(ProductVariantAttribute entity, IHookedEntity entry, CancellationToken cancelToken)
=> Task.FromResult(HookResult.Ok);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
namespace Smartstore.Core.Catalog.Attributes
{
[Important]
internal class ProductVariantAttributeValueHook : AsyncDbSaveHook<ProductVariantAttributeValue>
internal class ProductVariantAttributeValueHook(SmartDbContext db) : AsyncDbSaveHook<ProductVariantAttributeValue>
{
private readonly SmartDbContext _db;

public ProductVariantAttributeValueHook(SmartDbContext db)
{
_db = db;
}
private readonly SmartDbContext _db = db;

/// <summary>
/// Sets all product variant attribute values to <see cref="ProductVariantAttributeValue.IsPreSelected"/> = false if the currently inserted entity is preselected.
/// </summary>
protected override async Task<HookResult> OnInsertingAsync(ProductVariantAttributeValue entity, IHookedEntity entry, CancellationToken cancelToken)
{
await ResetPreselectedProductVariantAttributeValues(entity, cancelToken);

return HookResult.Ok;
}

Expand All @@ -28,6 +24,7 @@ protected override async Task<HookResult> OnInsertingAsync(ProductVariantAttribu
protected override async Task<HookResult> OnUpdatingAsync(ProductVariantAttributeValue entity, IHookedEntity entry, CancellationToken cancelToken)
{
await ResetPreselectedProductVariantAttributeValues(entity, cancelToken);

return HookResult.Ok;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ internal class UnavailableAttributeCombinationsHook : AsyncDbSaveHook<BaseEntity
{
private readonly ICacheManager _cache;

public UnavailableAttributeCombinationsHook(ICacheManager cache)
{
_cache = cache;
}
public UnavailableAttributeCombinationsHook(ICacheManager cache) => _cache = cache;

public override async Task<HookResult> OnAfterSaveAsync(IHookedEntity entry, CancellationToken cancelToken)
{
Expand Down
Loading