Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class1 with some simple work which goes like this :
C#
public interface IClass1Repository {
        void insert(Class1 class1);
    }
    public class Class1
    {
        public object Id { get; set; }
        public string Name { get; set; }
    }
    public interface IClass1Service {
        void Insert(Class1 class1);
    }
    public class Class1Service : IClass1Service {
        public IClass1Repository _repo { get; set; }
        public Class1Service(IClass1Repository repo)
        {
            _repo = repo;
        }
        public void Insert(Class1 class1) {
            if (class1 == null) throw new Exception("class1 is null");
            if (string.IsNullOrWhiteSpace(class1.Name)) throw new Exception("class1 Name Cannot Be Null");
            _repo.insert(class1);
        }
    }


What I have tried:

later in the development, I had to add a second class witch have a reference to class1 which goes like this :
C#
public interface ISecondClassRepository
    {
        void insert(SecondClass secondClass);
    }
    public class SecondClass
    {
        public object Id { get; set; }
        public Class1 Class1 { get; set; }
        // Some Other Props

    }

    public class SecondClassService {
        ISecondClassRepository _repo;
        IClass1Service _class1Service;
        public SecondClassService(ISecondClassRepository repo,IClass1Service class1Service) {
            _repo = repo;
            _class1Service = class1Service;
        }

        public void Insert(SecondClass secondClass) {
            _class1Service.Insert(secondClass.Class1);
            _repo.insert(secondClass);
        }
        
    }

the problem with the above code is where to put the transaction?
if I skip class1Service.Insert Call and depend on SecondClassRepository to do the insert then how to check the logic and validations for Class1.
I feel there is something wrong with my approach but cannot figure it out.
Posted
Updated 22-Jun-20 4:10am
Comments
PIEBALDconsult 21-Jun-20 15:08pm    
Sparingly.

1 solution

I use kind of the same "things": A (generic) repository for each model type and a common UnitOfWork-implementation. I added another layer called "DataService" which has the methods to do combined repository access (and other things) in one UnitOfWork.
I see no chance for you to do that in the current structure. So as always in programming - just add another layer of abstraction ;)
 
Share this answer
 
Comments
M. Rawan 22-Jun-20 12:03pm    
I kinda stuck how to implement the UOF without using any framework every example I found on the internet uses EF to handle UOF and Repositories work, what if in the future I wanted to use MongoDB (which don't support transactions over different documents) or a remote HTTP API provider?
johannesnestler 23-Jun-20 9:25am    
Hi again - As I said - just abstract it away - I use an abstract IUnitOfWork-Interface to implement my dataservices against (with Methods to Commit, Rollback, Save etc.), and have concrete Implementations for EF (SQLTransactions), even if you never change your provider this can be useful to keep the dependencies contained (think about updating to a new version). But if you ask me how likely it is that you really switch from one provider to another - VERY UNLIKELY - if it's not a toy-project…. (I'm an architect for large systems with ~30 years experience - i can't remember any customer who just switched the provider without re-implementing. So: don't abstract if you don't have to! Keep the YAGNI-principle high! - just my 2c... Kind regards Johannes
M. Rawan 23-Jun-20 9:53am    
yesterday when I first read the answer I just didn't get it, now it seems pretty obvious especially after the further explanation, many thanks.
johannesnestler 23-Jun-20 10:21am    
you are welcome - good luck with your project!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900