内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

[Architect]Abp框架原理解析(5)UnitOfWork

本節(jié)目錄

成都創(chuàng)新互聯(lián)專注于田家庵企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城開發(fā)。田家庵網(wǎng)站建設(shè)公司,為田家庵等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

  • 介紹

  • 分析Abp源碼

  • 實(shí)現(xiàn)UOW

 

介紹

UOW(全稱UnitOfWork)是指工作單元.

在Abp中,工作單元對(duì)于倉儲(chǔ)和應(yīng)用服務(wù)方法默認(rèn)開啟。并在一次請(qǐng)求中,共享同一個(gè)工作單元.

同時(shí)在Abp中,不僅支持同一個(gè)數(shù)據(jù)庫連接,還支持事務(wù)處理.

 

分析Abp源碼

1.UnitOfWorkRegistrar

[Architect] Abp 框架原理解析(5) UnitOfWork

 

2.ComponentRegistered

[Architect] Abp 框架原理解析(5) UnitOfWork

 

 

3.IsConventionalUowClass

[Architect] Abp 框架原理解析(5) UnitOfWork

 

 

4.Intercept

[Architect] Abp 框架原理解析(5) UnitOfWork

 

 

5.PerformSyncUow

 [Architect] Abp 框架原理解析(5) UnitOfWork

 

 

實(shí)現(xiàn)UOW

定義IUnitOfWork

1

2

3

4

5

6

7

8

9

10

11

12

public interface IUnitOfWork

{

    //1.開啟事務(wù)

    //2.設(shè)置Filter(本例中不做演示)

    void Begin(UnitOfWorkOptions options);

    void Complete();

}

public class UnitOfWorkOptions

{

    public bool? IsTransactional { getset; }

}

 

實(shí)現(xiàn)uow,在uow中會(huì)提供db的創(chuàng)建,這樣才能管理到每個(gè)db.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

public class EfUnitOfWork : UnitOfWorkBase

{

    public static DbContext DbContext { getset; }

    public static DbContext GetDbContext()

    {

        if (DbContext == null)

        {

            DbContext = new DemoDb();

        }

        return DbContext;

    }

    public override void Begin(UnitOfWorkOptions options)

    {

        if (options.IsTransactional == true)

        {

            CurrentTransaction = new TransactionScope();

        }

    }

    public TransactionScope CurrentTransaction { getset; }

    public override void Complete()

    {

        SaveChanges();

        if (CurrentTransaction != null)

        {

            CurrentTransaction.Complete();

        }

    }

    private void SaveChanges()

    {

        DbContext.SaveChanges();

    }

}

public abstract class UnitOfWorkBase : IUnitOfWork

{

    public virtual void Begin(UnitOfWorkOptions options)

    {

    }

    public virtual void Complete()

    {

    }

}

 

定義與實(shí)現(xiàn)倉儲(chǔ)層,這里不再做DbProvider.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public interface IRepository

{

}

public interface ITaskRepository : IRepository

{

    void Add(Task task);

}

public class TaskRepository : ITaskRepository

{

    public void Add(Task task)

    {

        var db = (DemoDb)EfUnitOfWork.GetDbContext();

        db.Tasks.Add(task);

    }

}

 

定義與實(shí)現(xiàn)應(yīng)用層

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

public interface IApplicationService

{

}

public interface ITaskAppService : IApplicationService

{

    void CreateTask(CreateTaskInput input);

}

public class TaskAppService : ITaskAppService

{

    private ITaskRepository _repository;

    public TaskAppService(ITaskRepository repository)

    {

        _repository = repository;

    }

    public void CreateTask(CreateTaskInput input)

    {

        _repository.Add(new Task(input.Name));

    }

}

public class CreateTaskInput

{

    public string Name { getset; }

}

 

定義與實(shí)現(xiàn)uow攔截器

1

2

3

4

5

6

7

8

9

10

11

12

13

14

internal class UnitOfWorkInterceptor : IInterceptor

{

    private IUnitOfWork _unitOfWork;

    public UnitOfWorkInterceptor(IUnitOfWork unitOfWork)

    {

        _unitOfWork = unitOfWork;

    }

    public void Intercept(IInvocation invocation)

    {

        _unitOfWork.Begin(new UnitOfWorkOptions());

        invocation.Proceed();

        _unitOfWork.Complete();

    }

}

 

定義在IApplicationService與IRepository接口下攔截

1

2

3

4

5

6

7

8

static void Kernel_ComponentRegistered(string key, Castle.MicroKernel.IHandler handler)

{

    var type = handler.ComponentModel.Implementation;

    if (typeof(IApplicationService).IsAssignableFrom(type) || typeof(IRepository).IsAssignableFrom(type))

    {

        handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));

    }

}

 

執(zhí)行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

static void Main(string[] args)

{

    using (var container = new WindsorContainer())

    {

        container.Register(Component.For<IInterceptor, UnitOfWorkInterceptor>());//先注入攔截器

        container.Register(Component.For<IUnitOfWork, EfUnitOfWork>());

        container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;

        container.Register(Component.For<ITaskAppService, TaskAppService>());

        container.Register(Component.For<ITaskRepository, TaskRepository>());

        var person = container.Resolve<ITaskAppService>();

        person.CreateTask(new CreateTaskInput() { Name = "3333" });

    }

    Console.ReadKey();

}

 

會(huì)自動(dòng)在application method的結(jié)尾調(diào)用Complete.

另外也可以在uow上定義option為啟用事務(wù).在本例中稍作擴(kuò)展即可實(shí)現(xiàn).

新聞名稱:[Architect]Abp框架原理解析(5)UnitOfWork
標(biāo)題鏈接:http://m.rwnh.cn/article32/gspcsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)小程序開發(fā)、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
富裕县| 邛崃市| 光山县| 宁化县| 福州市| 汽车| 开远市| 枝江市| 封丘县| 莱西市| 平度市| 大兴区| 琼结县| 金塔县| 天长市| 湄潭县| 巴林左旗| 阜南县| 霍城县| 镇巴县| 河津市| 新营市| 略阳县| 藁城市| 逊克县| 忻城县| 泸州市| 若羌县| 辽中县| 永年县| 如东县| 清徐县| 靖宇县| 南雄市| 哈巴河县| 马鞍山市| 沙坪坝区| 耿马| 汕尾市| 东丽区| 洛南县|