我對使用ServiceStack創建服務的速度印象深刻,但是現在我一直無法理解在項目中同時使用OrmLite和Dapper的概念。我註冊這樣的IDbConnectionFactory在我的Global.asax.csDapper和OrmLite在ServiceStack中的IDBConnectionFactory
public override void Configure(Funq.Container container)
{
var dbConnectionFactory =
new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("test"), true, OracleDialect.Provider);
container.Register<IDbConnectionFactory>(dbConnectionFactory);
container.Register<ISubscriberRepository>(
c => new SubscriberRepository(c.Resolve<IDbConnectionFactory>()));
}
,對於OrmLite工作正常,但它是一個不適合小巧玲瓏的那樣簡單。也許我只是認爲這應該比實際更方便。在我的存儲庫中,我試圖調用Oracle存儲過程。這是我在這個過程中使用Dapper而不是OrmLite的主要原因。這是我的信息庫:
public class SubscriberRepository : ISubscriberRepository {
public SubscriberRepository(IDbConnectionFactory conn) {
_conn = conn;
}
public IDbConnectionFactory _conn { get; set; }
public SubscriberResponse GetSubscriber(SubscriberRequest request) {
using (IDbConnection db = _conn.OpenDbConnection()) {
var resp = new SubscriberResponse();
List<Subscriber> s = db.Select<Subscriber>(
q => q.Subscribernum == request.Subscribernum &&
q.Personcode == request.Personcode &&
q.Clientcode == request.Clientcode);
resp.Subscriber = s[0];
return resp;
}
}
public SubscribersResponse SearchSubscribers(SubscribersRequest request) {
var response = new SubscribersResponse();
using (var cnn = new OracleConnection("this is my conneciton string")) {
cnn.Open();
var p = new OracleDynamicParameters();
p.Add("@username", "uname", OracleDbType.Varchar2);
p.Add("@Subscribernum", "", OracleDbType.Varchar2);
p.Add("@Personcode", "", OracleDbType.Varchar2);
p.Add("@Lastname", "TEST", OracleDbType.Varchar2);
p.Add("@Firstname", "HA", OracleDbType.Varchar2);
p.Add("@Mi", "", OracleDbType.Varchar2);
p.Add("@Dob", null, OracleDbType.Date);
p.Add("@MaxResults", 200, OracleDbType.Int32);
p.Add("@Curs", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output);
using (SqlMapper.GridReader multi = cnn.QueryMultiple("SEARCHSUBSCRIBER", p,
commandType: CommandType.StoredProcedure)) {
List<SearchSubscriberResults> r = multi.Read<SearchSubscriberResults>().ToList();
response.Results = r;
}
}
return response;
}
}
This works。但是它在SearchSubscribers函數中根本沒有使用IDbConnectionFactory。我不想在我的存儲庫中查看連接字符串,因爲我真的可以事先在服務中註冊它們。
我試圖使用ServiceStack.Razor.Dapper.SqlMapper.QueryMultiple(),但不起作用,因爲我無法將Oracle sys_refcursor映射回任何內容而不使用OracleDynamicParamaters解決方法。
所以,我的問題是我可以創建從IDBConnectionFactory的Dapper連接?
謝謝!