第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# OOP:在基礎具體類中使用帶有類型參數(shù)的接口

C# OOP:在基礎具體類中使用帶有類型參數(shù)的接口

C#
拉風的咖菲貓 2021-12-25 16:56:17
我希望標題是對問題的描述,我在表述我要解決的問題時遇到了麻煩。我正在使用 .NET Core 2.1我在過去 5 年里使用 Python 后最近開始在 C# 商店工作,所以我的強類型 oop 有點生疏。這是我所擁有的以及我正在嘗試做的事情:我是一個基本存儲庫接口,它定義了數(shù)據(jù)庫實體的基本 CRUD 功能:public interface IRepository<TEntity, TPrimaryKey> where TEntity : class{   TEntity FindById(TPrimaryKey id)   void Create(TEntity entity);   void Delete (TEntity entity);}我從這個接口繼承來定義我的存儲庫類實現(xiàn)的接口:interface IProductRepository : IRepository<Product, int>{   void SomeProductRepoMethod(int someParam);}然后我在我的具體類中實現(xiàn)所有接口方法:public class ProductRepository : IProductRepository{   public Product FindById(int id)   {      // lookup   }   public void Create(Product product)   {      // save    }   public void SomeProductRepoMethod(int someParam)   {      // do product repository specific stuff   }   ....}現(xiàn)在,我想做的很簡單。我想補充的過載Create上IRepository接受一個IEnumerable的TEntity:public interface IRepository<TEntity, TPrimaryKey> where TEntity : class   ...   void Create(IEnumerable<TEntity> entities);   ...但是我想定義一次這個重載的實現(xiàn):所以我想我可以制作一個抽象的基本存儲庫類來放置上述實現(xiàn)。我面臨的問題是我不確定如何或者甚至我是否可以用我現(xiàn)在擁有的模型干凈利落地做到這一點。我試圖創(chuàng)建一個實現(xiàn) 的基類,但這意味著將類型參數(shù)傳遞給基類和接口:IRepositorypublic abstract class BaseRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>{    public abstract TEntity FindById(TPrimaryKey id);    public abstract void Create(TEntity entity);    public abstract void Delete(TEntity entity);    public void Create(IEnumerable<TEntity> entities)    {        foreach(TEntity entity in entities)        {            Create(entity);        }    }}然后在我的具體存儲庫中:public class ProductRepository : BaseRepository<Product, int>, IProductRepository{   public override Product FindById(int id)   {      // lookup   }   public override void Create(Product product)   {      // save    }   public void SomeProductRepoMethod(int someParam)   {      // do product repository specific stuff   }   ....}這對我來說并不安靜,因為我在IProductRepository和中都傳遞了相同類型的參數(shù)ProductRepository。我覺得我很接近但不在那里,我不確定這里的最佳實踐方法是什么。如果有人可以建議一種方法,我將非常感謝您的反饋。為這篇文章的長度道歉,但我覺得我需要清楚地描述我想要做什么。謝謝!
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻1836條經(jīng)驗 獲得超5個贊


在接口和抽象類中具有相同的類型參數(shù)并不是什么大問題。使用您的抽象類解決方案是可以的,除非您ProductRepository需要從其他類繼承。


實際上,對于您的抽象類,您的IRepository接口不再需要存在。只需處理一切BaseRepository!


這個問題的另一個解決方案是擴展方法。在靜態(tài)類中,你可以這樣寫:


public static void Create<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repo, IEnumerable<TEntity> entities) where TEntity : class {

    // do your foreach loop here

現(xiàn)在你可以IRepository像這樣在任何實例上調用這個方法:


repository.Create(...);


查看完整回答
反對 回復 2021-12-25
?
湖上湖

TA貢獻2003條經(jīng)驗 獲得超2個贊

這就是我要做的方式。我會打破之間的繼承IRepository和IProductRepository:


這是您的接口:


public interface IRepository<TEntity, TPrimaryKey> where TEntity : class

{

    TEntity FindById(TPrimaryKey id);

    void Create(TEntity entity);

    void Delete(TEntity entity);

    void Create(IEnumerable<TEntity> entities);

}


internal interface IProductRepository

{

    void SomeProductRepoMethod(int someParam);

}

然后讓你的基類繼承IRepository你所做的:


基類:


public abstract class BaseRepository<TEntity, TPrimaryKey> : 

    IRepository<TEntity, TPrimaryKey> where TEntity : class

{

    public abstract TEntity FindById(TPrimaryKey id);

    public abstract void Create(TEntity entity);

    public abstract void Delete(TEntity entity);


    public void Create(IEnumerable<TEntity> entities)

    {

        foreach (TEntity entity in entities)

        {

            Create(entity);

        }

    }

}

然后你派生你的基類并實現(xiàn)你的IProductRepository:


public class ProductRepository : BaseRepository<Product, int>, IProductRepository

{

    public override Product FindById(int id)

    {

        // find

    }


    public override void Create(Product product)

    {

        // save 

    }


    public void SomeProductRepoMethod(int someParam)

    {

        // do product repository specific stuff

    }


    public override void Delete(Product entity)

    {

        // delete

    }

}

我認為您的派生類作為Product存儲庫的特殊性是BaseRepository.


查看完整回答
反對 回復 2021-12-25
  • 2 回答
  • 0 關注
  • 212 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號