开发者

How to extend abstract Entity class in RIA Services

开发者 https://www.devze.com 2023-02-01 05:51 出处:网络
I want to add a bool variable and property to the base Entity class in my RIA services project so that it is available throughout all the entity objects but seem unable to work out how to do this. I k

I want to add a bool variable and property to the base Entity class in my RIA services project so that it is available throughout all the entity objects but seem unable to work out how to do this. I know that adding properties to actual entities themselves is easy using .shared.cs and partial开发者_开发知识库 classes but adding such properties to the Entity class using similar methods doesn't work.

For example, the following code doesn't work

namespace System.ServiceModel.DomainServices.Client
{
    public abstract partial class Entity
    {
        private bool auditRequired;
        public bool AuditRequired
        {
            get { return auditRequired; }
            set { auditRequired = value; }
        }
    }
}

All that happens is that the existing Entity class gets totally overriden rather than extending the Entity class.

How do I extend the base Entity class so that functionality is available thoughout all derived entity classes?


You won't be able to add a property to the Entity class. This class is already compiled and cannot be modified (partial classes only work because your have the source code of the class in your solution and the code can be merged at compile time).

One option may be to create a class that inherits from Entity, then add your property in this class, and have your entities inherit from your custom class instead of Entity. This might not be practical for use with designers, though.

public class MyEntityBase : Entity
{
    private bool auditRequired;
    public bool AuditRequired
    {
        get { return auditRequired; }
        set { auditRequired = value; }
    }
}

public class Entity1 : MyEntityBase
{

}
0

精彩评论

暂无评论...
验证码 换一张
取 消