🚀 C# Progress Academy - Become a job-ready C# and Angular web developer to land your dream developer role: bit.ly/45vbPUg
@learndevtech6 күн бұрын
❤ magnific. This is way underrated!
@maryamblri6 ай бұрын
I have 8 years of experience in programming and I found this tutorial awesome. great job! please cover more design pattern like UOW and factory design pattern.
@usmanfarooq3071 Жыл бұрын
What will happen if we have a complex business transaction where we are saving multiple entities in DB. How would we manage the SaveChanges method when we need a db autogenerated id of one entity as an input value for another entity in same transaction?
@TheMezanine Жыл бұрын
In case of multiples transactions, you might implement Unit Of Work on top of Repositories. The UoW will manage the DBContext and implement transactions: EXAMPLE: public class UnitOfWork : IUnitOfWork { private readonly AppContext _context; public IUserRepository Users { get; private set; } public IAddressRepository Addresses { get; private set; } public UnitOfWork(AppContext context) { _context = context; Users = new UserRepository(_context); Addresses = new AddressRepository(_context); } public UnitOfWork() : this(new AppContext()) { } public int Save() { return _context.SaveChanges(); } public void Dispose() { _context.Dispose(); } public IDatabaseTransaction BeginTransaction() { return new EntityDatabaseTransaction(_context); } } USAGE: using(var unitOfWork = new UnitOfWork()) using(var transaction = new unitOfWork.BeginTransaction()) { try { unitOfWork.Users.Add(new User(... User One ...)) unitOfWork.Save(); unitOfWork.Addresses(new Address(... Address For User One ...)) unitOfWork.Save(); unitOfWork.Users.Add(new User(... User Two...)) unitOfWork.Save(); unitOfWork.Addresses(new Address(... Address For User Two...)) unitOfWork.Save(); transaction.Commit(); } catch(Exception) { transaction.Rollback(); } }
@gregorydeclercq29904 ай бұрын
If you use controller-service-repository and handle the logic in the service. You (should) never get those issues
@Lonchanick7 ай бұрын
marvelous!
@luisgonzalez88774 ай бұрын
Excellent explanation, thank you a million. I noticed that you invoked the repository tier from the controller (presentation) tier. Is this a best practice? I believe that specific logic requires the presence of business logic; for instance, in your example, it could be for the update method...
@HenningBerggren17 күн бұрын
You could insert an application layer (often referred as use case layer) where you would implement all your business logic (critical and application logic), but this is merely for larger applications.
@kriskata76539 ай бұрын
Okay but what If we use services? I mean this repository pattern does the exact same thing as having a service. So I don't think this pattern makes sense in this case.