Question

Need a little help please if anyone can shed some light on this.

I've created a code-first MVC 3 application which I have working fine. I'm refactoring now to remove as much coupling as possible as I want the domain model to be used in various other MVC 3 applications later on. What I have now is a collection of entities which are persisted via a normalised database and they are CRUD-ed through a repository pattern. I have used Ninject to DI the repositories via the controller's constructor and am using models within the MVC 3 project to act as DAOs.

So, within the domain I have an entity called Case that has a foreign key to another case Client that looks like this:

public class Case : ICase
{
    [Key]
    public int CaseId { get; set; }
    public string CaseName { get; set; }
    public DateTime DateCreated { get; set; }
    public IClient Client { get; set; }
}

Then I have an interface (the interface exists mainly to implement it to the view model to add my data annotations - I know I could add the annotations to the domain object but as I said I want to use this domain model in other applications which will have a different ubiquitious language.

public interface ICase
{
    int CaseId { get; set; }
    string CaseName { get; set; }
    DateTime DateCreated { get; set; }
    IClient Client { get; set; }
}

And then I have my view model within the MVC 3 project.

public class CaseModel : ICase
{
    [HiddenInput(DisplayValue = false)]
    int CaseId { get; set; }

    [Required(AllowEmptyStrings = false)]
    [MaxLength(100)]
    string CaseName { get; set; }

    [RegularExpression("")]
    DateTime DateCreated { get; set; }    

    IClient Client { get; set; }    
}

So, my first problem is this: changing my foreign key reference for Client to IClient is a new thing, and it returns a null object. When the type was a concrete class it returned fine - I assume this is because EF4.1 tries to create an instance of IClient. Am I totally wrong here or is there a way around this?

My second problem (which may negate my first problem) is am I also doing something wrong by adding data annotations to a view model inheriting the interface of my domain entity? Should I be using model meta data? If so, how do I use meta data in such a way that I can make the data annotations unique to each project without touching the domain?

Thanks!

Was it helpful?

Solution

Caveat: I'm not an expert on EF or MVC3.

We're in the process of building EF Code First entities, and we're not planning on adding interfaces to the entities. Repositories get interfaces. Units of Work get interfaces. Entities don't. Repositories return concrete entities, which are POCOs. Entities may be coupled to related entities. Models and other classes will typically get repository interfaces and/or unit of work interfaces injected in. For testing, we'll just new up some POCO entities and return them from the mock repositories.

We're planning to make the relevant POCO properties virtual so that EF can create proxies.

If you want to decouple a view from concrete entities, I'd first ask what value you expect to gain from that. Is the view going to be reused with different entities? If so, one option would be to use something like AutoMapper to copy the properties over. You'd have to be aware of the immediate access of lazy-load properties, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top