简体   繁体   English

具有存储库的域/模型对象的静态方法

[英]Static Method on Domain/Model Object with Repository

I have separate repository classes for each entity. 我为每个实体都有单独的存储库类。 Now I would like to implement few static methods on domain object which uses repository instance, but the drawback of this approach is that, I have to hold repository instance on the static variable(which not works well on web application) or I have to create new instance on each static method. 现在,我想在使用存储库实例的域对象上实现一些静态方法,但是这种方法的缺点是,我必须将存储库实例保存在静态变量上(不适用于Web应用程序),或者必须创建每个静态方法上的新实例。 Is there any better approach for this(ie to implement static method on domain class with repository instance)? 有没有更好的方法(即在具有存储库实例的域类上实现静态方法)? please share your ideas. 请分享您的想法。

Base repository: 基础资料库:

public abstract class AbstractRepository<TEntity> : IabstractRepository<TEntity>
    where TEntity : EntityObject
{
    protected CivilRegistryEntities civilContext;

    public AbstractRepository()
    {
        civilContext = CivilRegistryEntities.Instance; // Per HTTP request singletone.
    }

    // Other method implementation goes here.
}

Per entity repository: 每个实体存储库:

public class BirthReportRepository : AbstractRepository<BirthReport>
{

}

Domain/Entity/Model object: 域/实体/模型对象:

public partial class BirthReport
{
    //Not works well in web application.
    private static BirthReportRepository repository = new BirthReportRepository();

    public static BirthReport Method1()
    {
        return repository.SomeMethod();
    }

    public static BirthReport Method2()
    {
        return repository.SomeOtherMethod();
    }

    // Other methods(both static and instance) goes here.
}

Static mutable state is horrible. 静态可变状态是可怕的。 Period. 期。 The repository contains a context and a context contains tons of mutable states. 存储库包含一个上下文,并且上下文包含大量可变状态。 Besides that, contexts are supposed to have a short life cycle. 除此之外,上下文应该具有较短的生命周期。 And: I think that your domain classes should have nothing to do with repositories. 并且:我认为您的域应该与存储库无关。 This is pseudo encapsulation. 这是伪封装。 Instead, let domain services handle Method1, method2. 而是让域服务处理Method1,method2。

You may consider using an IoC container to inject a context into your repositories and a repository into your domain services. 您可以考虑使用IoC容器将上下文注入到存储库中,并将存储库注入到域服务中。 The IoC container could also handle the life cycles of contexts and repositories. IoC容器还可以处理上下文和存储库的生命周期。 For web, an instance per request is common. 对于Web,每个请求都有一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有存储库模式的域模型 - Domain Model with Repository Pattern 如何在我的域实体/模型中注入存储库 object? - How to inject Repository object in my Domain Entity/Model? MVC 存储库 - 域 Model 与实体 Model - MVC Repository - Domain Model vs Entity Model 如何基于传递给“编辑”操作方法的域模型参数更新域模型对象? - How to update the domain model object based on the domain model parameter passed to Edit action method? 当存在单独的数据模型时,如何从存储库返回域对象? - How do I return a domain object from the repository when there is a separate data model? 如何在存储库中的域模型上设置私有字段 - How to set private fields on a domain model in the repository 在MVC中查看模型对象的域模型对象 - Domain Model Object to View Model Object in MVC 如何使用带有DDD的通用存储库(域模型+持久性模型)? - How to use Generic Repository with DDD (Domain Model + Persistence Model)? 将 model object 传递给存储库方法 (_accountObj) 时出现错误 - I am getting error while passing model object to repository method(_accountObj ) 在模型绑定中使用静态方法 - Using a static method in Model binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM