实体框架 4.1 实体的松耦合

发布于 2024-12-20 03:17:27 字数 1491 浏览 2 评论 0原文

如果有人能对此有所了解,请提供一些帮助。

我创建了一个代码优先的 MVC 3 应用程序,并且运行良好。我现在正在重构以尽可能多地消除耦合,因为我希望域模型稍后能在各种其他 MVC 3 应用程序中使用。我现在拥有的是通过规范化数据库持久保存的实体集合,并通过存储库模式对它们进行 CRUD 编辑。我已使用 Ninject 通过控制器的构造函数来 DI 存储库,并使用 MVC 3 项目中的模型充当 DAO。

因此,在域内,我有一个名为 Case 的实体,它具有另一个案例 Client 的外键,如下所示:

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; }
}

然后我有一个接口(该接口主要用于将其实现到视图模型以添加我的数据注释 - 我知道我可以将注释添加到域对象,但正如我所说,我想在其他应用程序中使用此域模型,这些应用程序将具有不同的通用语言,

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

然后我就有了我的 。 MVC 3 项目中的视图模型

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; }    
}

。我的第一个问题是:将 Client 的外键引用更改为 IClient 是一个新事物,当类型是它返回的具体类时,它返回一个 null 对象。很好 - 我认为这是因为 EF4.1 尝试创建 IClient 的实例,我在这里完全错了还是有办法解决这个问题

?通过向视图模型添加数据注释,我是否也做错了什么继承我的域实体的接口?我应该使用模型元数据吗?如果是这样,我如何使用元数据,使数据注释对于每个项目都是唯一的,而不触及域?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2024-12-27 03:17:27

警告:我不是 EF 或 MVC3 方面的专家。

我们正在构建 EF Code First 实体,并且不打算向实体添加接口。存储库获取接口。工作单元获取接口。实体则不然。存储库返回具体实体,即 POCO。实体可以耦合到相关实体。模型和其他类通常会注入存储库接口和/或工作单元接口。为了测试,我们将新建一些 POCO 实体并从模拟存储库返回它们。

我们计划将相关 POCO 属性虚拟化,以便 EF 可以创建代理。

如果您想将视图与具体实体解耦,我首先会问您期望从中获得什么价值。该视图是否会被不同的实体重用?如果是这样,一种选择是使用 AutoMapper 之类的东西来复制属性。不过,您必须了解延迟加载属性的立即访问。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文