WCF - DataContract by copy/paste original code or by auto-generated code for shared abstract class?
[DataContract]
public abstract class FooBase
{
[DataMember]
public int Bar { get; set; }
}
That is a base class that I use as the base class for other classes that are also DataContracts. Here's the problem though...
In Proj1 I choose Add Service Reference... (MyService) and it generates the code for me, including the FooBase code. In Proj2 I choose Add Service Reference... (OtherService) and it does the same.
But, I want the Foo base class to be in it's own assembly th开发者_运维技巧at both projects can reference... so, is it better to:
Copy/paste the FooBase class into the other shared assembly as it is?
[DataContract]
public abstract class FooBase
{
[DataMember]
public int Bar { get; set; }
}
Or, copy/paste the generated code for the FooBase class into the other shared assembly?
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="FooBase", Namespace="http://schemas.datacontract.org/2004/07/MyNamespace")]
[System.SerializableAttribute()]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(MyNamespace.Proj1.TypeA))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(MyNamespace.Proj1.TypeB))]
public partial class FooBase : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private int BarField;
[global::System.ComponentModel.BrowsableAttribute(false)]
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public int Bar {
get {
return this.BarField;
}
set {
if ((this.BarField.Equals(value) != true)) {
this.BarField = value;
this.RaisePropertyChanged("Bar");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
You can have it in a shared library but when adding service reference make sure that shared library is referenced, and that in the Advanced settings of the add reference dialog, you have selected Reuse types in all referenced assemblies.
My preference would be using the non-generated code in the shared library, that is code with just data contracts.
精彩评论