Question

I've got an ADO.NET background and its the first time I've used Entity Framework in a serious project so I've gone ahead and got VS2012 and am using .NET 4.5 and entity framework 5.

I'm taking the data first approach and have created the database, generated the .edmx and also seperated out the POCOS from the context using this method: http://allen-conway-dotnet.blogspot.co.uk/2013/01/separating-entity-framework-poco.html

So my solution looks like this:

Data.Access (where I access the EF context and return POCOS to Business Layer)
Data.Model (where the POCOs are)
Data.Repository (where the context sites)
Presentation.Admin.Website 

Now say I have the following table in the database / POCO class (just an example - not real)

        namespace Data.Model
            {
                using System;
                using System.Collections.Generic;

                public partial class Car
                {
                    public Car()
                    {
                       this.Parts= new HashSet<Parts>();
                    }

                    public int ID { get; set; }
                    public string Manufacturer { get; set; }
                    public string Model{ get; set; }
                    public Nullable<bool> Enabled { get; set; }

                   public virtual ICollection<Parts> Parts{ get; set; }
                }
            }

Now say all the cars in the database have an web service or even just a link to a URL which returns XML and gives me a list of available parts. The way in which each car retrieves the parts data is different (some are REST, some WCF, some from a CSV file, etc).

So I want to define classes that extend Car and define a "GetParts()" method which will have specific business logic to get parts for that particular car.

I also want to be able to get all cars from the database and loop them, calling the GetParts method for each one to retrieve the data.

I am thinking I need to define an interface or abstract class ICar declaring the GetParts method and somehow incorporating the Car POCO but getting confused about how to go about coding this.

Can anyone explain briefly how I can structure my code to get this done, perhaps suggesting a design pattern?

Was it helpful?

Solution

I'm taking the data first approach

The way in which each car retrieves the parts data is different (some are REST, some WCF, some from a CSV file, etc).

Considering your type of data store is variable and you presumably want a reusable model then I think the choice of using EF database first is not a good one for you. Better to go with code first.

So I want to define classes that extend Car and define a "GetParts()" method which will have specific business logic to get parts for that particular car.

Your model should be persistence ignorant. I would not consider extending or hardcoding a data store specific GetParts() if that's what you are after.

perhaps suggesting a design pattern?

Perhaps look into using a repository to provide a layer of abstraction over your data mapping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top