Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何在IRepository,Repository,IServices,Services 这种代码结构下使用工作单元模式 #159

Closed
landonzeng opened this issue Dec 18, 2019 · 6 comments

Comments

@landonzeng
Copy link

大神,新手请教一下,项目是工厂模式的,项目中使用FreeSql.Repository的时候不知道咋个创建IRepository,Repository了,好像直接在Services层就能直接用了,但是项目是IRepository,Repository,IServices,Services 这种代码结构的

@2881099
Copy link
Collaborator

2881099 commented Dec 18, 2019

FreeSql.Repository 是通用的仓储实现,里面有接口,如果延用到项目使用,有两种办法:

  1. 通用仓储,文档上的注入方式

  2. 自定义仓储类,继承 FreeSql.BaseRepositoy

public interface ITopicRrpository : IBasicRepository
{
   //定义其他方法
}

public class TopicRepository : BaseRepository<Topic, int>
{

   public TopicRepository(IFreeSql fsql) : base(fsql, null, null)
   {
   }
   //实现其他方法
}

手机整理的,大概是这样。

构造参数 IFreeSql 可以改成 IUnitOfWork ,后话……

@2881099
Copy link
Collaborator

2881099 commented Dec 18, 2019

第一种,意味着省略了原有的 Repository 层

第二种,意味着 Repsiroty 层会有很多基本空着的类

@landonzeng
Copy link
Author

landonzeng commented Dec 18, 2019

@2881099 目前我是用第二种方式实现的,但是在多库注入的时候就卡主了,不太清楚我要如何才能解决一个项目多库的问题,以下是我注入的方法:
https://github.com/landonzeng/EasyCore/blob/master/WebApi/appsettings.json

@2881099
Copy link
Collaborator

2881099 commented Dec 18, 2019

IFreeSql 多库目前使用

.new FreeSqlBuilder()
.Build<TMark>()

,他的返回类型是 IFreeSql<TMark>,这样ioc 中就可以区分多个 IFreeSql 了。

仓储层注入,参数是固定的 IFreeSql,所以看下能不能重新定义一个

class MyBaseRepository<TMark, TEntity, TKey> : FreeSql.BaseRepository<TEntity, TKey>
{
   public MyBaseRepository(IFreeSql<TMark> fsql) : base(fsql, null, null) {}
}

@pjy612
Copy link

pjy612 commented Dec 18, 2019

官方推荐注入方式
#44
本人尝试注入方式(可以改为原生DI,方法雷同)
#33

@landonzeng
Copy link
Author

@2881099 @pjy612 好的,多谢,我先试试,有不懂的再请教

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants