Unable to insert new records using Entity Framework 6 + Code First + Castle Windsor

256 views Asked by At

I know this may be a simple question, but I'm pretty new to architecture and I want to do it right. So, thank you for your comprehension.

I'm also new to Castle Windsor and not used with Unit Of Work neither Repository patterns - And I even don't know if I need them to solve the problem I'm having at the moment.

What I'm trying to accomplish:

I have an interface called IDomain with just some properties and a concrete POCO class Domain that implements it.

IDomain interface:

public interface IDomain : IPersistentObject
{
    int DomainId { get; set; }

    string Name { get; set; }

    [LocalizedString]
    string ItemName { get; set; }

    [LocalizedString]
    string ItemDescription { get; set; }

    int ItemValue { get; set; }
}

Domain POCO Class:

public class Domain : AbstractDefault, IDomain, ILocalizedEntity
{
    public virtual int DomainId { get; set; }

    public virtual string Name { get; set; }

    [LocalizedString]
    public virtual string ItemName { get; set; }

    [LocalizedString]
    public virtual string ItemDescription { get; set; }

    public virtual int ItemValue { get; set; }
}

My DomainService.cs class does this:

    public void Insert(IDomain param)
    {
        using (var db = new DefaultContext())
        {
            new DomainValidation(new DbMetaData().GetMetaData, Settings.Default.CultureName).Validate(param);

            db.Domains.Add((Domain)param);
        }
    }

Another important info is that I'm using AOC, i.e., I'm intercepting method calls to my Domain class. See my Windsor container Installer:

public class Installers : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component
            .For<IInterceptor>()
            .ImplementedBy<Class1>()
            .Named("DomainInterceptor"))

            .Register(Component
            .For<IDomain>()
            .ImplementedBy<Domain>()
            .LifestyleTransient()
            .Interceptors(InterceptorReference.ForKey("DomainInterceptor")).Anywhere);
    }
}

In my Unit Test I am doing:

var domain = container.Resolve<IDomain>(); // Returns IDomainProxy, not IDomain
domain.Name = "MIN_MAX";
domain.ItemName = new LocalizedProperty("en-US", "Mininum").Serialize();
domain.ItemValue = (int)MinMax.Minimum;

new DomainService().Insert(domain); // If I try to cast by doing .Insert(domain as Domain), I get a null

But when my code reaches the ".Add(Domain)param)" (DomainService.cs) I get the error: "Unable to cast object of type 'Castle.Proxies.IDomainProxy' to type 'Model.Domain'."

Why am I getting this error and how am I supposed to fix it, considering that I do want to use IoC, Windsor, etc?

Best regards.

1

There are 1 answers

0
Marco Alves On

In those situations Castle Windsor creates new instances with a "proxy" interface. I ended up using AutoMapper in my Service class.