Getting subclass type in base class?

I am trying to use Microsoft corporate network validation methods to perform validation on my entities. In my base class, I have the following method:

public class BaseEntity
{
  public bool IsValid()
  {
     return Validate().IsValid;
  }

  public ValidationResults Validate()
  {
     return Validation.Validate<this.GetType()>(this);
}

The problem is that even if a subclass of BaseEntity calls IsValid, this.GetType () always returns BaseEntity, not the type of the subclass. I do not want to rewrite this code for each object, as it seems very non-OO. Is there any other way to do this?

I had the idea of ​​protecting a protected variable of type _validationType and setting it to the value of this .GetType object in every object, but it seems like there should be a better way to do this.

Update
It doesn't seem to matter. this.GetType () seems to work the way I hoped. Not sure why this was not before.

Validate(), :

 return ValidationFactory.CreateValidator(this.GetType()).Validate(this);
+3
5

O/RM, LINQ to SQL, NHibernate LINQ to Entities (ADO.NET Entity Framework), . ( BaseEntity.Validate(). ObjectContext (EF)/DataContext (L2S)/Session (NH) LINQ to SQL:

public partial class NorthwindDataContext
{
    public override void SubmitChanges(ConflictMode failureMode)
    {
        var invalidResults = (
            from entity in this.GetChangedEntities()
            let type = entity.GetType()
            let validator = ValidationFactory.CreateValidator(type)
            let results = validator.Validate(entity)
            where !results.IsValid
            from result in results
            select result).ToArray();            

        if (invalidResults.Length > 0)
        {
            // You should define this exception type
            throw new ValidationException(invalidResults);
        }

        base.SubmitChanges(failureMode);
    }

    private IEnumerable<object> GetChangedEntities()
    {
        ChangeSet changes = this.GetChangeSet();
        return changes.Inserts.Concat(changes.Updates);
    }
}

, , ValidationException. InvalidResults, .

, . , Entity Framework.

.

+2

very un-OO like - . , .

- , .

+2

, , , , .

... , ( OO, ). :

public ValidationResults Validate<TEntity>(this TEntity e) where TEntity : BaseEntity
{
   return Validation.Validate<TEntity>(e);
}

The big advantage of the extension method among others ... :)

+1
source

You can make IsValid()and Validate()virtual method to provide some user-defined in subclasses.

0
source

Moving type logic from BaseEntity is cleaner.

public class BaseEntity
{
    public bool IsValid()
    {
        return Validate().IsValid;
    }

    public ValidationResults Validate()
    {
        return Validation.Validate(this);
    }
}
public class Validation
{
    public static ValidatorResults Validator<T>( T entity )
        where T : BaseEntity
    {
        return ValidationFactory.CreateValidator(entity.GetType()).Validate(entity);
    }
}
0
source

Source: https://habr.com/ru/post/1731739/


All Articles