Asp.net mvc 从数据库加载设置的最佳方法是什么?
如何将表“设置”加载到asp.net mvc中,以便将其用作整个应用程序的参考设置 是否有任何方法可以节省内存和使用量来解决此问题?据我所知,如果数据库中有设置,我必须让程序将表加载到变量中,然后调用。但是有没有办法避免查询被浪费呢 我正在使用LINQtoSQLAsp.net mvc 从数据库加载设置的最佳方法是什么?,asp.net-mvc,linq-to-sql,Asp.net Mvc,Linq To Sql,如何将表“设置”加载到asp.net mvc中,以便将其用作整个应用程序的参考设置 是否有任何方法可以节省内存和使用量来解决此问题?据我所知,如果数据库中有设置,我必须让程序将表加载到变量中,然后调用。但是有没有办法避免查询被浪费呢 我正在使用LINQtoSQL 如果您使用合适的ORM层,如NHibernate(例如Fluent),它可以完全自动地将调用(SQL查询)缓存到设置表中,那就谢谢了。您将在不使用任何SQL的情况下处理这些表,仅作为对类的方法的调用 然而,它需要学习NHibernate
如果您使用合适的ORM层,如NHibernate(例如Fluent),它可以完全自动地将调用(SQL查询)缓存到设置表中,那就谢谢了。您将在不使用任何SQL的情况下处理这些表,仅作为对类的方法的调用 然而,它需要学习NHibernate,这需要一点时间来适应 如果不向数据库发出查询,则无法从数据库中获取设置表的数据。但是,您可以通过使用ORM来避免将结果映射到对象的繁琐使用 如果同时使用和,则MS SQL Server 2008的外观如下所示:
// this depends on your implementation, I assume a Settings class with
// simple getters and setters that map directly to the table. Use
// Fluent to do the mapping (see link) automatically through AutoMappings
// example of using AutoMappings plus configuration of linking to DB:
ISessionFactory sessionFactory = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c =>
c.Server("123.12.21.321")
.Database("db_name")
.Username("db_user_with_access_to_db")
.Password("passwordhere")
)
)
.Mappings(m =>
m.AutoMappings.Add(AutoMap.AssemblyOf<Logo>()
.Where(t => t.Namespace == "YourNamespace.Entities"))
)
.BuildSessionFactory();
// example of a Settings class:
public class Settings
{
public int Id { get; private set; }
public int BackgroundColor { get; set }
// etc
}
// example of getting a session, retrieving data, changing/saving data
ISession session = sessionFactory.OpenSession(); // session for getting data from DB
Setting mySetting = session.Get<Setting>(someId);
mySetting.BackgroundColor = 0xAA44DD;
var transaction = session.BeginTransaction();
session.SaveOrUpdate(mySetting);
transaction.Commit();
// how it looks like if you use Generics and a little Dao class to wrap it all up:
Dao<Settings> daoSettings = new Dao<Settings>();
Settings someSettings = daoSettings.Get(someIdHere);
Settings userSettings = daoSettings.Get(new User("John"));
List<Settings> allSettings = daoSettings.GetAll();
int BackgroundColor = userSettings.BackgroundColor; // any of the columns here
userSettings.BackgroundColor = 0x45DA8E;
daoSettings.Save(userSettings);
//这取决于您的实现,我假设设置类
//直接映射到表的简单getter和setter。使用
//Fluent通过自动映射自动进行映射(请参见链接)
//使用AutoMappings plus配置链接到数据库的示例:
ISessionFactory sessionFactory=fluntly.Configure()
.数据库(
MsSqlConfiguration.MsSql2008
.ConnectionString(c=>
c、 服务器(“123.12.21.321”)
.数据库(“数据库名称”)
.Username(“db\u用户\u具有访问\u db的权限”)
.Password(“passwordhere”)
)
)
.Mappings(m=>
m、 AutoMappings.Add(AutoMap.AssemblyOf())
.Where(t=>t.Namespace==“YourNamespace.Entities”))
)
.BuildSessionFactory();
//设置类的示例:
公共类设置
{
public int Id{get;private set;}
public int BackgroundColor{get;set}
//等
}
//获取会话、检索数据、更改/保存数据的示例
ISession session=sessionFactory.OpenSession();//用于从数据库获取数据的会话
设置mySetting=session.Get(someId);
mySetting.BackgroundColor=0xAA44DD;
var transaction=session.BeginTransaction();
session.SaveOrUpdate(mySetting);
Commit();
//如果您使用泛型和一个小的Dao类来概括这一切,会是什么样子:
Dao daoSettings=newdao();
Settings someSettings=daoSettings.Get(someIdHere);
Settings userSettings=daoSettings.Get(新用户(“John”);
List allSettings=daoSettings.GetAll();
int BackgroundColor=userSettings.BackgroundColor;//这里有没有专栏
userSettings.BackgroundColor=0x45DA8E;
daoSettings.Save(用户设置);
其他的ORM也存在,如果这是一次性的情况,并且您以前从未这样做过,那么NHibernate可能有点过火。但是,它有一个自动的1级和2级缓存,以防止对数据库进行任何不必要的往返。它目前(据称?)是业界领先的开源ORM解决方案
更新:添加了简单的代码示例和指向NH/Fluent的链接是,如果您使用适当的ORM层,如NHibernate(例如使用Fluent),它可以完全自动地将调用(SQL查询)缓存到设置表中。您将在不使用任何SQL的情况下处理这些表,仅作为对类的方法的调用 然而,它需要学习NHibernate,这需要一点时间来适应 如果不向数据库发出查询,则无法从数据库中获取设置表的数据。但是,您可以通过使用ORM来避免将结果映射到对象的繁琐使用 如果同时使用和,则MS SQL Server 2008的外观如下所示:
// this depends on your implementation, I assume a Settings class with
// simple getters and setters that map directly to the table. Use
// Fluent to do the mapping (see link) automatically through AutoMappings
// example of using AutoMappings plus configuration of linking to DB:
ISessionFactory sessionFactory = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c =>
c.Server("123.12.21.321")
.Database("db_name")
.Username("db_user_with_access_to_db")
.Password("passwordhere")
)
)
.Mappings(m =>
m.AutoMappings.Add(AutoMap.AssemblyOf<Logo>()
.Where(t => t.Namespace == "YourNamespace.Entities"))
)
.BuildSessionFactory();
// example of a Settings class:
public class Settings
{
public int Id { get; private set; }
public int BackgroundColor { get; set }
// etc
}
// example of getting a session, retrieving data, changing/saving data
ISession session = sessionFactory.OpenSession(); // session for getting data from DB
Setting mySetting = session.Get<Setting>(someId);
mySetting.BackgroundColor = 0xAA44DD;
var transaction = session.BeginTransaction();
session.SaveOrUpdate(mySetting);
transaction.Commit();
// how it looks like if you use Generics and a little Dao class to wrap it all up:
Dao<Settings> daoSettings = new Dao<Settings>();
Settings someSettings = daoSettings.Get(someIdHere);
Settings userSettings = daoSettings.Get(new User("John"));
List<Settings> allSettings = daoSettings.GetAll();
int BackgroundColor = userSettings.BackgroundColor; // any of the columns here
userSettings.BackgroundColor = 0x45DA8E;
daoSettings.Save(userSettings);
//这取决于您的实现,我假设设置类
//直接映射到表的简单getter和setter。使用
//Fluent通过自动映射自动进行映射(请参见链接)
//使用AutoMappings plus配置链接到数据库的示例:
ISessionFactory sessionFactory=fluntly.Configure()
.数据库(
MsSqlConfiguration.MsSql2008
.ConnectionString(c=>
c、 服务器(“123.12.21.321”)
.数据库(“数据库名称”)
.Username(“db\u用户\u具有访问\u db的权限”)
.Password(“passwordhere”)
)
)
.Mappings(m=>
m、 AutoMappings.Add(AutoMap.AssemblyOf())
.Where(t=>t.Namespace==“YourNamespace.Entities”))
)
.BuildSessionFactory();
//设置类的示例:
公共类设置
{
public int Id{get;private set;}
public int BackgroundColor{get;set}
//等
}
//获取会话、检索数据、更改/保存数据的示例
ISession session=sessionFactory.OpenSession();//用于从数据库获取数据的会话
设置mySetting=session.Get(someId);
mySetting.BackgroundColor=0xAA44DD;
var transaction=session.BeginTransaction();
session.SaveOrUpdate(mySetting);
Commit();
//如果您使用泛型和一个小的Dao类来概括这一切,会是什么样子:
Dao daoSettings=newdao();
Settings someSettings=daoSettings.Get(someIdHere);
Settings userSettings=daoSettings.Get(新用户(“John”);
List allSettings=daoSettings.GetAll();
int BackgroundColor=userSettings.BackgroundColor;//这里有没有专栏
userSettings.BackgroundColor=0x45DA8E;
daoSettings.Save(用户设置);
其他的ORM也存在,如果这是一次性的情况,并且您以前从未这样做过,那么NHibernate可能有点过火。但是,它有一个自动的1级和2级缓存,以防止对数据库进行任何不必要的往返。它目前(据称?)是业界领先的开源ORM解决方案
更新:添加了简单的代码示例和NH/Fluent的链接