While reading Jon Skeet's article on fields vs properties he mentions that changing fields to properties is a breaking change.
I would like to understand the common scenarios in which this change can cause breaks. Along with the scenario, if you can, please provide any details.
For starters, the following points have been mentioned elsewhere:
You can't change fields to properties if you are using reflection on the class. This is obvious even though I don't have details. Serialization is one scenario where reflection is used to iterate over the object and changing fields to properties will break the serializer or change the output
You can't easily bind against fields. (Why is this? I read it here)
???
EDIT: Robert has a comprehensive list of reasons for choosing properties over fields and also explains how switching between them can cause a breaking change.
If you have a public field and another assembly has code that is using it, it will need to be recompiled.
IOW the definition of breaking includes "will need to be recompiled".
Properties can throw any arbitrary exceptions, whereas fields can't (at least when compiler knows about field assignment at compile time).
In Windows Forms at least, you can only databind things like DataGridViewColumns to properties on your business objects, not fields. So if your class was being used as a DataSource for a grid, its properties changing to fields would result in some new bugs for the grid owner.
You can pass a field as a ref or out parameter, or take its address in an unsafe context, whilst you cannot do these with a property.
Related
Lets say my c# model updated while correspondent collection still contains old documents, I want old and new documents to coexist in the collection, while using only new version of c# model to read them. I wish no inheritance is used if possible. So I wonder which of this issues are solvable and how:
there is a new property in c# model which does not present in database. I think it never should be an issue, Mongo knows nothing about it, and it will be initialized with default value. The only issue here is to initialize it with particular value for all old documents, anybody knows how?
one of property has gone from model. I want MongoDb to find out there is no more property in c# class to map the field of old document to, and to ignore it instead of crashing. This scenario probably sounds a bit strange as it would mean some garbage left in database, but anyway, is the behavior possible to implement/configure?
type if changed, new type is convertible to old one, like integer->string. Is there any way to configure mapping for old docs?
I can consider using inheritance for second case if it is not solvable otherwise
Most of the answers to your questions are found here.
BsonDefaultValue("abc") attribute on properties to handle values not present in the database, and to give them a default value upon deserialization
BsonIgnoreExtraElements attribute on the class to ignore extra elements found during deserialization (to avoid the exception)
A custom serializer is required to handle if the type of a member is changed, or you need to write an upgrade script to fix the data. It would probably be easier to leave the int on load, and save to a string as needed. (That will mean that you'll need a new property name for the string version of the property.)
Many questions have been raised on this site and others regarding the "Dependency property field missing" false error that is generated by Resharper when using this implementation of binding to the Password property of a PasswordBox. Simply searching for the phrase with Google returns "About 8,550,000 results" at the time of this posting. There is also a bug report on it.
I recently found another implementation that does not generate the error, and I'm struggling to find the difference in that implementation that causes Resharper to not generate the false error.
The differences that I see:
The property that actually stores the password string (BoundPassword/Password) uses a FrameworkPropertyMetadata object in the second implementation instead of the standard PropertyMetadata object. But when I changed the first implementation to use this new type, I still got the error.
The first implementation performs casting prior to assigning in the property-changed handlers, whereas the second has both in the same statement. But this is just coding preference; the net result wouldn't change.
What am I missing?
The answer you looking for was given in RSRP-174487 issue comment 2 years ago - dependency/attached property field names should follow Microsoft notation - SomeNameProperty (have Property suffix) - to be correctly handled by ReSharper.
Dependency property registration is actually may be dynamic and performed at runtime, but this is really rare scenario and ReSharper still supports it by simply triggering of "Dependency property field missing" inspection off (Alt + Enter - Suppress inspection).
ReSharper is designed to deal with statically known dependency property registrations and validates existence of DP fields for you, but require you to follow Microsoft DP fields naming notation to work well. I think error message may be better, we will work on it.
Fluent builder is a well-known pattern to build objects with many properties:
Team team = teamBuilder.CreateTeam("Chelsea")
.WithNickName("The blues")
.WithShirtColor(Color.Blue)
.FromTown("London")
.PlayingAt("Stamford Bridge");
However, using it doesn't seem very clear to me due to one particular reason:
Every Team object has its minimal operational state, in other words, set of properties which have to be set (mandatory), so that the object is ready to use.
Now, how should the Fluent builder approach be used considering that you have to maintain this state?
Should the With_XYZ members modify the part of the object, that can't affect this state?
Maybe there are some general rules for this situation?
Update:
If the CreateTeam method should take the mandatory properties as arguments, what happens next?
What happens if I (for example) omit the WithNickName call?
Does this mean that the nickname should be defaulted to some DefaultNickname?
Does this mean that the example (see the link) is bad, because the object can be left in invalid state?
And, well, I suspect that in this case the fluent building approach actually loses it's "beauty", doesn't it?
CreateTeam() should have the mandatory the properties as parameters.
Team CreateTeam(string name, Color shirtColor, string Town)
{
}
Seems to me the points of Fluent Interface are:
Minimize the number of parameters to zero in a constructor while still dynamically initializing certain properties upon creation.
Makes the property/ parameter-value association very clear - in a large parameter list, what value is for what? Can't tell without digging further.
The coding style of the instantiation is very clean, readable, and editable. Adding or deleting property settings with this formatting style is less error prone. I.E. delete an entire line, rather than edit in the middle of a long parameter list; not to mention editing the wrong parameter
I thought I had type editors and converters nailed until I tried to persist a Readonly Reference type property after editing it in a UITypeEditor.
In my UITypeEditor, because I'm working with a read only property, I'm careful to pass back the original value (after updating the relevant sub property).
This change is reflected immediately on the designer but will not be persisted unless I do something like resize the control that the property is attached to.
To fix this I, blindly, include a call to context.OnComponentChanged() before returning the value.
I can see why this is needed. It's a reference type, I've altered it (not replaced it), and the property grid doesn't know this. I have a couple of questions for clarification:
Do I need a call to context.OnComponentChanging as well? A simple call to OnComponentChanged works in the tests I've done so far, but I don't want biting on the arse at some point in the future.
Also, is there any danger that, with my call to OnComponentChanging, I'll be persisting other components, in DesignerTransactions, that I shouldn't be persisting?
i´m trying to query a DataTable object without specifying the fields, like this :
var linqdata = from ItemA in ItemData.AsEnumerable()
select ItemA
but the returning type is
System.Data.EnumerableRowCollection<System.Data.DataRow>
and I need the following returning type
System.Data.EnumerableRowCollection<<object,object>>
(like the standard anonymous type)
Any idea?
Thanks
If I understand you correctly, you'd like to get a collection of objects that you don't need to define in your code but that are usable in a strongly typed fashion. Sadly, no you can't.
An anonymous type seems like some kind of variant or dynamic object, but it is in fact a strongly typed class that is defined at compile time. .NET defines the type for you automatically behind the scenes. In order for .net to be able to do this, it has to have some clue from the code with which to infer the type definition. It has to have something like:
from ItemA in ItemData.AsEnumerable()
select ItemA.Item("Name"), ItemA.Item("Email")
so it knows what members to define. There's no way to get around it, the information has to logically be there for the anonymous type to be defined.
Depending on why exactly your are trying to do this, there are some options.
If you want intellisense while still encapsulating your data access, you can return xml instead of a datatable from your encapsulated data access class. (You can convert data tables to xml very easily. You'll want to use the new System.Xml.Linq classes like the XElement. They're great!) Then you can use VS2008's ability to create an xsd schema from xml. Then use/import that schema at the top of your code page, and you have intellisense.
If you have to have an object an with properties for your data, but don't want to define a class/structure for them, you'll love the new dynamic objects coming in C#4.0/VB10. You have object properties based on what the sql returns, but you won't have intellisense. There is also a performance cost to this, but (a) that might not matter for your situation and (b) it actually is not so bad in some situations.
If you're just trying to avoid making a lot of classes, consider defining structs/structures on the same code file, beneath your class definition. When you add more columns to your result set, it's easy to adjust a struct with more public fields.
In short you can have any two of the following three: (a) dynamic, (b) strontly-typed objects, (3) intellisense. But not all three.
There is one way to accomplish what you want, but it required knowledge of dynamic linq. You would build the query during run-time and then use it. I am no expert and have never really played around with it, but here is a link to Scott Guthrie's blog about it - Dynamic Linq. Hope that helps.
Wade