Service Dependency and Property

Topics: Web Client Software Factory, UIP Application Block discussion, User Forum
Sep 20, 2007 at 10:28 AM
Hi All

I have two web forms using Service Dependency to access an object to do some operations.
If I have a property in this object and I modify this property value from one web form, will this update shown in another pages also?

regards
Alex

Sep 21, 2007 at 7:37 PM
Hi

I understood what you’re trying as if you want to persist changes in an entity so that they can be reflected in all of your webforms. What you can do is make a service which contains an object of that entity in order to persist it. So, when modifying that object, yes, changes will be reflected in other webforms.

Please let me know if this helps!

Ignacio Baumann Fonay
http://staff.southworks.net/blogs/ibaumann/
Sep 22, 2007 at 2:01 AM
Thanks.

But is this change affect your current session only or it will affect other concurrent user also?

Besides, if I don't want the variable shared by the other webforms but want to use Service Dependency, could I handle in this way?

regards
Alex
Sep 24, 2007 at 9:47 PM
Hi!

Answers inline. =)


hnchan wrote:
Thanks.

But is this change affect your current session only or it will affect other concurrent user also?


It will affect all sessions, as the service leaves in the application context, and does not involve sessions at all.


Besides, if I don't want the variable shared by the other webforms but want to use Service Dependency, could I handle in this way?

regards
Alex


In that case, you could use CreateNew instead of ServiceDependency, if what you are trying to do is make different instances of a class. That way, different webforms would have different instances, and, in consequence, modifications would only be shown in the webforms they were made. These of course will not be automatically persisted between postbacks.

If you do not want to specify the concrete type of the object to create (as you do with services) consider checking this post written by (Julian Dominguez) where he creates something called InterfaceDependency:
There is also some work on TypeMapping in WCSF vNext, that will let you inject a different type without the need of this new InterfaceDependency attribute, but it is not out yet.

Please let me know if this helps!

Ignacio Baumann Fonay
http://staff.southworks.net/blogs/ibaumann/
Sep 25, 2007 at 2:55 AM
Thank you very much!!!

BTW, One last question. Is there any big difference for the performance if we create the new object with CreateNew rather the get the service use ServiceDependency?

regards
Alex
Coordinator
Sep 25, 2007 at 7:09 PM


hnchan wrote:
BTW, One last question. Is there any big difference for the performance if we create the new object with CreateNew rather the get the service use ServiceDependency?


Great question.
CreateNew will create a new object on each request, which for things that are request specific, user specific, or session specific is the right thing to do. Of course, things are optimized to minimize the overhead, thanks to some work that ObjectBuilder and its strategies do.

ServiceDependencies live for the lifetime of the application and are shared across sessions/users/etc.

The idea is to use the right one for the right situation. For some things, you really want one object per request. For others, shared objects are OK and desired.

Michael Puleio - patterns & practices
Web – http://msdn.microsoft.com/practices/
Blog – http://blogs.msdn.com/mpuleio/
Sep 25, 2007 at 9:36 PM
Hi,

Just to clarify the differences with the CreateNew and ServiceDependency, here is an extract from the WCSF Help:

“When you examine the source code for a Composite Web Application Block application, you will notice that the application does not create instances of another component by directly constructing an object (using new). Instead, the source code for a Composite Web Application Block application contains attributes such as CreateNew and ServiceDependency.

These attributes support the injection of objects at run time. The Composite Web Application Block uses ObjectBuilder to create and inject instances of objects of the appropriate type into the application at run time. ObjectBuilder uses the factory pattern to create object instances. It supports a general-purpose attribute-based dependency injection. The attributes determine the type of object that the ObjectBuilder factory creates.”

[CreateNew]

The [CreateNew] attribute tells the dependency injection system to always create a new one of whatever it is you need. This is helpful for patterns such as Model-View-Controller (MVC) or Model-View-Presenter (MVP), where creating a view automatically generates a new controller/presenter.

For example:
[CreateNew]
public MyViewPresenter Presenter
{
  get { return _presenter; }
  set
  {
    _presenter = value;
    _presenter.View = this;
  }
}
The CreateNew attribute instructs ObjectBuilder to instantiate and initialize an instance of a MyViewPresenter when the MyView is created. When the property is set, the View property of the presenter is used to connect this implementation of the IMyView interface to the presenter (the View property is defined in the Presenter base class).

[ServiceDependency]

You can add the ServiceDependency attribute to a property in your class to obtain a reference to a service declaratively. The property specifies the type of service or interface you require, as shown in the following code. When this attribute is present, ObjectBuilder locates an instance of the service and passes back a reference to it. To locate the service, ObjectBuilder first looks in the current WorkItem. If the service is not found, ObjectBuilder then looks at the services in the parent WorkItem. This process continues until the service is located or ObjectBuilder reaches the root WorkItem. If the service is not found, ObjectBuilder throws an exception.

You can use a [ServiceDependency] attribute on a parameter of the class constructor to accept an injected reference and store it in a class-level variable. Or you can also add the ServiceDependency attribute to a property in your class that that is of the type of service or interface you require. You do this so that the dependency injection feature of the underlying ObjectBuilder utility will create an instance of the service and pass back a reference to it, or references the existing WorkItem, and passes back a reference to it.

In conclusion, you use CreateNew to have a new instance of whatever it is you need when something is created (not before), and you use ServiceDependency to have a new instance of service (that it’s already created) and pass back a reference to it. Now, it’s up to you to decide which one suits better to your problem.

You may want to have a look at the following WCSF Help Topics:
  • Working with ObjectBuilder.

Please let me know if this helps!

Ignacio Baumann Fonay
http://staff.southworks.net/blogs/ibaumann/
Sep 25, 2007 at 9:36 PM
Edited Sep 25, 2007 at 9:37 PM
Deleted as posted twice.