Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Resolve only activated instances
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
jonathan.s.oli...@hotmail.com  
View profile  
 More options Jun 27 2008, 11:47 am
From: jonathan.s.oli...@hotmail.com
Date: Fri, 27 Jun 2008 08:47:05 -0700 (PDT)
Local: Fri, Jun 27 2008 11:47 am
Subject: Resolve only activated instances
Nick,

Is there a way to resolve only activated instances?  In my repository
code I'm trying to inject a unit of work into them only if one has
already been created, otherwise either null or a null object will be
injected.

I was looking through the source and it appears that OnPreparing could
be a possibility because it's called isn't an instance already
available (_scope.InstanceAvailable).  I'd have to use the Null Object
Pattern to inject a non-null object during OnPreparing - but it
appears that instance

One possibility would be to use the Null Object Pattern, but it
appears that the null object provided during OnPreparing would be used
in any future calls to Resolve<>.

The idea is to have the repository use a unit of work if available
otherwise to perform the persistence operation right then and there.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tyler Tipton  
View profile  
 More options Jun 27 2008, 12:38 pm
From: "Tyler Tipton" <tyler.tip...@gmail.com>
Date: Fri, 27 Jun 2008 11:38:55 -0500
Local: Fri, Jun 27 2008 12:38 pm
Subject: Re: Resolve only activated instances

Why not just have your code insert an already created instance into the
container?

I just set up simple factories for creating and adding instances to the
container rather than using a registration in this situation. Or if you need
the UoW instances to be built by the container also then you could consider
using tagged instances.

-Tyler


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicholas Blumhardt  
View profile  
 More options Jun 27 2008, 6:41 pm
From: "Nicholas Blumhardt" <nicholas.blumha...@gmail.com>
Date: Sat, 28 Jun 2008 08:41:33 +1000
Local: Fri, Jun 27 2008 6:41 pm
Subject: Re: Resolve only activated instances

public class UnitOfWorkTracker
{
  public UnitOfWork CurrentUnitOfWork { get; set; }

}

builder.Register<UnitOfWorkTracker>().ContainerScoped();

builder.Register(c => {
  var result = new UnitOfWork();
  c.Resolve<UnitOfWorkTracker>().CurrentUnitOfWork = result;
  return result;

}).ContainerScoped();

builder.Register(c => new SomeRepository() { UoW =
c.Resolve<UnitOfWorkTracker>().CurrentUnitOfWork) })
  .ContainerScoped();

:) ? Just the first thing that came to mind. May not be the nicest if you
want to autowire your repositories. There might be other variations on the
UnitOfWorkTracker theme.

A better approach might be:

builder.Register(c => someCondition ? new UnitOfWork() : new
NullUnitOfWork())
  .As<IUnitOfWork>()
  .ContainerScoped();

... That one depends on what that condition is.

Tyler was onto something too:

builder.Register(c =>
c.Resolve<UnitOfWorkTracker>().CurrentUnitOfWork).ContainerScoped();

That way you could put an instance in the
UnitOfWorkTracker.CurrentUnitOfWork property when a 'real' UoW is going on
(the value could be still be resolved from the container, perhaps by name.)

I think the second approach is the cleanest because within a nested
container there will either be a UnitOfWork proper, or a NullUnitOfWork,
never both. If this is not possible in your design, look closely at how the
inner container corresponds with a unit of work - it *probably* should be
1-1.

I'd guess there are many other approaches - some of them a bit crazier than
others.

Hope this helps

NB

On Sat, Jun 28, 2008 at 2:38 AM, Tyler Tipton <tyler.tip...@gmail.com>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google