简体   繁体   English

数据仓库模式设计C#

[英]Data Repository Pattern Design C#

I have an entity class 我有一个实体课

public partial class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
}

and this entity 和这个实体

public partial class User
{
    public User()
    {
        this.Tokens = new HashSet<Token>();
    }

    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Token> Tokens { get; set; }
}

I have a repository pattern 我有一个存储库模式

public abstract class DataRepositoryBase<T> : IDataRepository<T>
       where T : class, new()
{
        protected abstract T AddEntity(T entity);

        protected abstract T UpdateEntity(T entity);

        protected abstract IEnumerable<T> GetEntities();

        protected abstract T GetEntity(int id);
}

How can I call repository using generic data repository using some thing like this.<T> just using generic interface not creating new class? 我该如何使用通用数据存储库来调用存储库,就像this.<T>仅使用通用接口而不创建新类? Thanks. 谢谢。

_datarepositorypattern.GetDataRepository<IProductRepository>();
_datarepositorypattern.GetDataRepository<IUserRepository>();

what i want, somthing like this. 我想要的是这样的东西。

var obj = _datarepositorypattern.GetDataRepository<IDataRepository<User>>(); 

//then i can access use
obj.GetEntities();

update I already created repository this repository can return 更新我已经创建的存储库,此存储库可以返回

public DataRepositoryBase<Product> ProductRepository => new ProductRepository();

in the service class 在服务等级

        private readonly UnitOfWork _unitOfWork;
        public ProductServices(UnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }



    Product[] IProductServices.GetProduct()
    {
        var repository = _unitOfWork.ProductRepository;

        return repository.Get().ToArray();
    }

i use this current repository method like this. 我使用这种当前的存储库方法。

what i wanted create dynamic repository so i do not updating my repository again only updating my service class with code like this. 我想要创建动态存储库,因此我不必再次更新存储库,而仅使用这样的代码更新服务类。

var obj = _datarepositorypattern.GetDataRepository<IProductRepository>(); 
obj.GetEntities();

just using repository repository pattern i can get dynamic repository product 仅使用存储库存储库模式,我就可以获得动态存储库产品

var obj = _datarepositorypattern.GetDataRepository<IUserRepository>(); 
obj.GetEntities();

just using repository repository pattern i can get dynamic repository User. 仅使用存储库存储库模式,我就可以获得动态存储库用户。

UPDATE I just googling this is an repositoryfactorypattern can anyone help me. 更新我只是谷歌搜索这是一个repositoryfactorypattern谁能帮助我。

as you've written it here you need you need new classes. 正如您在此处编写的一样,您需要新的课程。 You specify the IProductRepository and IUserRepository 您指定IProductRepository和IUserRepository

that means you need something like 这意味着您需要

class UserRepository : DataRepositoryBase<User>, IUserRepository
{
  //userrepository code here
}

otherwise you would get something like 否则你会得到类似

_datarepositorypattern.GetDataRepository<IDataRepository<User>>();

expecially since your datarepository class is abstract you need a concrete implementation somewhere 特别是因为您的datarepository类是抽象的,所以您需要在某个地方进行具体的实现

Update: example not using entity specific repositories 更新:不使用实体特定存储库的示例

If you really want to avoid entity specific repositories you can look at breeze. 如果您真的想避免特定于实体的存储库,则可以轻而易举地查看。

you still need something like theis 你仍然需要像泰西斯这样的东西

class MyContext : DbContext {
    DbSet<Product> Products {get; set;}
    DBSet<User> Users {get; set;}
}

but then you can call a context provider like this 但是您可以像这样调用上下文提供者

public class DataRepositoryBase<T> : IDataRepository<T> {
    readonly EFContextProvider<User> _contextProvider =
        new EFContextProvider<User>();


    public void SaveEntity(JObject saveBundle) {
       _contextProvider.SaveChanges(saveBundle);
    }

}

now you can just call new DataBaseObjectRepository() (or have that code in your GetDataRepository method) 现在您可以只调用新的DataBaseObjectRepository()(或在GetDataRepository方法中使用该代码)

maybe that is a place to start. 也许那是一个起点。 You do need something like DbContext that tells you how to map different types to different tables. 您确实需要像DbContext这样的东西,它告诉您如何将不同的类型映射到不同的表。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM