views:

47

answers:

1

Hi everyone,

I'm using NHibernate + Castle.Windsor to add some behaviour to my entities. This means that NHibernate creates entities through Windsor. This means that I must have a default empty constructor so Windsor will be able to instantiate my entities. I don't like this for many reasons, the main one being that I dislike to have objects in an unstable state.

Is there any way I can instantiate an entity without a public parameterless constructor?

More info: I tried using a custom Activator for my entities but that doesn't work since Windsor throws an exception when it detects that there's no way to resolve dependencies, so it blows before using my activator.

Some code:

public class Product
{
    public string Name { get; set; }
    public Product(string name)
    {
        Name = name;
    }
    protected Product() { }
}

Let's suppose I have the above entity: If I try to resolve that entity through Windsor it throws an exception telling me that there are some dependencies that Product is waiting for some dependencies that weren't registered. If I use NHibernate alone, everything works correctly. So, basically I'm trying to emulate NHibernate behaviour in that scenario while resolving my entities through Windsor.

Thanks in advance,

Jorge Vargas.

+1  A: 

You don't need to have parameterless constructors in your NHibernate entities or write custom Windsor activators. See this article.

Mauricio Scheffer
I had already seen that article but I'm not facing that problem. I updated my question with more information, I hope you know a way to do what I'm asking. Thanks.
Jorge Vargas

related questions