Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...