共享連接這是我的Global.asax當前實現StructureMap的:使用PetaPoco與StructureMap
var container = (IContainer)IOCContainer.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
下面是refered到上面的代碼:
public static class IOCContainer
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IController>();
});
x.For<IConfigRepository>().Use<ConfigRepository>();
});
return ObjectFactory.Container;
}
}
public class StructureMapDependencyResolver : IDependencyResolver
{
public StructureMapDependencyResolver(IContainer container)
{
_container = container;
}
public object GetService(Type serviceType)
{
if (serviceType.IsAbstract || serviceType.IsInterface)
{
return _container.TryGetInstance(serviceType);
}
else
{
return _container.GetInstance(serviceType);
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.GetAllInstances<object>()
.Where(s => s.GetType() == serviceType);
}
private readonly IContainer _container;
}
我已閱讀,使用共享連接可能會提高性能,所以我想知道如何在我的MVC應用程序中使用它。我想我將不得不將新創建的PetaPoco.Database對象傳遞給我的存儲庫的構造函數?
感謝
我不使用T4遺憾 – Jon
哦,對了,你是用手寫出所有的poco嗎? –
是的,因爲我有數據庫有令人討厭的大寫字母和下劃線到處都是列名,所以我創建了POCO並使用列名裝飾屬性以保持代碼清潔 – Jon