开发者

N-Tier - responsbility location for insert vs update

I'm creating an application which is split into a Data Layer (using repository pattern & EF 4.0), a Business layer (POCO's with开发者_C百科 additional logic) and a Service Layer (which is exposed to the client using WCF).

When I'm saving a record to the database I need to check and see if I'm updating an existing record or inserting a new one. Where should that responsibility lie - in the Service Layer so that the lifetime of an object is explicitly managed, or in the Data Layer so that saving a record will implicitly determine what action to take.

All comments welcome - I can't decide myself!


My first thought is that I think this is something that Entity Framework should be able to figure out itself, but i'm not 100% sure about that.

Barring EF doing it for you, I would put this logic in the repository. You pass in say a Customer object to the repository like so: myRepository.Save(myCustomer);

and then in the Save method of the Customer, it checks if the customer has an ID assigned or maybe you can ask EF to track its state, and then do an Insert or Update. This way, regardless of where the Save gets called--web service, UI, etc. this logic is taken care of.


If there are any business rules that apply to saving business objects then the responsibility lies in business layer, otherwise the decision should be the responsibility of the day layer.

That you are using EF should be irrelevant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜