How to separate POCO layer from Entity Framework layer and still be able to add business logic to objects

StackOverflow https://stackoverflow.com/questions/19070433

Domanda

I've seen a lot of these types of questions on here but they are either unanswered, not quite the same question that I am asking or the detail I need.

I have included a screenshot of my project below for a high-level reference view of things.

  1. In my "Sample" solution, I currently have a project called Sample.Data.Model that contains an edmx file of my database-first model.
  2. Following the steps in this MSDN walkthrough, I then moved the auto-generated POCO classes into a separate project and called it Sample.Data.Entities.
  3. I then referenced the Sample.Data.Entities project in my Sample.Data.Model project and fixed all the usings so my project builds successfully.
  4. I will of course need to add to the POCO classes so I added a PartialClasses folder in the Sample.Data.Entities project for my partial classes.

And then this is where I get lost. In smaller (non n-tier projects) my edmx file and partial classes where in the same project so I could just add my class object related methods in my partial class files like this:

namespace Sample.Data.Entities
{
    public partial class User
    {
        public string FullName 
        { 
            get 
            { 
                return string.Format("{0} {1}", this.FirstName, this.LastName); 
            } 
        }
    }

    public User GetUser(int userID)
    {
        using (var dc = new ProntoEntities())
        {
            return (from u in dc.Users where u.ID == userID select u).SingleOrDefault();
        }
    }
}

And then in MVC project I could just do something like this...

User user = new User();
user = user.GetUser(1);

OR

User user = new User();
user.FirstName = "John";
user.LastName = "Smith";
user.Update();

However, I can't do that in this current setup because the partial classes know nothing about the Entities.

So, my question is, where in my current setup do I put the "queries" for data?

Also, if I'm going to make "ViewModels" for my MVC project, where should those go? I would usually put those in the same project as my partial classes and edmx file as well.

Click to view larger version of image below

enter image description here

È stato utile?

Soluzione

So, my question is, where in my current setup do I put the "queries" for data?

I think you should use the magical Repository Pattern.
For example: http://www.remondo.net/repository-pattern-example-csharp/

Also, if I'm going to make "ViewModels" for my MVC project, where should those go?

Since viewmodel contain data what are connected to their views, i believe you don't need to put together with the Entities or their partial classes. If i were you, i just put to the Model folder in your MVC project.

Altri suggerimenti

You might be interested in using the open source N-Tier Entity Framework which uses Entity Framework on server-side and generates the entire infrastructure for building an n-tier architecture based on WCF including an EF-like API on client-side. Have a look at the framework’s user guide and sample applications available for download on codeplex. The resulting solutions are highly customizable while providing guidance on where to put what.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top