从实体空间(Tiraggo)过渡到 Servicestack Ormlite [英] Transition from Entityspaces(Tiraggo) into Servicestack Ormlite

查看:61
本文介绍了从实体空间(Tiraggo)过渡到 Servicestack Ormlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此时我们正在从 Entityspaces(Tiraggo) 迁移到 Servicestack Ormlite.

一点是打开和关闭DBConnection的方式.

我为这种比较道歉,但它对这个问题很有用.在 Tiraggo 中,在我的 wep 应用程序中,在 global.asax.cs 中,我把这个:

 protected void Application_Start(object sender, EventArgs e){Tiraggo.Interfaces.tgProviderFactory.Factory = new Tiraggo.Loader.tgDataProviderFactory();}

在 web.config 中存在 Tiraggo、连接字符串和 ORM 的部分.

在使用类的过程中,我们只是这样做:

User user = new User();用户名=一些";user.Comment = "一些";用户保存();

我不打开,关闭一个 DBConnection.它对程序员是透明的.只需创建实例类并使用它们即可.

我定义了一个类、一个存储库,仅此而已.没有数据库定义或交互.一切都发生在 webforms 应用程序中,数据层在同一个应用程序中.

当我们迁移到 Servicestack ORMLite 时,我看到 DBConnection 的打开太在 globlal.asax.cs 内部,但它引用了一个服务,而不是类或存储库.

 公共类 AppHost : AppHostBase{public AppHost() : base("Hello ServiceStack", typeof(HelloService).Assembly) {}公共覆盖无效配置(容器容器){}}

所以我的第一个问题是:如果我没有服务 (HelloService),我只有类或存储库,我该如何使用它.所以我不能将这种技术用于 DBConnection 我的数据库.

我还看到访问数据库,我需要一个开放的连接.我尝试这样做:

 using (var Db = DbFactory.Conn.OpenDbConnection()){返回 Db.SingleById(id);}

后来,我找到了一个我正在寻找的示例,Pluralsight 视频.NET Micro ORMs"Steve Mihcelotti,他只是打开了连接,但从不关闭它,从不使用使用"语法.

所以我的两个问题是:

1) 有没有办法像使用 servicestack ormlite 的所有示例一样打开 DbFactory(dbConnection),但不使用服务(我不使用服务,我想使用 Ormlite 但只使用类和存储库)2)有没有办法在每次访问类或存储库时连接到数据库而不使用使用"语法,或者3)唯一的方法是Pluralsight视频中显示的方法,即.打开连接在每个方法中抛出 using 语法(类的行程)

我希望我说的很清楚.

解决方案

IDbConnectionFactory 的好处在于它是一个 ThreadSafe 单例,可以安全地传递和引用,因为它不包含任何资源自行打开(即数据库连接).

一个提供良好调用站点 API 的惰性模式是 RepositoryBase 类:

公共抽象类 RepositoryBase : IDisposable, IRepository{公共虚拟 IDbConnectionFactory DbFactory { get;放;}IDb连接数据库;公共虚拟 IDbConnection Db{得到 { 返回数据库 ??(db = DbFactory.OpenDbConnection());}}公共虚拟无效处置(){如果(数据库!= null)db.Dispose();}}

这与 ServiceStack 的 Service 类用来提供一个漂亮的 API 的模式相同,该 API 只有在服务中使用时才会打开,例如:

公共类 MyRepository : RepositoryBase{公共 Foo GetFooById(int id){返回 Db.SingleById(id);}}

<块引用>

注意:此模式确实希望您的依赖项在使用后会被处理掉.

另一种选择是利用您的 IOC 注入具有托管生命周期范围的开放 IDbConnection,例如:

container.Register(c =>c.解析().OpenDbConnection()).ReusedWithin(ReuseScope.Request);

连接的生命周期取决于您的首选 IOC.

不使用 IOC

虽然使用 IOC 来管理您的应用程序依赖项并提供松耦合通常是一种很好的做法,但如果您不想使用 IOC,您也可以将 DbFactory 设为静态属性,例如:

公共抽象类 RepositoryBase : IDisposable{公共静态 IDbConnectionFactory DbFactory { 获取;放;}IDb连接数据库;公共虚拟 IDbConnection Db{得到 { 返回数据库 ??(db = DbFactory.OpenDbConnection());}}公共虚拟无效处置(){如果(数据库!= null)db.Dispose();}}

你可以直接在启动时初始化,例如:

protected void Application_Start(object sender, EventArgs e){RepositoryBase.DbFactory = new OrmLiteConnectionFactory(connectionString, SqlServer.Provider);}

<块引用>

注意:如果您不使用 IOC,那么您需要确保在使用后处理存储库类的实例(例如 MyRepository).

at this moment we are migrating from Entityspaces(Tiraggo) into Servicestack Ormlite.

One point is the way to open and close the DBConnection.

I apologize for the comparission but it is useful for the question. In Tiraggo, inside my wep application, in the global.asax.cs I put this:

    protected void Application_Start(object sender, EventArgs e)
    {
        Tiraggo.Interfaces.tgProviderFactory.Factory = new Tiraggo.Loader.tgDataProviderFactory();
    }

In web.config exists the section for Tiraggo, the connectionstring and the ORM does the rest.

During the use of the classes we just do this:

User user = new User(); user.Name="some"; user.Comment = "some"; user.Save();

I dont open, close a DBConnection. It is transparent for the programmer. Just create the instance classes and use them.

I define a class, a repository and that's all. No DB definition or interaction. Everything happens in a webforms app, with the datalayer inside the same app.

When we are migrating to Servicestack ORMLite, I see the open of the DBConnection is too inside the globlal.asax.cs, but it references a Service no a class or repository.

   public class AppHost : AppHostBase
{
    public AppHost() : base("Hello ServiceStack", typeof(HelloService).Assembly) {}
    public override void Configure(Container container) {}
}

So my first question is: how can I use it if I dont have a Service (HelloService), I have just classes or repositories. So I cant use this technique for DBConnection my DB.

I also see that accesing the Db, I need a open connection. I try to do this:

            using (var Db = DbFactory.Conn.OpenDbConnection())
        {
            return Db.SingleById<Anio>(id);
        }

Later, I found a sample like I was looking for, the Pluralsight video ".NET Micro ORMs" Steve Mihcelotti, and he just open the connection, but never Close it, never use the "using" syntax.

So my 2 questions are:

1) Is there a way for open the DbFactory(dbConnection) like all the samples using servicestack ormlite, but without using a Services ( I dont use Services, I want to use Ormlite but just with classes and repositories) 2) Is there a way for connnect to the database in each trip to the class or repository without using the "using" syntax, or 3) the only way is the one showed in the Pluralsight video, ie. open the connection throw the using syntax in each Method (trip to the class)

I hope I was clear.

解决方案

The nice thing about IDbConnectionFactory is that it's a ThreadSafe Singleton which can be safely passed around and referenced as it doesn't hold any resources open itself (i.e. DB Connections).

A lazy pattern which provides a nice call-site API is the RepositoryBase class:

public abstract class RepositoryBase : IDisposable, IRepository
{
    public virtual IDbConnectionFactory DbFactory { get; set; }

    IDbConnection db;
    public virtual IDbConnection Db
    {
        get { return db ?? (db = DbFactory.OpenDbConnection()); }
    }

    public virtual void Dispose()
    {
        if (db != null)
            db.Dispose();
    }
}

This is the same pattern ServiceStack's Service class uses to provide a nice API that only gets opened when it's used in Services, e.g:

public class MyRepository : RepositoryBase
{
    public Foo GetFooById(int id)
    {
        return Db.SingleById<Foo>(id);
    }
}

Note: This pattern does expect that your dependencies will be disposed after use.

Another alternative is to leverage your IOC to inject an Open IDbConnection with a managed lifetime scope, e.g:

container.Register<IDbConnection>(c => 
    c.Resolve<IDbConnectionFactory>().OpenDbConnection())
    .ReusedWithin(ReuseScope.Request);

The life-cycle of the connection is then up to your preferred IOC.

Without Using an IOC

Whilst it's typically good practice to use an IOC to manage your Apps dependencies and provide loose-coupling, if you don't want to use an IOC you can also make DbFactory a static property, e.g:

public abstract class RepositoryBase : IDisposable
{
    public static IDbConnectionFactory DbFactory { get; set; }

    IDbConnection db;
    public virtual IDbConnection Db
    {
        get { return db ?? (db = DbFactory.OpenDbConnection()); }
    }

    public virtual void Dispose()
    {
        if (db != null)
            db.Dispose();
    }
}

Which you can just initialize directly on startup, e.g:

protected void Application_Start(object sender, EventArgs e)
{
    RepositoryBase.DbFactory = new OrmLiteConnectionFactory(
        connectionString, SqlServer.Provider);
}

Note: If you're not using an IOC then you want to make sure that instances of your repository classes (e.g. MyRepository) are disposed of after use.

这篇关于从实体空间(Tiraggo)过渡到 Servicestack Ormlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆