Skip to content

Commit

Permalink
Merge pull request #379 from shesha-io/fix/alex_stephens
Browse files Browse the repository at this point in the history
fix Equals for GenericEntityReference
  • Loading branch information
AlexStepantsov committed Jul 24, 2023
2 parents 47eb5f1 + f47eb77 commit b81abac
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Shesha.EntityReferences
{
public class GenericEntityReference : IGenericEntityReference
public class GenericEntityReference : IEquatable<GenericEntityReference>, IGenericEntityReference
{
private object _entity;

Expand Down Expand Up @@ -55,5 +55,32 @@ private static GenericEntityReference SetEntity<T>(Entity<T> entity)
{
return new GenericEntityReference(entity);
}

public override bool Equals(object obj) => this.Equals(obj as GenericEntityReference);

public bool Equals(GenericEntityReference obj)
{
return Id == obj.Id && _className == obj._className;
}

public static bool operator ==(GenericEntityReference l, GenericEntityReference r)
{
if (l is null)
{
if (r is null)
return true;
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return l.Equals(r);
}

public static bool operator !=(GenericEntityReference l, GenericEntityReference r) => !(l == r);

public override int GetHashCode()
{
return Id.IsNullOrEmpty() ? 0 : Id.GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Shesha.EntityReferences;
using System;
using System.Data.Common;
using System.Data.SqlTypes;

namespace Shesha.NHibernate.UserTypes
{
Expand Down Expand Up @@ -37,8 +38,10 @@ public object Disassemble(object value, ISessionImplementor session)

public new bool Equals(object x, object y)
{
if (x == null && y == null) return true;

if (x is GenericEntityReference erx && y is GenericEntityReference ery)
return erx.Id == ery.Id && erx._className == ery._className;
return erx == ery;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public object Disassemble(object value, ISessionImplementor session)

public new bool Equals(object x, object y)
{
if (x == null && y == null) return true;

if (x is GenericEntityReference erx && y is GenericEntityReference ery)
return erx.Id == ery.Id && erx._className == ery._className;
return erx == ery;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public new bool Equals(object x, object y)
if (x == null && y == null)
return true;

if (x == null || y == null)
if (x == null && y != null || x != null && y == null)
return false;

if (x is IJsonEntityProxy xp && y is IJsonEntityProxy yp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.Timing;
using DocumentFormat.OpenXml.Vml.Office;
using Shesha.Domain;
using Shesha.Domain.ConfigurationItems;
using Shesha.DynamicEntities;
using Shesha.EntityReferences;
using Shesha.Extensions;
using Shesha.NHibernate;
using Shesha.NHibernate.Session;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
Expand All @@ -17,7 +20,7 @@
namespace Shesha.Tests.EntityReferenceTest
{
public class EntityReference_Tests : SheshaNhTestBase
{
{
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IRepository<Person, Guid> _personRepository;
private readonly IRepository<Organisation, Guid> _organisationRepository;
Expand All @@ -40,12 +43,40 @@ public EntityReference_Tests()
_moduleRepo = Resolve<IRepository<Module, Guid>>();
}

[Fact]
public async Task TestGnericEntityReference()
{
LoginAsHostAdmin();

using (var uow = _unitOfWorkManager.Begin())
{
var repo = LocalIocManager.Resolve<IRepository<ShaRoleAppointedPerson, Guid>>();
var items = await repo.GetAllListAsync();

var sessionProvider = LocalIocManager.Resolve<ISessionProvider>();
var session = sessionProvider.Session;

//GenericEntityReference i = null;

foreach (var item in items)
{
//var b = i == item.PermissionedEntity1;
//i = item.PermissionedEntity1;
var entry = session?.GetEntry(item, false);
var dirty = session.GetDirtyProperties(item);
}

await uow.CompleteAsync();
}
}

[Fact]
public async Task CheckUow()
{
LoginAsHostAdmin();

using (var uow = _unitOfWorkManager.Begin(new UnitOfWorkOptions {
using (var uow = _unitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsTransactional = true,
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
}))
Expand Down Expand Up @@ -75,70 +106,70 @@ public async Task CheckUow()
}
}

/* [Table("Test_EntityRef")]
public class EntityRef : Entity<Guid>
{
public GenericEntityReference AnyEntity { get; set; }
/* [Table("Test_EntityRef")]
public class EntityRef : Entity<Guid>
{
public GenericEntityReference AnyEntity { get; set; }
public GenericEntityReference MyEntity { get; set; }
public GenericEntityReference MyEntity { get; set; }
}
}
[Fact]
public async Task CheckUserType()
{
LoginAsHostAdmin();
[Fact]
public async Task CheckUserType()
{
LoginAsHostAdmin();
using (var uow = _unitOfWorkManager.Begin())
{
EntityRef er = _entityRefRepository.GetAll().FirstOrDefault();
using (var uow = _unitOfWorkManager.Begin())
{
EntityRef er = _entityRefRepository.GetAll().FirstOrDefault();
Entity<Guid> anyEntity = er.AnyEntity;
Entity<Guid> anyEntity = er.AnyEntity;
if (anyEntity is Person person)
{
if (anyEntity is Person person)
{
}
if (anyEntity is Organisation organisation)
{
}
if (anyEntity is Organisation organisation)
{
}
}
var entity = (Person)er.AnyEntity;
var entity = (Person)er.AnyEntity;
if (anyEntity is Person person2)
{
var name = person2.FullName;
}
if (anyEntity is Person person2)
{
var name = person2.FullName;
}
var org = _organisationRepository.GetAll().FirstOrDefault();
var org = _organisationRepository.GetAll().FirstOrDefault();
GenericEntityReference eref = org;
er.AnyEntity = org;
_entityRefRepository.InsertOrUpdate(er);
GenericEntityReference eref = org;
er.AnyEntity = org;
_entityRefRepository.InsertOrUpdate(er);
uow.Complete();
}
}
uow.Complete();
}
}
[Fact]
public async Task CheckMuliProp()
{
LoginAsHostAdmin();
[Fact]
public async Task CheckMuliProp()
{
LoginAsHostAdmin();
using (var uow = _unitOfWorkManager.Begin())
{
EntityRef er = _entityRefRepository.GetAll().FirstOrDefault();
using (var uow = _unitOfWorkManager.Begin())
{
EntityRef er = _entityRefRepository.GetAll().FirstOrDefault();
Entity<Guid> anyEntity = er.AnyEntity;
Entity<Guid> anyEntity = er.AnyEntity;
er.MyEntity = anyEntity;
er.MyEntity = anyEntity;
_entityRefRepository.InsertOrUpdate(er);
_entityRefRepository.InsertOrUpdate(er);
uow.Complete();
}
}
*/
uow.Complete();
}
}
*/
}
}

0 comments on commit b81abac

Please sign in to comment.