2009-06-30 104 views
0

我正在创建一个asp.net mvc网站,我需要一个建议。我有以下层:Spring + Hibernate - 多个数据库

  • 数据库
  • 数据访问层(域对象,DAO接口基于一个NHibernate的+ DAO实现)
  • 服务层(服务接口+服务实现)
  • 表示层( ASP.NET MVC)

实际上有几个数据库:

  • 一个数据库,一个共同的数据和客户名单
  • 许多数据库 - 每个数据库的一个客户(具有相同的结构,但不necessery在同一台服务器上)

DAO的服务“链接在一起”以这种方式:

MyMainService (contains business logic) 
    MyMainDao (contains data access functions) 
    MyMainSessionFactory (session factory for the main database) 
     MyMainDbProvider (db provider with a connection to the main database) 

或:

MyCustomerService (contains business logic) 
    MyCustomerDao (contains data access functions) 
    MyCustomerSessionFactory (session factory for the customer database) 
     MyCustomerDbProvider (db provider with a connection to the main database) 

或混合(使用在两个数据库同时):

MySuperService (contains business logic) 
    MyMainDao (contains data access functions) 
    MyMainSessionFactory (session factory for the main database) 
     MyMainDbProvider (db provider with a connection to the main database) 
    MyCustomerDao (contains data access functions) 
    MyCustomerSessionFactory (session factory for the customer database) 
     MyCustomerDbProvider (db provider with a connection to the main database) 

我在两个提供程序中使用属性占位符(和PropertyPlaceholderConfigurer)。

在这里,我们来到这里我想用这个服务(在ASP.NET MVC控制器)的观点:如果我想使用MyMainService

是没有问题的 - 我使用DI,一切工作正常。

但是,如果我想使用MyCustomerService或MySuperService,我不认为我可以使用DI,但更多的“依赖拉”。 我认为我应该创建某种“服务工厂”,我将传递一个客户ID,服务工厂 将返回与相应数据库连接的服务。喜欢的东西:

TService GetService<TService>(int customerId) 
{ 
    CustomerInfo info = GetCustomerInfo(customerId); 
    IConfigurableApplicationContext context = (IConfigurableApplicationContext)WebApplicationContext.GetRootContext(); 
    PropertyPlaceholderConfigurer conf = (PropertyPlaceholderConfigurer)context.GetObject("PropertyPlaceholderConfigurer"); 
    conf.Properties["db.datasource"] = info.DataSource; 
    conf.Properties["db.user"] = info.UserName; 
    conf.Properties["db.password"] = info.Password; 
    conf.Properties["db.database"] = info.DatabaseName; 
    context.AddObjectFactoryPostProcessor(conf); 
    context.Refresh(); 
    IEnumerator it = context.GetObjectsOfType(typeof(TService)).Values.GetEnumerator(); 
    if (it.MoveNext()) 
    { 
    return (TService)it.Current; 
    } 
} 

这是正确的方式还是我完全错了,我应该这样做一些其他的方式?

注:会有的时候我会想在同一时间使用不同的客户相同的服务,例如一个案例:

IMyService s1 = GetService<IMyService>(1); 
    IMyService s2 = GetService<IMyService>(2); 
    s1.importData(s2.exportData()); 

任何意见,将不胜感激。

非常感谢!

回答

0

在“MySuperService”中,您同时使用两个bean(MyMainDao和MyCustomerDao)。这是可行的,因为它们有不同的类型(Java类)。

如果您想要一个可以返回的工厂,请使用与“MySuperService”中相同的方法,但不要依赖于类型,请给这两个不同的名称。这样,你的工厂可以按名称查询它们,你可以这样说:

connector = factory.lookup("name"); 
+0

我不确定你的意思。你能否再描述一下?谢谢! – rrejc 2009-06-30 14:34:52

相关问题