سؤال

I'm attempting to implement the factory pattern with Entity Framework, but Entity Framework seems to call the default ctor and setters of each member whether they are private, or not.

Is there any way to get Entity Framework to call my Object.Create() instead?

Edit:
This is a simplified example of my class. I need entity framework to call the static Create() instead of the ctor.

public class Foo
{
    public Name { get; set; }
    public static Foo Create()
    {
        var newFoo = new Foo();
        newFoo.Init();
        return newFoo;
    }
    public void Init()
    {
        Name = "You know, on second thought, I want the name to be this instead.";
    }
    private Foo()
    {
        Name = "Hello Stack Overflow!";
    }
}
هل كانت مفيدة؟

المحلول

First, you could instead try searching for how to resolve or Entities injection IoC or similar - as that's requested more often, and basically need the same solution.

And second, usually (but not necessarily) what you're trying to do suggests a bit awkward design and could paint you into problems later on - actually, to me it suggests that you should seek different approach. Entities, POCO are not designed (again normally, it doesn't mean there isn't a case that warrants it) to be used in such way, and there're ways to work around such use. Let the EF create and deal with them, and try to extract that initialization into some form of repository or something. My suggestion at least...

Entity constructor is invoked internally (whether private or not) and as far as I know, even the latest EF 6 doesn't provide means for that. EF 6 has the IDbDependencyResolver but doesn't help in your case.

But what you could do is to 'inject into the object initialization' process I think - also mentioned by Ladislav Mrmka in a similar context here IOC with Entity Framework

You need to obtain the ref to ObjectContext as in:

var objectContext = ((IObjectContextAdapter)db).ObjectContext;

...and then use the objectContext.ObjectMaterialized where the ObjectMaterializedEventArgs argument of the event has the Entity object.

You need to do additional casting there based on your type to get to the entity of yours.

I haven't worked with that for a while but might work what you need.

نصائح أخرى

The factory should only return an instance which adheres to an interface; thus returning an interface solely. Whether the instance was created by EF (and subsequently changing properties after creation but before being passed on) or a mocking framework will have no bearing to the consumer of the factory.

You can not do duck typing in generic method groups. And there is no such thing as static interface. So it is not really possible without creating some Factory class (instead of your static factory method) and passing instance of a factory to EF or some Resource-Locator or registry with all Factories it might need. However Entity Framework doesn't have such extensibility.

On the other hand, if you need such a control you might want to create your own mini framework on top of say dapper

//#1 Product Interface //#2 Factory Method interface //#3 Controller Class inherits from the Product and Method interface to produce your product
//#4 you can regenerate controller as many as business needs with different procedures inside Factory Method

//#5 create EF with a Subset of your Controller Class you need to record in DB //#6

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top