Reputation: 4662
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?
Upvotes: 42
Views: 58842
Reputation: 21224
If you really need the abstraction offered by using interfaces then consider adding a domain layer to your application instead. The domain layer is meant to represent entities without the burden of persistence logic, which leads to a cleaner and more extensible architecture. (It's not clear that this is the OP's goal but it seems to be for other people who have discussed the same problem elsewhere.) This might be the only solution that doesn't introduce unintuitive constraints like the other solutions (explicit interface implementations, type casting problems...) If you go this route, you probably don't even need interfaces -- the domain class should be enough.
The end result could look like this in terms of the class/namespace structure:
namespace Domain.Entities // domain layer (not EF)
class MyDomainEntity
namespace DataAccess.Entities // peristence/EF entities
class MyDataAccessEntity // no relation to MyDomainEntity
namespace DataAccess.Entities.Mappers
class MyDataAccessEntityMapper // responsible for mapping MyDataAccessEntity to and from MyDomainEntity
This approach admittedly requires a lot more work. You will need 2 sets of entities (1 set for domain, 1 set for persistence) and classes to map between the domain and persistence/EF entities. The EF entities would only be used within the data access layer and the domain entities would be used in the higher levels of your application. This approach is usually only worth it for big apps or if there is a compelling reason. Otherwise, for small apps and apps whose persistence layer is not likely to change, there is likely less work and confusion if you keep using your EF entities.
However if you do go this route then you will see that working with domain (non-EF) entities in your app becomes easier, and keeping domain logic out of your EF entities makes the EF model easier to work with too.
Upvotes: 8
Reputation: 119
I had a simliar case and I solved it this way:
public class Order : IOrder
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<OrderItem> Items {get; set;} = new List<OrderItem>();
IEnumerable<IOrderItem> IOrder.Items
{
get { return Items; }
set { Items = value as List<OrderItem>; }
}
}
public class OrderItem: IOrderItem
{
public string Name {get; set;}
public decimal Price {get; set;}
}
Upvotes: 2
Reputation: 860
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
.
Upvotes: 11
Reputation: 11
I had this issue too on some on my models and not others and tried using the accepted answer. Then I dug a bit deeper as to what was different on the models that worked.
Fix was to change from using ICollection to use IEnumerable instead and the issues went away, so far.
This removed the need to use the below code in accepted answer:
public interface IParent<out TChild>
where TChild : IChild
{
ICollection<TChild> Children { get; set; }
and it became
public interface IParent
{
IEnumerable<IChild> Children { get; set; }
which is much simpler.
Upvotes: 1
Reputation: 96
After several sleepless nights I believe I have found a solution to this problem. I have tested this approach (a little) and it seems to work but it probably needs some more eyeballs to tear it apart and explain why this approach might break. I used FluentAPI to set up my database attributes instead of decorating the entity class properties. I've removed the virtual attribute from the entity class members (i prefer to use explicit includes instead of falling back on lazy loading for child entities). I've also renamed the example classes and properties a bit so that it is clearer to me. I'm assuming you are trying to express a one-to-many relationship between an entity and its details. You are trying to implement interfaces for your entities in the repository tier so that upper tiers are agnostic to the entity classes. Higher tiers are only aware of the interfaces not the entities themselves...
public interface IMyEntity
{
int EntityId { get; set; }
//children
ICollection<IMyDetailEntity> Details { get; set; }
}
public interface IMyDetailEntity
{
int DetailEntityId { get; set; }
int EntityId { get; set; }
int IntValue { get; set; }
//parent
IEntity Entity { get; set; }
}
public class MyEntity : IMyEntity
{
public int EntityId { get; set; }
private ICollection<IMyDetailEntity> _Details;
public ICollection<MyDetailEntity> Details {
get
{
if (_Details == null)
{
return null;
}
return _Details.Select(x => (MyDetailEntity) x).ToList();
}
set
{
_Details = value.Select(x => (IMyDetailEntity) x).ToList();
}
}
ICollection<IMyDetailEntity> IMyEntity.Details
{
get
{
return _Details;
}
set
{
_Details = value;
}
}
}
public class MyDetailEntity : IMyDetailEntity
{
public int DetailEntityId { get; set; }
public int EntityId { get; set; }
public int IntValue { get; set; }
private IMyEntity _Entity;
public MyEntity Entity
{
get
{
return (Entity)_Entity;
}
set
{
_Entity = (Entity)value;
}
}
IEntity IMyDetailEntity.Entity
{
get
{
return _Entity;
}
set
{
_Entity = value;
}
}
}
Upvotes: 5
Reputation: 37947
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
Upvotes: 21
Reputation: 6649
A workaround is to create a special implementation for each interface you want to use with Entity Framework, utilizing the adapter pattern:
// Entity Framework will recognize this because it is a concrete type
public class SecondLevelDomainRep: ISecondLevelDomain
{
private readonly ISecondLevelDomain _adaptee;
// For persisting into database
public SecondLevelDomainRep(ISecondLevelDomain adaptee)
{
_adaptee = adaptee;
}
// For retrieving data out of database
public SecondLevelDomainRep()
{
// Mapping to desired implementation
_adaptee = new SecondLevelDomain();
}
public ISecondLevelDomain Adaptee
{
get { return _adaptee; }
}
public string Id
{
get { return _adaptee.Id; }
set { _adaptee.Id = value; }
}
// ... whatever other members the interface defines
}
// Repositor is your DbContext
public void SubmitDomain(ISecondLevelDomain secondLevelDomain)
{
Repositor.SecondLevelDomainReps.Add(new SecondLevelDomainRep(secondLevelDomain));
Repositor.SaveChanges();
}
public IList<ISecondLevelDomain> RetrieveDomains()
{
return Repositor.SecondLevelDomainReps.Select(i => i.Adaptee).ToList();
}
For more complex interfaces/classes, you may get InvalidOperationException - see Conflicting changes with code first foreign key in Entity Framework for an implementation that works with such object hierarchies
Upvotes: 5