我为每个实体都有单独的存储库类。现在我想在使用存储库实例的域对象上实现一些静态方法,但这种方法的缺点是,我必须在静态变量上保存存储库实例(这在 Web 应用程序上效果不佳)或者我必须创建每个静态方法的新实例。有没有更好的方法(即在具有存储库实例的域类上实现静态方法)?请分享你的想法。
基础存储库:
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.
}
每个实体存储库:
public class BirthReportRepository : AbstractRepository<BirthReport>
{
}
域/实体/模型对象:
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.
}