We have an abstract class where all properties have private setters. In our concrete derived class, the code generator is creating a static “create” method that attempts to set the properties of the abstract class. Obviously this fails since the setters are private.
How do we suppress the creation of the “create” method?
In v3.5 I don't think you can suppress just this part. However, it will only attempt to set non-nullable/required properties. So I see a few options. None are ideal.
Hang on for v4.0, where you can customize codegen.
Abandon EF codegen altogether and use a custom data class.
Make the properties nullable.
Don't put the properties on the parent type. Put them on the subtypes and use an interface for polymorphism.
Don't make the properties private.
Related
Extension methods are a great way to extend the functionality of a type.
Are there any ways similar to this which can be used to extend properties of a class without inheriting a new class.
No extension properties do not exist.
You can't do it via properties without inheriting a new class. There are only extension methods, not extension properties (it may be added at a future date). If you don't want to alter the original class you should inherit from the original class and then add your properties to the derived class.
I'm wondering if there is some way to declare a variable that is member data of a base class so that it is not inherited by a class derived from it. I have some member data in my base class that should not be part of objects of the derived class, so I'd like to separate what should be inherited from what should not. Is there some way to do this?
If you're trying to 'hide' data from derived/inherited classes, use private access modifier.
Yes, even though they are inherited, you cannot access them unless they are marked protected or public.
Ric, yes. I think it's just not a feature available in C++. What I'd really like to do is create a base class with member data with some kind of prefix that prevents the data from being inherited by child classes. Something like: noinherit void func1(); or noinherit double x; Where noinherit is just some keyword I made up to define data that should not be inherited by child classes. In a way, I want to be able to determine the genes inherited by the children from the parent, instead of the children just getting the full set of the parent's genes, and simply having a certain phenotype based on which genes are private, and which are public or protected, to use a genetics analogy.
Is there a way to initialize virtual automatic property without using a constructor ?
or should i just make a private field ?
Virtual or not, you need a constructor. Or it will have the default-value for it's type.
Because it is not good to call a virtual member in constructor
Correct, you will have to step carefully. The normal rules do apply. To be safe you would design your property so that it doesn't need initialization or only initialize it in derived constructors where either the class or the property is sealed.
Note that there will be plenty of cases where automatic properties makes little or no sense.
In the case of a virtual automatic property, I would say that the initialization part makes this a problem, and would remove the "automatic" part and create a backing field.
Of course, since base constructors are called before descendant constructors, if a base constructor initializes the property to the wrong value, a descendant constructor has a chance to rectify that before construction of the object is final.
Yesterday I thought it would be nice to implement my own Trigger in a WPF app. I created a class MyTrigger which inherited TriggerBase. TriggerBase is a public abstract class. So inheritance isn't a problem. But the constructors inside this class are marked internal. The compiler throws an error because the is no valid constructor. Why does anyone create a public class but marks the constructors as internal?
If you want the class to be visible, but only allow it to be subclassed within your own assembly. The subclasses may have public constuctors themselves - or they may be accessed with a factory.
I can't comment on whether that's a good design decision for TriggerBase in WPF, but it's at least reasonable in some situations.
One reason that I could think of is that the actual creation of new instances would be handled by another public class in the same assembly. This would force that you create the instance through this other class - possibly some sort of a factory pattern implementation.
It's public because it's used as a base class for the triggers that ship with WPF (Trigger, MultiTrigger, EventTrigger, DataTrigger etc). It it wasn't public then you wouldn't be able to flag these classes as public.
The constructors are internal because they don't intend for you to use it yourself. I'd guess you're suppose to derive from one of the classes mentioned above.
I have read that private variables in a base class are technically inherited by child classes, but are not accessible.
If this is correct, why do we say they are inherited when presumably they can only be accessed by reflection?
Subclassing is about inheriting implementation; and fields are an implementation detail.
The fields are indeed present, and are available via reflection. But ultimately, it is the base-classes job to manage the state of those fields via any public/protected/etc members.
But ultimately - if a base-class declares a property (and field) for property Foo, then when you set that property the data has to go somewhere. The sub-class has to include all the fields from the base-class for it to make sense. This is also critical for field-based serialization frameworks (such as BinaryFormatter).
Private fields are inherited in the sense, that they take up space on the heap when allocated. However, the derived class cannot access them directly.
why do we say they are inherited...
Personally, I don't. I consider inheritance to include those things that you can access in a child class, not those things that are hidden.
I could see someone saying that to be clear that inheritance includes all elements up the chain, but it strikes me as overly pedantic and not especially useful.