2017-06-07 84 views
0

我一直在收集關於擁有的實例,我需要設置一個。 http://docs.autofac.org/en/latest/advanced/owned-instances.htmlautofac和擁有的實例

我有2個方法在同一個類中使用這個擁有的實例。我已經將它設置這樣的:

private readonly Func<SessionDetails, Owned<ITroposUnitOfWork>> _paramatizedTroposUnitOfWork; 

我的工作類的構造單位是這樣的:

/// <summary> 
/// Used for creating manual sessions 
/// </summary> 
/// <param name="sessionDetails">The details of the session to be created</param> 
public TroposUnitOfWork(SessionDetails sessionDetails) 
{ 

    // Throw if we don't supply any details 
    ThrowIf.ArgumentIsNull(() => sessionDetails); 

    // Start the session 
    StartSession(sessionDetails); 
} 

所以,我的理解是,如果我用一個使用塊,那麼單元的工作將在電話會議結束時處理。但事實並非如此。 就像我之前提到的,我有2個方法使用這個擁有的實例。它們是:

/// <summary> 
/// Creates the Tropos user 
/// </summary> 
/// <param name="model">The user to be created</param> 
/// <param name="password">The password to set</param> 
private async Task CreateTroposUserAsync(User model, string password) 
{ 

    // If there is no password, throw an error 
    ThrowIf.ArgumentIsNull(() => password); 

    // Get our master user 
    var user = await base.FindByNameAsync(model.Master); 

    // If we have no master user, throw an error 
    if (user == null) throw new ObjectNotFoundException(); 

    // Create our session details 
    var sessionDetails = _troposSession.Value.Create(user); 

    // User our new user 
    using (var troposUnitOfWork = _paramatizedTroposUnitOfWork(sessionDetails)) 
    { 

     try 
     { 

      // Create our tropos user service 
      var userService = new TroposUserService(troposUnitOfWork.Value); 

      // Create our user 
      var transaction = userService.Create(model); 

      // Save our changes (Don't throw an error if the user already exists) 
      troposUnitOfWork.Value.RunTransaction(transaction); 

     } catch (Exception ex) 
     { 

      // Display human readable messages 
      throw new Exception(ex.Message); 
     } 
    } 

    // Sets the new users password 
    SetTroposPassword(model, password); 

    // Update the flag 
    model.HasTroposLogin = true; 
} 

而另一個是:

/// <summary> 
/// Sets the tropos password 
/// </summary> 
/// <param name="model">The user that needs the password setting</param> 
/// <param name="password"></param> 
private void SetTroposPassword(User model, string password) 
{ 

    // Create our session details 
    var sessionDetails = _troposSession.Value.Create(model.UserName); 

    // Create our anonymous session 
    using (var troposUnitOfWork = _paramatizedTroposUnitOfWork(sessionDetails)) 
    { 

     // Create our tropos user service 
     var userService = new TroposUserService(troposUnitOfWork.Value); 

     // Set our password 
     var transaction = userService.ChangePassword(password); 

     // Save our changes 
     troposUnitOfWork.Value.RunTransaction(transaction); 
    } 
} 

第一種方法不調用第二方法,但外使用塊。我在處理方法中放置了一個斷點,它只被命中一次。構造函數只被打一次。 有誰知道爲什麼?

+0

我們需要看到_paramatizedTroposUnitOfWork的初始化解決類似的問題。哪個類具有CreateTroposUserAsync方法?我們需要看看那個班級的指揮員。你爲什麼要新的 –

回答

0

我們需要看到_paramatizedTroposUnitOfWork的初始化。 CreateTroposUserAsync方法有哪些類?我們需要看看那個類的構造函數。我想你的總體目標是實現一個工作單元。

您之所以只敲擊構造函數一次的原因可能是由於您在註冊時使用的一生。如果它擁有。然後這兩個方法可能在相同的生命週期範圍內執行,並且只依賴解析一次。換句話說,_paramatizedTroposUnitOfWork(sessionDetails)返回相同的實例。

我用的裝飾和工廠

public interface IEventHandlerFactory<in TNotification> where TNotification 
    : class, IAsyncNotification 
{ 
    IAsyncNotificationHandler<TNotification> Create(ILifetimeScope 
                  lifetimeScope); 
} 

public class EventHandlerFactory<TNotification, TEventHandler> : 
     IEventHandlerFactory<TNotification> 
     where TNotification : class, IAsyncNotification 
     where TEventHandler : class, IAsyncNotificationHandler<TNotification> 
{ 
    public IAsyncNotificationHandler<TNotification> Create(ILifetimeScope 
        lifetimeScope) 
    { 
     return lifetimeScope.ResolveNamed<TEventHandler>("EventHandler") 
         as IAsyncNotificationHandler<TNotification>; 
    } 
} 

完整的.NET小提琴是這裏https://dotnetfiddle.net/fw4IBw

相關問題