-1

I am doing a home project in C#. My project includes EF. It has a database, an EF auto-generated entity class library with all the model classes in it, a repository tier and a presentation tier. I want the presentation tier to be independent from the data tier. I thought I made another one tier that contains the Entity interfaces so in the upper layers I could reference only the interface tier and still be able to pass data, without accidentaly modify the data or to be dependent from the data. However this doesnt work because the model generator class throws an exception that the properties are not mapped. Im confused right now. How could I achieve this? Is there any design pattern I should consider?

Thanks.

  • Could you show us what you have got and what exception exactly do you get? – Arthur Sep 30 '19 at 15:06
  • I looked the answer you provided for me. That is what I am doing, however I didnt make another partial class, rather I made the interface where I declared all my model properties. It builds fine but when I would like to iterate through the entities like this: ` var stuff = entityRepo.GetAll(); Console.WriteLine("entities:"); foreach (var item in stuff) { //something }` throws an error at the iteration saying: System.InvalidOperationException: 'The type 'AppInterfaces.IEntity' was not mapped. – zsoltTakacs Sep 30 '19 at 19:15
  • In a Layered Architecture higher layers may depend on lower layers. And you are always have a dependency on your data model. You can interpose an intermediate layer to minimize the scope of change in some cases, but that's really neither here nor there. You're presentation tier will never completely independent from the data tier. – David Browne - Microsoft Sep 30 '19 at 20:06

1 Answers1

0

Entity classes are generated as partial classes so that you could extend these classes with other members like methods, fields etc. What I would do is I would add another partial class in the Repository project that implements desired interface. This interface would be defined in your interface project. This way you can return interface from repository instead of specific class.

In you repository this is auto generated data model

    public partial class EntityClass
    {
        public int MyProperty { get; set; }

        public string String { get; set; }

        public bool Boolean { get; set; }
    }

Then in the same project you define partial class with the same name. Remember to keep exactly the same namespace here as you have for original auto generated model!

    public partial class EntityClass : IEntityClass
    {
        public int AnotherOne { get; set; }
    }

And in your interface project you could add something like this

    public interface IEntityClass
    {
        int MyProperty { get; set; }

        string String { get; set; }

        bool Boolean { get; set; }

        int AnotherOne { get; set; }
    }

Remember that this is very cumbersome approach because you have to remember to update your interface whenever you change your data model. Other way around that is commonly used is defining DTOs (Data Transfer Object).

Here someone asked similar question. Please read this.

Arthur
  • 181
  • 1
  • 10