开发者

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 Milest开发者_StackOverflowoneServiceMock()
        {

        }

        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.


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.


The interface implementation is to be added to the T4 template while modifying it. It is not shown in the screenshots but it is mentioned as item 13 that the user has to implement the IValidate interface in the T4 template. Once the implementation is provide, the generated classes would have those implementations too.

User: notes that the generated classes (under the .tt file) implement the IValidate interface as expected


What about making a base class (outside the EntityModel) that all entities derive from (a simple modification in the T4 template file) and implementing the interface in the base class once? It works perfect for me.


It was a while ago, but I did this using a T4 template... see this post: EntityFramework trigger like Auditing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜