Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use interface properties with CodeFirst

I have the following entities:

public interface IMyEntity {     [Key]     int Id { get; set; }     IMyDetail MyDetail { get; set; }     ICollection<IMyDetail> CollectionOfReferences { get; set; } }  public interface IMyDetail {     [Key]     int Id { get; set; }     int IntValue { get; set; } }  public class MyEntity : IMyEntity {     [Key]     public virtual int Id { get; set; }     public virtual IMyDetail MyDetail { get; set; }     public virtual ICollection<IMyDetail> CollectionOfReferences { get; set; } }  public class MyDetail : IMyDetail {     [Key]     public virtual int Id { get; set; }     public virtual int IntValue { get; set; } } 

I want to use EF CodeFirst to access the database and to create database schema. But CodeFirst doesn't allow to use interface types for relations between entities. Therefore it doesn't create relation between MyEntity and MyDetail. I can't change interfaces therefore I can't change the type of property to MyDetail instead of IMyDetail. But I know that the client of this model will use only one implementation of each interface.

I've found a workaround for properties of type IMyDetail. I can create a property of type MyDetail and explicitly implement property of interface:

    private MyDetail _myDetail;      public virtual MyDetail MyDetail     {         get         {             return this._myDetail;         }         set         {             this._myDetail = value;         }     }      IMyDetail IMyEntity.MyDetail     {         get         {             return this._myDetail;         }         set         {             this._myDetail = (MyDetail)value;         }     } 

It works fine. But this solution doesn't work with ICollection<IMyDetail> because I can't cast it to ICollection<MyDetail>.

Are there any solutions for this?

like image 609
Pavel Surmenok Avatar asked Mar 21 '12 13:03

Pavel Surmenok


People also ask

Can we use properties in interface?

An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain fields, auto-implemented properties. The following interface declares some basic functionalities for the file operations.

Can we have properties in interface C#?

Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members.

Should interfaces have properties?

Yes, An interface should define properties when it really in need. Please suppose that. There is a IUser interface that has defined a property "Name" then you can use it without worry about if the object didn't implement the property.


2 Answers

I've had the same problem and found a solution just like Nathan, but you can even take it one step further and have the properties named the same (here Extensions and IAddress.Extensions), by explicitly defining the interface:

public interface IAddress {     string Address { get; set; }     IEnumerable<IAddressExtension> Extensions { get; set; } }  public interface IAddressExtension {     string Key { get; set; }     string Value { set; } }  [Table("AddressExtensions")] public class AddressExtension : IAddressExtension {     [Key]     public string Id { get; set; }     public string Key { get; set; }     public string Value { get; set; } }  [Table("Addresses")] public class Address : IAddress {     [Key]     public string Id { get; set; }     public string Address { get; set; }      public IEnumerable<AddressExtension> Extensions { get; set; }      [NotMapped]     IEnumerable<IAddressExtension> IAddress.Extensions     {         get { return Extensions; }         set { Extensions = value as IEnumerable<AddressExtension>; }     } } 

Code First ignores the interface-property and uses the concrete class, while you can still access this class as an interface of IAddress.

like image 27
Sven Möhring Avatar answered Sep 26 '22 02:09

Sven Möhring


An imperfect solution is to just merge these interfaces you want to persist into base classes and break down the underlying objects with subclasses. EF does support this, and if you go with Table Per Hierarchy (the default), you can sort all of the underlying subclassed objects by a shared property using a regular LINQ query from EF instead of having to get crafty and do things like write raw SQL or get multiple lists into memory and sort the union without the help of the DB, like you would with Cel's solution of interfaces and adapters.

You could also take the child/parent types of interfaces as generics, so that when the implementer uses concrete classes in the Db they can mostly use your interfaces, but tell EF to use concrete classes:

public interface IParent<out TChild>     where TChild : IChild {     ICollection<TChild> Children { get; set; } 

Someone could create their Db classes like:

public class Parent : IParent<Child> . . . 

But still use them like:

IParent<IChild> parents = db.Parents.Include(p => p.Children).ToArray(); 

Because the generic is marked out, the generic is covariant and therefore can take anything that meets the generic's restrictions, including the above cast up the type tree to the IChild interface.

That said, if you really want to persist interfaces, the right answer is probably to use NHibernate: How to map an interface in nhibernate?

And some coders recommend you keep interfaces on entities in any ORM limited to a few shared properties, or risk misuse: Programming to interfaces while mapping with Fluent NHibernate

like image 164
Chris Moschini Avatar answered Sep 26 '22 02:09

Chris Moschini