I agree with @Ladislav - Method_1 is a bad approach. Let the database raise exceptions that are caught by EF - don't try to swallow these exceptions yourself.
Being on the right track with method 1.
This is how I do it - as I also have a separate context (POCO, no change tracking, ASP.NET MVC).
BLL interface: (note that I have TPT in my model, so generics. "Post" is abstract)
void Add(Post post); void Update<TPost>(TPost post) where TPost : Post, new();
The new() constraint is crucial - you will immediately understand why.
I will not show how I do "Add", because it is just, as you think - AddObject(entity);
Upgrading is the hard part:
public class GenericRepository<T> : IRepository<T> where T : class { public void Update<T2>(T2 entity) where T2: class, new() { var stub = new T2();
And it works for me.
As for the entity key, I used attributes for my domain classes. An alternative (which I am going to move) is that all my domain objects implement an interface that indicates that all domain objects should have a property called EntityKey. Then I will use this interface for my limitations. Basically, I need a dynamic way to create stub objects in a shared repository.
I personally do not like the idea of ββ"checking the identifier, if its> 0, then this is an update." Since I work with ASP.NET MVC, if I (or another developer) forget to associate the ID with the view, it will not be passed, so even if it can be an update, because ID == 0 it will be added.
I like to talk about operations. This way I can add / update a separate validation logic.
RPM1984
source share