In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The contents of this section
Introduction
Analyze the Abp source code
Implement UOW
Introduction
UOW (full name UnitOfWork) refers to the unit of work.
In Abp, the unit of work is enabled by default for warehousing and application service methods. And share the same unit of work in a request.
At the same time, in Abp, not only supports the same database connection, but also supports transaction processing.
Analyze the Abp source code
1.UnitOfWorkRegistrar
2.ComponentRegistered
3.IsConventionalUowClass
4.Intercept
5.PerformSyncUow
Implement UOW
Define IUnitOfWork
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
Public interface IUnitOfWork
{
/ / 1. Open a transaction
/ / 2. Set Filter (no demonstration in this example)
Void Begin (UnitOfWorkOptions options)
Void Complete ()
}
Public class UnitOfWorkOptions
{
Public bool? IsTransactional {get; set;}
}
Implement uow, which provides the creation of db in uow, so that each db can be managed.
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
forty-five
forty-six
forty-seven
Public class EfUnitOfWork: UnitOfWorkBase
{
Public static DbContext DbContext {get; set;}
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 {get; set;}
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 ()
{
}
}
Define and implement the warehouse reservoir, DbProvider is no longer done here.
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
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)
}
}
Define and implement the application layer
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
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 {get; set;}
}
Define and implement uow interceptor
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
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 ()
}
}
Define intercept under the interface between IApplicationService and IRepository
one
two
three
four
five
six
seven
eight
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)
}
}
Execution
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
Static void Main (string [] args)
{
Using (var container = new WindsorContainer ())
{
Container.Register (Component.For ()); / / inject the interceptor first
Container.Register (Component.For ())
Container.Kernel.ComponentRegistered + = Kernel_ComponentRegistered
Container.Register (Component.For ())
Container.Register (Component.For ())
Var person = container.Resolve ()
Person.CreateTask (new CreateTaskInput () {Name = "3333"})
}
Console.ReadKey ()
}
Complete. Exe is automatically called at the end of the application method.
Alternatively, option can be defined as transaction-enabled on uow. It can be implemented with a little extension in this example.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.