Asp.net INotifyPropertyChanged on domain objects

2019-03-05 04:56发布

What happens if a domain object in asp.net implements INotifyPropertyChanged and fires off the PropertyChanged event on the property setters?

I have a common domain layer for both a silverlight a WebForms system. For silverlight to get all the binding magic, the domain now does property notifications.

My webforms system doesn't seem to be affected (everything still works) but I am worried that something is happening behind the scenes that I don't understand.

I could make a silverlight specific domain layer that extends the original domain objects, and overrides the properties to do notifications, but is this necessary? i.e. am I adding an extra layer of code for no reason.

Polluting my domain with ComponentModel stuff does feel ugly.

2条回答
SAY GOODBYE
2楼-- · 2019-03-05 05:29

Nothing wrong will probably happen, but it is not the best approach..

I could make a silverlight specific domain layer that extends the original domain objects, and overrides the properties to do notifications, but is this necessary? i.e. am I adding an extra layer of code for no reason.

First thing to point out is that your UI in Silverlight should not be bound to Domain Objects. In fact, your views are bound to a ViewModel, and this viewmodel should contain Models.

UI Model != Domain Model

Polluting my domain with ComponentModel stuff does feel ugly.

If you add presentation logic in your Domain Models, your business logic layer can become a mess and you lose abstraction since it's coupled to the presentation layer. Just imagine that in 2 years you move to another web framework that needs special logic in the models, will you add more logic in the business objects?

In my opinion Domain Models should be isolated from other layers and should not be dependant on a technology (in this case, to Silverlight)

I would add a new layer that would transform your domain objects to Silverlight Models. This might look as an overhead, but this way your system will be clean and with clear isolated layers.

There are tools that can help you to do this mappings without much effort, for example AutoMapper

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-05 05:32

If nobody subscribes to the event, than nothing will happen as a result of raising it.

It is very unlikely that you serverside ASP.NET application would subscribe to those events, which explains why nothing happens.

查看更多
登录 后发表回答