Note that there are some explanatory texts on larger screens.

plurals
  1. POInherited C# Class Losing "Reference"
    primarykey
    data
    text
    <p>I have a partial class using an interface because I can’t inherit what was an original abstract class due to the other partial class being auto-generated from Entity Framework 4 and therefore already inheriting ObjectContext.</p> <p>I have the following for my partial class:</p> <pre><code>namespace Model { using Microsoft.Practices.EnterpriseLibrary.Validation; using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; using Utilities.BusinessRules; using Utilities.BusinessRules.Rules; [HasSelfValidation] public partial class MyObject : IBusinessObject { private readonly IBusinessObject businessObject = new BusinessObject(); private IBusinessObject BusinessObject { get { return businessObject; } } public Comment() { AddRule(new ValidateRequired("Title")); } public void AddRule(BusinessRule rule) { BusinessObject.AddRule(rule); } [SelfValidation] public void Validate(ValidationResults results) { BusinessObject.Validate(results); } } } </code></pre> <p>Here’s the interface:</p> <pre><code>namespace Utilities.BusinessRules { using Microsoft.Practices.EnterpriseLibrary.Validation; public interface IBusinessObject { void AddRule(BusinessRule rule); void Validate(ValidationResults results); } } </code></pre> <p>And the implementation:</p> <pre><code>namespace Utilities.BusinessRules { using System.Collections.Generic; using System.Linq; using Microsoft.Practices.EnterpriseLibrary.Validation; using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; public class BusinessObject : IBusinessObject { private readonly IList&lt;BusinessRule&gt; businessRules = new List&lt;BusinessRule&gt;(); public void AddRule(BusinessRule rule) { this.businessRules.Add(rule); } [SelfValidation] public void Validate(ValidationResults results) { foreach (var rule in this.businessRules) { if (!rule.Validate(this)) { results.AddResult(new ValidationResult(rule.ErrorMessage, this, rule.Key, rule.Tag, null)); } } } } } </code></pre> <p>Don’t worry about the rules, they work. The problem is if I use an abstract class in a test project, the Validate() method in BusinessObject will correctly identity (this) as whatever class inherited the abstract class (in this example, I would expect that to be MyObject). Unfortunately, switching to an Interface, (this) loses the inheriting class and instead identifies this as BusinessObject.</p> <p>How can I make it so the inheriting class is properly identified?</p> <p>Here’s the calling console class:</p> <pre><code>namespace ModelConsumer { using Model; using System; using Microsoft.Practices.EnterpriseLibrary.Validation; class Program { static void Main(string[] args) { using (var myEntities = new MyEntities()) { var myObject= new MyObject(); myObject.Title = "Credo"; var validationResults = Validation.Validate(myObject); if (validationResults.IsValid) { myEntities.MyObjects.AddObject(myObject); //myEntities.SaveChanges(); Console.WriteLine("Valid."); } else { foreach (var validationResult in validationResults) { Console.WriteLine(validationResult.Message); } } Console.Read(); } } } } </code></pre> <p>This should be valid but will instead be invalid because (this) is identified as being type BusinessObject instead of MyObject.</p> <p>Argh! I'm so close, it's pretty vexing.</p> <p>Richard</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload