user356247
user356247

Reputation: 325

Injecting IDbConnectionFactory into Service class

I have started using ServiceStack today. So, let me know if I am doing something completely wrong.

I am trying to inject Db into my Service class, for which I am using this code

[TestFixture]
public class UserServiceTests
{
    private UserService service;

    [SetUp]
    public void SetUp()
    {
        var appHost = new BasicAppHost();
        var dbFactory = new OrmLiteConnectionFactory(":memory:", false, SqliteDialect.Provider);
        appHost.Container.Register<IDbConnectionFactory>(dbFactory);
        service = new UserService();
        service.SetAppHost(appHost);
    }

    [Test]
    public void Calling_post_method_with_valid_User_saves_it_in_database()
    {
        var User = new User { Name = "Acme Limited" };
        var id = service.Post(User);
        Assert.AreEqual(1, id);
    }
}

There are two problems: 1) I am getting exception:

Could not load file or assembly 'System.Data.SQLite, Version=1.0.82.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 

Is there an easy way to fix this? And do we really need SQLite for testing, is there is Fake object available?

2) The compiler is warning about - service.SetAppHost(appHost). SetAppHost is depricated. How can I inject Db into my service class without using SetAppHost?

Any ideas?

Upvotes: 1

Views: 535

Answers (1)

mythz
mythz

Reputation: 143399

Sounds like you have a platform incompatibility issue with Sqlite, make sure you're using the right Sqlite package for your machine.

As for service.SetAppHost(appHost) the deprecated message says to use service.SetResolver(appHost) as IAppHost also implements IResolver, so use that.

Upvotes: 1

Related Questions