Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Entity framework implement an interface

I want to use IoC with Entity framework and Ninject. I figure I need the Generated Entity classes to implement an interface, ICRUD. There's a walkthrough that shows how to force Entity framework to implement an interface. I followed the directions and my EntityObjectCodeGenerator.cs file indeed shows "ICrud", but doesn't implement the interface. I don't see any subclasses under EntityObjectCodeGenerator.tt as the article says I'm supposed to. I get error

'BugnetMvc.Models.BugNetEntities' does not implement interface member 'BugnetMvc.Services.ICrud.Update()'

UPDATE
The goal is to create a testable, extensible MVC-3 intranet utilizing entity framework that also supports strongly typed Views and partial views. From my small level of experience with Ninject thus far, I believe I need to overload my Controller's constructor with a service for the View itself (Assume CRUD methods available for each interface) and one for each partial view:

E.G.

public HomeController(HomeService homeCrudService, PartialViewService1 partialviewService) 

Update2
To be clear and hopefully help others, the code can be implemented as follows:

This is how one can extend Entity

namespace BugnetMvc.Models//ensure namespace matches entity {     public partial class Milestone : ICrud<Milestone>//Entity, note the CRUD generic.  This gives us a lot of flexibility working with Ninject     {         public bool Create()         {             throw new System.NotImplementedException();         }          public List<Milestone> Read()         {             var milestones = new List<Milestone>();              var result = from a in new BugNetEntities1().Milestones                             where a.MilestoneID >= 0                             select new { a.Milestone1 };              milestones = result.AsEnumerable()                                         .Select(o => new Models.Milestone                                         {                                             Milestone1 = o.Milestone1                                         }).ToList();             return milestones;         }          public bool Update()         {             throw new System.NotImplementedException();         }          public bool Delete()         {             throw new System.NotImplementedException();         }     } 

A sample Mock entity:

namespace BugnetMvc.Services {     public class MilestoneServiceMock : ICrud<MilestoneMock>     {         public MilestoneServiceMock()         {          }          public bool Create()         {             throw new System.NotImplementedException();         }          public bool Update()         {             throw new System.NotImplementedException();         }          public bool Delete()         {             throw new System.NotImplementedException();         }           List<MilestoneMock> ICrud<MilestoneMock>.Read()         {             //string[] mileStones = new string[14];             List<MilestoneMock> milestoneMocks = new List<MilestoneMock>();             milestoneMocks.Add(new MilestoneMock("New"));             milestoneMocks.Add(new MilestoneMock("Assessment"));             milestoneMocks.Add(new MilestoneMock("Pending Approval"));             milestoneMocks.Add(new MilestoneMock("Pending Start"));             milestoneMocks.Add(new MilestoneMock("Planning"));             milestoneMocks.Add(new MilestoneMock("Dev-In Process"));             milestoneMocks.Add(new MilestoneMock("Dev-Pending Approval to QA"));             milestoneMocks.Add(new MilestoneMock("Dev-Pending Move to QA"));             milestoneMocks.Add(new MilestoneMock("QA-In Process"));             milestoneMocks.Add(new MilestoneMock("QA-UAT"));             milestoneMocks.Add(new MilestoneMock("QA-Pending Approval to Prod"));             milestoneMocks.Add(new MilestoneMock("QA-Pending Move to Prod"));             milestoneMocks.Add(new MilestoneMock("On-Going"));             return milestoneMocks;         }     } } //Global.asax         internal class SiteModule : NinjectModule         {             public override void Load()             {                 bool MOCKDB = true;                 MOCKDB = false;                 if (MOCKDB)                 {                     //Set up ninject bindings here.                     Bind<ICrud<MilestoneMock>>().To<MilestoneServiceMock>();                     Bind<ICrud<Application>>().To<ApplicationService>();                 }                 else                 {                     //Set up ninject bindings here.                     Bind<ICrud<Milestone>>().To<Milestone>();                     Bind<ICrud<Application>>().To<ApplicationService>();                 }             }         } 

The need for Read(int Id), may arise, in which case a new interface using the same basic ideas above should do the trick. One could even update ICrud to pass the model type into the methods as well. There's plenty of options. This worked for me, thanks to Jon Skeet for his expert guidance.

like image 434
P.Brian.Mackey Avatar asked Feb 18 '11 17:02

P.Brian.Mackey


1 Answers

Presumably the generated entity classes are partial classes, correct?

If so, you can just add your own partial class files to specify the interfaces to be implemented - and to provide any actual implementation methods you need. I suspect that will be a lot simpler than changing what gets generated.

like image 173
Jon Skeet Avatar answered Oct 01 '22 16:10

Jon Skeet