Improve BaseEntity class to support generic

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 年 前
According to the current design, the entity identifier is restricted to integer. I think we should use generic design. The improved code as below:

using System;

namespace Nop.Core
{
    /// <summary>
    /// Base class for entities
    /// </summary>
    public abstract partial class BaseEntity<T>
    {
        /// <summary>
        /// Gets or sets the entity identifier
        /// </summary>
        public T Id { get; set; }

        public override bool Equals(object obj)
        {
            return Equals(obj as BaseEntity<T>);
        }

        private static bool IsTransient(BaseEntity<T> obj)
        {
            return obj != null && Equals(obj.Id, default(T));
        }

        private Type GetUnproxiedType()
        {
            return GetType();
        }

        public virtual bool Equals(BaseEntity<T> other)
        {
            if (other == null)
                return false;

            if (ReferenceEquals(this, other))
                return true;

            if (!IsTransient(this) &&
                !IsTransient(other) &&
                Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) ||
                        otherType.IsAssignableFrom(thisType);
            }

            return false;
        }

        public override int GetHashCode()
        {
            if (Equals(Id, default(T)))
                return base.GetHashCode();
            return Id.GetHashCode();
        }

        public static bool operator ==(BaseEntity<T> x, BaseEntity<T> y)
        {
            return Equals(x, y);
        }

        public static bool operator !=(BaseEntity<T> x, BaseEntity<T> y)
        {
            return !(x == y);
        }
    }
}


In Domain class, we rewrite the code, such as:

public partial class Customer : BaseEntity<Guid>
{}
public partial class UrlRecord : BaseEntity<int>
{}


But I don't know how to rewrite IRepository ans EfRepository class.

public partial interface IRepository<T> where T : BaseEntity
{}
public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
{}


Who can give me some idea? Thanks very much!
8 年 前
You can use something like this:

public partial interface IRepository<TEntity, TKey> where TEntity : BaseEntity<TKey>
{ }
public partial class EfRepository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : BaseEntity<TKey>
{ }


But I think there are too many refactorings you need to do.
5 年 前
Hi. Do you know why a validation software says that I should delete this base call? and change it by:



public class Point
{
private readonly int x;
public MyClass (int x)
{
this.x = x;
}
public override int GetHashCode ()
{
return x.GetHashCode ();
}
}
Exceptions
This rule does not report on guard conditions checking for reference equality.
public class Point
{
public override bool Equals (object obj)
{
if (base.Equals (obj)) // Compliant, although it could be replaced with
object.ReferenceEquals (obj, this), which is clearer
{
return true;
}
...
}
}
5 年 前
Bump
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.