I have two interfaces:
public interface I1
{
A MyProperty { get; set; }
}
public interface I2 : I1
{
new B MyProperty { get; set; }
}
In C# I can explicitly implement like this:
public class C : I1, I2
{
public B MyProperty { get; set; }
A I1.MyProperty { get; set; }
}
Somehow I have to use these interfaces in a c++/cli project. So, how can I implement this in c++/cli?
Thanks in advance.
I solved it myself. It should be:
public ref class C : I1, I2
{
public:
virtual property B^ MyProperty
{
B^ get() { ... }
void set(B^ value) { ... }
}
protected:
virtual property A^ DummyProperty
{
A^ get() = I1::MyProperty::get { return nullptr; }
void set(A^ value) = I1::MyProperty::set { }
}
}
Related
I have a xaml file that needs to convert into objects, Is there anyone done this before?
using (var stream = File.OpenRead(filename)) {
var yourObj = XamlReader.Load(stream);
}
//Configuration Class
namespace SKAT.Postfordeler.Shared.DataTypes
{
[Serializable]
public class PostFordelerConfiguration
{
private readonly ReceiverAddressList _receiverAddresses;
private readonly DocumentTypeList _documentTypes;
public PostFordelerConfiguration()
{
_receiverAddresses = new ReceiverAddressList();// I don't want to implement like this.
_documentTypes = new DocumentTypeList(); //// I don't want to implement like this.
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ReceiverAddressList ReceiverAddresses
{
get { return _receiverAddresses; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DocumentTypeList DocumentTypes { get {return _documentTypes;} }
public static PostFordelerConfiguration Load(string location)
{
return (PostFordelerConfiguration)XamlReader.Load(new XmlTextReader(location));
}
}
}
//Document Entity
namespace SKAT.Postfordeler.Shared.DataTypes
{
[Serializable]
public class DocumentType
{
public String Id { get; set; }
}
}
//Document List
namespace SKAT.Postfordeler.Shared.DataTypes
{
[Serializable]
public class DocumentTypeList : List<DocumentType>{ }
}
//ReceiverAddress Entities
namespace SKAT.Postfordeler.Shared.DataTypes
{
[Serializable]
public class ReceiverAddress
{
public String Id { get; set; }
public String Routable { get; set; }
public String Description { get; set; }
}
}
//ReceiverAddress List
namespace SKAT.Postfordeler.Shared.DataTypes
{
[Serializable]
public class ReceiverAddressList : List<ReceiverAddress>{ }
}
// Load XAML file and Convert into objects
SKAT.Postfordeler.Shared.DataTypes.PostFordelerConfiguration loader =
Postfordeler.Shared.DataTypes.PostFordelerConfiguration.Load(
#"D:\projects\skatpostfordeler\SKAT.Postfordeler.Client.UI\PostfordelerConfiguration.xaml");
Is it possible to specify at runtime the sub-types of a specific abstract contract?
In the classic WCF/DataContract we have the KnownTypeAttribute and its constructor accepting a string representing the name of static function to invoke to get a set of Type:s.
[DataContract]
[KnownType("GetTypes")]
public abstract class AbstractContract
{
[DataMember] public int Prop1 { get; set; }
[DataMember] public string Prop2 { get; set; }
static IEnumerable<Type> GetTypes()
{
var list = new List<Type>();
list.Add(typeof(ConcreteContract1));
list.Add(typeof(ConcreteContract2));
return list;
}
}
[DataContract]
public class ConcreteContract1 : AbstractContract
{
[DataMember] public int Prop3 { get; set; }
}
[DataContract]
public class ConcreteContract2 : AbstractContract
{
[DataMember] public bool Prop3 { get; set; }
}
Is this scenario supported?
The scenario with GetTypes() isn't supported, partly due to how to v1 handles the generation/caching -however, in v2 (preview available) this is supoortable:
using System;
using System.Runtime.Serialization;
using ProtoBuf.Meta;
class Program
{
static void Main()
{
var model = TypeModel.Create();
var abst = model.Add(typeof(AbstractContract), true);
// define inheritance here...
abst.AddSubType(10, typeof(ConcreteContract1));
abst.AddSubType(11, typeof(ConcreteContract2));
model.CompileInPlace();
AbstractContract foo = new ConcreteContract1 { Prop1 = 123, Prop2 = "abc", Prop3 = 456 };
AbstractContract bar = (AbstractContract)model.DeepClone(foo);
Console.WriteLine(bar.Prop1);
Console.WriteLine(bar.Prop2);
Console.WriteLine(((ConcreteContract1)bar).Prop3);
}
}
[DataContract]
public abstract class AbstractContract
{
[DataMember(Order=1)]
public int Prop1 { get; set; }
[DataMember(Order=2)]
public string Prop2 { get; set; }
}
[DataContract]
public class ConcreteContract1 : AbstractContract
{
[DataMember(Order=1)]
public int Prop3 { get; set; }
}
[DataContract]
public class ConcreteContract2 : AbstractContract
{
[DataMember(Order=1)]
public bool Prop3 { get; set; }
}
Actually, with this approach you can take away all the attributes if you want (telling it explicitly instead). Note: you should cache and re-use the compiled model as far as possible - it is thread-safe, but generating it each time will be a bit more expensive.
I am having an issue with using Fluent NHibernate automapping with Inheritance. Below is my entity setup (abbreviated for simplicity). I have configured Fluent NHibernate to create 1 class for the hierarchy with a discriminator column. The automapping appears to be working correctly as when I generate a database, one table is created named "AddressBase" with a discriminator column that signals what type of address each row is.
The problem lies in the face that when I call the method "GetPrimaryBillingAddress()" on the UserAccount class, instead of just querying Billing Addresses, NHibernate is creating a query that looks at both Billing and Shipping Addresses. It doesn't take into account the discriminator at all. I am assuming there is some sort of configuration I can set but have not been able to find anything.
public abstract class AddressBase : ActiveRecord<AddressBase>
{
public virtual long Id { get; set; }
public virtual string Address1 { get; set; }
}
public class AddressBilling : AddressBase
{
public class TypedQuery : ActiveRecordQuery<AddressBilling> { }
public virtual bool IsPrimary { get; set; }
}
public class AddressShipping : AddressBase
{
public class TypedQuery : ActiveRecordQuery<AddressShipping> { }
[Display(Name = "Is Primary")]
public virtual bool IsPrimary { get; set; }
}
public class UserAccount : ActiveRecord<UserAccount>
{
public virtual long Id { get; set; }
public virtual IList<AddressBilling> BillingAddresses { get; set; }
public virtual IList<AddressShipping> ShippingAddresses { get; set; }
public UserAccount()
{
BillingAddresses = new List<AddressBilling>();
ShippingAddresses = new List<AddressShipping>();
}
public virtual AddressBilling GetPrimaryBillingAddress()
{
if (BillingAddresses.Any(x => x.IsPrimary))
{
return BillingAddresses.Single(x => x.IsPrimary);
}
return BillingAddresses.FirstOrDefault();
}
public virtual AddressShipping GetPrimaryShippingAddress()
{
if (ShippingAddresses.Any(x => x.IsPrimary)) {
return ShippingAddresses.Single(x => x.IsPrimary);
}
return ShippingAddresses.FirstOrDefault();
}
}
UPDATE:
Here is the Mapping override functions used in the automapping:
private static FluentConfiguration GetFluentConfiguration(string connectionStringName = "CS")
{
var autoMapping = AutoMap
.AssemblyOf<Product>(new Mapping.AutoMappingConfiguration())
.Conventions.Setup(c =>
{
c.Add<Mapping.ForeignKeyConvention>();
c.Add<Mapping.DiscriminatorConvention>();
})
.IgnoreBase<AddressBilling.TypedQuery>()
.IgnoreBase<AddressShipping.TypedQuery>()
.IncludeBase<AddressBase>();
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey(connectionStringName)))
.Mappings(m => m.AutoMappings.Add(autoMapping));
}
public class AutoMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
var isStatic = type.IsAbstract && type.IsSealed;
return type.Namespace == typeof(Entities.Product).Namespace && !isStatic;
}
public override bool IsDiscriminated(Type type)
{
if (type == (typeof(Entities.AddressBase))) {
return true;
}
return false;
}
public override string GetDiscriminatorColumn(Type type)
{
return "Type";
}
public class DiscriminatorConvention : ISubclassConvention
{
public void Apply(ISubclassInstance instance)
{
//Address
if (instance.Name == typeof(AddressBilling).AssemblyQualifiedName)
{
instance.DiscriminatorValue(Enums.AddressType.BillingAddress);
}
else if (instance.Name == typeof(AddressShipping).AssemblyQualifiedName)
{
instance.DiscriminatorValue(Enums.AddressType.ShippingAddress);
}
}
}
Thanks!
Please, try to change your class UserAccount like this:
public class UserAccount : ActiveRecord<UserAccount>
{
public virtual IList<AddressBase> Addresses { get; set; }
public virtual IList<AddressBilling> BillingAddresses { get {return this.Addresses.OfType<AddressBilling>();} }
public virtual IList<AddressShipping> ShippingAddresses { get {return this.Addresses.OfType<AddressShipping>();} }
// ...
}
Of course, only Addresses property should be mapped here.
The below code makes ccrewrite blow up! Ideas?
BTW, If you comment out the ActualClass, ccrewrite succeeds...
[ContractClass(typeof(TestContracts))]
interface ITestInterface
{
bool IsStarted { get; set; }
void Begin();
}
class ActualClass : ITestInterface
{
public bool IsStarted { get; set; }
public void Begin()
{
this.IsStarted = true;
}
}
[ContractClassFor(typeof(ITestInterface))]
class TestContracts : ITestInterface
{
ITestInterface Current { get; set; }
private TestContracts()
{
Current = this;
}
#region ITestInterface Members
bool ITestInterface.IsStarted
{
get; set;
}
void ITestInterface.Begin()
{
Contract.Requires(!Current.IsStarted);
Contract.Ensures(Current.IsStarted);
}
Thanks in advance!
Well, serves me right for not reading Jon Skeet well enough ;) The bit about how rewriter takes the contracts and puts them in your actual class...
[ContractClassFor(typeof(ITestInterface))]
class TestContracts : ITestInterface
{
private TestContracts()
{
}
#region ITestInterface Members
bool ITestInterface.IsStarted
{
get; set;
}
void ITestInterface.Begin()
{
ITestInterface iface = this;
Contract.Requires(!iface.IsStarted);
Contract.Ensures(iface.IsStarted);
}
}
http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/853227bf-56e6-427b-8e9e-162c129e87ce/
Say I have a class like this:
public class MyClass
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string String1 { get; set; }
public string String2 { get; set; }
public string String3 { get; set; }
public string String4 { get; set; }
}
Is it possible to get NHibernate to store it in the following schema?
CREATE TABLE [dbo].[MyClass](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Xml] [varchar](max) NOT NULL,
)
Where the Id maps to Id, but then all other fields get serialized into XML (or otherwise)? I don't mind if these other fields have to go on a child object like the below, if that helps:
public class MyClass
{
public int Id { get; set; }
public AllOtherOptions Options { get; set; }
}
public class AllOtherOptions
{
public DateTime Date { get; set; }
public string String1 { get; set; }
public string String2 { get; set; }
public string String3 { get; set; }
public string String4 { get; set; }
}
I am thinking about doing something similar for an upcoming project. The project requires collecting a lot of data but only a few elements need to be stored in a relational database. I haven't started experimenting but these are my thoughts so far.
You can map an XML data type by creating a type that implements IUserType. If the child class (AllOtherOptions) is serializable, you should be able to map the XML field as a private member in MyClass and serialize/deserialize AllOtherOptions as needed. You could either dynamically maintain the XML field (sounds like a lot of work) or create an interceptor to do it. My thinking is that MyClass would implement an interface such as
public interface IXmlObjectContainer
{
void SerializeChildObjects();
void DeSerializeChildObjects();
}
and the interceptor would call those methods as needed. That's a proof of concept idea. I would probably refine that by exposing pairs of xml fields and serializable objects to remove the work of serializing from IXmlObjectContainer implementers. Or maybe handle serialization through the XML field's get/set accessors.
More info:
Working with XML Fields in NHibernate
Another XML implementation of IUserType
I had the same idea to save object in XML column. My idea was other. I took code from links and changed it to generic IUserType implementation. So any field/prop which is [Serializable] can be saved in XML column.
public class XmlUserType<T> : IUserType where T : class
{
public new bool Equals(object x, object y)
{
return x == y;
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
if (names.Length != 1)
throw new InvalidOperationException("names array has more than one element. can't handle this!");
var val = rs[names[0]] as string;
if (string.IsNullOrWhiteSpace(val) == false)
{
return KRD.Common.GenericXmlSerialization.Deserialize<T>(val);
}
return null;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var parameter = (DbParameter)cmd.Parameters[index];
T toSave = value as T;
if (toSave != null)
{
parameter.Value = KRD.Common.GenericXmlSerialization.Serialize(toSave);
}
else
{
parameter.Value = DBNull.Value;
}
}
public object DeepCopy(object value)
{
T toCopy = value as T;
if (toCopy == null)
return null;
string serialized = KRD.Common.GenericXmlSerialization.Serialize(toCopy);
return KRD.Common.GenericXmlSerialization.Deserialize<T>(serialized);
}
public object Replace(object original, object target, object owner)
{
throw new NotImplementedException();
}
public object Assemble(object cached, object owner)
{
var str = cached as string;
if (string.IsNullOrWhiteSpace(str) == false)
{
return null;
}
return KRD.Common.GenericXmlSerialization.Deserialize<T>(str);
}
public object Disassemble(object value)
{
var toCache = value as T;
if (toCache != null)
{
return KRD.Common.GenericXmlSerialization.Serialize(toCache);
}
return null;
}
public SqlType[] SqlTypes
{
get
{
return new SqlType[] { new SqlXmlType() };
}
}
public Type ReturnedType
{
get { return typeof(XmlDocument); }
}
public bool IsMutable
{
get { return true; }
}
}
public class SqlXmlType : SqlType
{
public SqlXmlType()
: base(DbType.Xml)
{
}
}
Usage with FluentNHibernate:
public class MainObject
{
public int Id { get; set; }
public ObjectAsXml Data { get; set; }
}
public class ObjectAsXml
{
public string Name { get; set; }
public int Date { get; set; }
public ObjectAsXml OtherObject { get; set; }
}
private class MainObjectMap : ClassMap<MainObject>
{
public MainObjectMap()
{
Id(id => id.Id);
Map(m => m.Data).CustomType<XmlUserType<ObjectAsXml>>().Nullable();
}
}
Maybe it will help somebody.