gpt4 book ai didi

mvvm - 使用IoC在 View 模型中创建NHibernate存储库

转载 作者:行者123 更新时间:2023-12-03 10:50:01 25 4
gpt4 key购买 nike

我想如何在 View 模型类中使用NHibernate存储库和 session 来更正构建和处理。

例如,我将Caliburn Micro框架与CaSTLe Windsor boostraper一起使用。

首先,我创建了NHibernate存储库:

public interface IRepository{//I omitted not needed code for simplification}

public class NHibRepository: IRepository
{
private ISession _session;

public NHibRepository(ISession session)
{
_session=session;
}
// I omitted not needed code for simplification
}

其次,我使用Fluent NHibernate定义了一些POCO类和映射类。
public class User{}
public class Profile{}
public class Album{}

public class UserMap : ClassMap<User>{}
public class ProfileMap : ClassMap<Profile>{}
public class AlbumMap : ClassMap<Album>{}

现在,我需要在 View 模型中使用NHibernate存储库。
public interface IViewModelA{}

public class ViewModelA : ScreenViewModel, IViewModelA
{
public NHibRepository<User> UserRepo{get;set;}
public NHibRepository<Profile> ProfileRepo{get;set;}

}

public interface IViewModelB{}

public class ViewModelB : Screen, IViewModelB
{
public NHibRepository<Profile> ProfileRepo{get;set;}
public NHibRepoistory<Album> AlbumRepo{get;set;}
}

当我为某些实体类创建存储库类时,我需要将 session 传递给NHibRepository构造函数。
UserRepo= new NHibRepository<User>(NHIBERNATE SESSION);

到目前为止,我使用了一些帮助程序类来创建Nhibernate session 和init存储库。

助手类在这里:
public class FluentNHibHelper
{
private ISessionFactory _sessionFactory;

public FluentNHibHelper(IPersistenceConfigurer db, Assembly asm)
{
InitializeSessionFactory(db, asm);
}


private void InitializeSessionFactory(IPersistenceConfigurer db, Assembly asm)
{
_sessionFactory = Fluently.Configure()
.Database(db)
.Mappings(m => m.FluentMappings.AddFromAssembly(asm))
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.BuildSessionFactory();
}

public ISession OpenSession()
{
return _sessionFactory.OpenSession();
}
}

使用此类创建了存储库类:
private const string ConnString =
@"Server=TestMachine\SQLEXPRESS;Database=TEST;Trusted_Connection=True;";

UserRepo = new NHibRepository<User>(
new FluentNHibHelper(MsSqlConfiguration.MsSql2008.ConnectionString(ConnString),
Assembly.GetExecutingAssembly())
.OpenSession());

现在,我混淆了不是在 View 模型中创建存储库对象的最佳方法是什么。

例如,在bootstraper类中,我可以以某种方式注册存储库类吗?
public class CastleBootstrapper : Bootstrapper<IShellViewModel>
{
private IWindsorContainer _windsorContainer;

protected override void Configure()
{
_windsorContainer = new WindsorContainer();
//register repository class here ???

}
}

有谁能够帮我?非常感谢你

对不起我的英语不好。

最佳答案

我在ASP.net MVC应用程序中使用以下内容。 CaSTLe.Windsor负责为每个存储库创建ISession依赖项。

//Located in your application startup
protected IWindsorContainer CreateContainer()
{
container = new WindsorContainer();

container.Install(
new PersistenceInstaller(),
new RepositoryInstaller()
//, other installers here
);

return container;
}


public class PersistenceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<PersistenceFacility>();
}
}

public class PersistenceFacility : AbstractFacility
{
protected override void Init()
{
NHibernate.Cfg.Configuration config = BuildDatabaseConfiguration();

Kernel.Register(
Component.For<ISessionFactory>()
.UsingFactoryMethod(config.BuildSessionFactory),
Component.For<ISession>()
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
.LifeStyle.Trasient
);
}

private NHibernate.Cfg.Configuration BuildDatabaseConfiguration()
{
return Fluently.Configure()
.Database(SetupDatabase)
.Mappings(m => m.HbmMappings.AddFromAssemblyOf<Entity>())
.ExposeConfiguration(ConfigurePersistence)
.BuildConfiguration();
}

protected virtual IPersistenceConfigurer SetupDatabase()
{
return MsSqlConfiguration.MsSql2008
.UseOuterJoin()
.ConnectionString(x => x.FromConnectionStringWithKey("ApplicationServices"))
.ShowSql();
}

protected virtual void ConfigurePersistence(NHibernate.Cfg.Configuration config)
{
SchemaMetadataUpdater.QuoteTableAndColumns(config);
}

protected virtual bool IsDomainEntity(Type t)
{
return typeof(DomainBase).IsAssignableFrom(t);
}
}



public class RepositoryInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Find().Configure(ConfigureLifeStyle()));
}

private ConfigureDelegate ConfigureLifeStyle()
{
return c => c.LifeStyle.Transient;
}
private BasedOnDescriptor Find()
{
return AllTypes.FromAssemblyContaining<NHRepository>()
.Where(type => type.Name.EndsWith("Repository"))
.WithService
.AllInterfaces();
}
}

关于mvvm - 使用IoC在 View 模型中创建NHibernate存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com