Updating my katana

28-Apr-2020 00:01

In fact, I won’t touch the repository interfaces at all. Actually, it wasn't there on my code but while I was writing the post, I wanted to add there. As for your suggestion, yes it can be dropped and even as you suggested, we can have one method which accepts an optional paramater that is type of x. With a 2nd method you'd end up needing to basically duplicate the above mock but forward the where predicate to Foo Repository. comment by scott gu near the bottom) Thanks though... I want to be able to make model classes any ole way I want and persist them with EF (with some generic implementation if possible) or any other persistence model/device be it NHibernate, XML or simple Db. @Chris IGeneric Repository is not specific to the EF.

What I did here first is to create an abstract class which implements IGeneric Reposity abstract class to access an instance of Db Context. IGeneric Repository would be defined in your Model somewhere.

I have exposed also exposed the inner Db Context in Generic Repository as a protected field (so that only concrete repository implementations have access to it, I really did not want the domain library user to have access to the context, too dangerous I think and what is the point of the repo in that case ;)).

@peluca Yep, I look around a bit on how to unit test with directly consuming Db Context and the only way is to use a database only for testing.

If the code you are writing to hit your repositories references the Interfaces and not the Implementations and using Dependency Injection it would be really simple to change from one implementation to another with a simple factory class or Structure Map etc... @Tugberk Thank you for the nice generic Base Repo class. Why did you not put the Get Single method to the other generic methods in the base repo class?

Just like this sample code: public T Get Single By Id(Expression @Peluca That is actually right. BTW, you can fork me or open an issue if you want : https://github.com/tugberkugurlu/Generic Repository @peluca I will include that as well but believe me, for a real word app you will definately find yourself implementing those repositories.

Indeed, there would be probably more repository classes for your project.

After playing like that for a while I decided to do something different which still sucked but better. I must accept that I could be doing something against OOP here which I have no idea what they are. Im used to using the "Using" statement to make the connections, and im not seeing this being done...

@peluca Yep, I look around a bit on how to unit test with directly consuming Db Context and the only way is to use a database only for testing.

If the code you are writing to hit your repositories references the Interfaces and not the Implementations and using Dependency Injection it would be really simple to change from one implementation to another with a simple factory class or Structure Map etc... @Tugberk Thank you for the nice generic Base Repo class. Why did you not put the Get Single method to the other generic methods in the base repo class?

Just like this sample code: public T Get Single By Id(Expression @Peluca That is actually right. BTW, you can fork me or open an issue if you want : https://github.com/tugberkugurlu/Generic Repository @peluca I will include that as well but believe me, for a real word app you will definately find yourself implementing those repositories.

Indeed, there would be probably more repository classes for your project.

After playing like that for a while I decided to do something different which still sucked but better. I must accept that I could be doing something against OOP here which I have no idea what they are. Im used to using the "Using" statement to make the connections, and im not seeing this being done...

I then realized that a single Db Context instance should be shared by all repositories, which is more than logical if you think about it, and correctly exposes the Db Context as a single Unit Of Work.