2011-06-24 18 views
10

Her Repository sınıfında bu kodu kullanırsam, SQL profil çalışmasını yaparım, ancak bu kodu her sınıftan StructureMap'in DB'yi işlediği sınıfa taşımak istiyorum. Bir Depo sınıfınStructureMap DBServiceRegistry ve MVC-mini-profiler?

Örnek:

public DB CreateNewContext() 
    { 
     var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString); 
     var profiledConnection = ProfiledDbConnection.Get(sqlConnection); 
     return DataContextUtils.CreateDataContext<DB>(profiledConnection); 
    } 

    public SqlRecipeRepository(DB dataContext) 
    {   
     _db = CreateNewContext(); 
    } 

Şimdi DataContext değişken profilli versiyonu olacak ve böylece benim DBServiceRegistry sınıftan gelmek istiyorum. İşte

DBServiceRegistry sınıftır:

public class DBServiceRegistry : Registry 
{ 
    public DBServiceRegistry() 
    { 
     var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString); 
     var profiledConnection = ProfiledDbConnection.Get(sqlConnection); 
     For<DB>().HybridHttpOrThreadLocalScoped().Use(() => DataContextUtils.CreateDataContext<DB>(profiledConnection)); 

     //Original method just had this: 
     //For<DB>().HybridHttpOrThreadLocalScoped().Use(() => new DB()); 

    } 
} 

Bu kod hataları neden olmaz ama SQL profil alamadım, ben yanlış yapıyorum?

+0

elimden profil parçasının neden çalışmadığını gerçekten anladım, ancak kodla ilgili ciddi bir sorun görüyorum. Tüm DbContexts için kullanılacak kayıt defterinde tek bir bağlantı örneği oluşturuyorsunuz. – PHeiberg

+1

LINQ-to-SQL bağlantısının '.Log' parametresini atarsınız? Ayrıca, ObjectFactory.GetInstance () 'herhangi bir yerde mi çalışıyorsunuz? –

cevap

1

Yorum Yaptığımız doğrudur, satır için sql bağlantısını oluşturarak, kapsam komutunu geçersiz kılarsınız. kapsam doğru olduğunu

çok daha iyidir

Sen doğrulamak için birim testleri kullanabilirsiniz

using System.Configuration; 
using System.Data.SqlClient; 
using System.Threading.Tasks; 

using StructureMap; 
using StructureMap.Configuration.DSL; 

using Xunit; 

public class DBServiceRegistry : Registry 
{ 
    private string connString = ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString; 

    public DBServiceRegistry() 
    { 
     For<DB>().HybridHttpOrThreadLocalScoped().Use(
      () => 
       { 
        var sqlConnection = new SqlConnection(connString); 
        var profiledConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(sqlConnection, MiniProfiler.Current); 
        return DataContextUtils.CreateDataContext<DB>(profiledConnection); 
       }); 
    } 
} 

anonim temsilci içine sürü saklanması (test sözdizimi xunit.net)

public class DBRegistryTests : IDisposable 
{ 
    private Container container; 

    public DBRegistryTests() 
    { 
     // Arrange (or test setup) 
     container = new Container(new DBServiceRegistry()); 
    } 

    [Fact] 
    public void ConnectionsAreSameInThread() 
    { 
     // Create two connections on same thread 
     var conn1 = container.GetInstance<DB>(); 
     var conn2 = container.GetInstance<DB>(); 

     // Assert should be equal because hybrid thread is scope 
     // and test executes on same thread 
     Assert.Equal(conn1, conn2); 

     // Other assertions that connection is profiled 
    } 

    [Fact] 
    public void ConnectionAreNotSameInDifferentThreads() 
    { 
     var conn1 = container.GetInstance<DB>(); 

     // Request second connection from a different thread 
     // (for < c# 4.0 use Thread instead of task) 
     var conn2 = new Task<DB>(() => this.container.GetInstance<DB>()); 
     conn2.Start(); 
     conn2.Wait(); 

     // Assert that request from two different threads 
     // are not the same 
     Assert.NotEqual(conn1, conn2.Result); 
    } 

    public void Dispose() 
    { 
     // Test teardown 
     container.Dispose(); 
    } 
} 
İlgili konular