[翻译]  How to create a complex type from a stored procedure that uses exec()?

[CHINESE]  如何从使用exec()的存储过程创建复杂类型?


I want to create a complex type to use within an entity manager from a query constructed dynamically and executed with exec(). Is it possible?; since I'm writing a filter, what would you do instead if it is not possible?

我想创建一个复杂类型,在动态构造的查询中使用exec()执行实体管理器。可能吗?;因为我正在写一个过滤器,如果不可能,你会做什么呢?

Also, I'm evaluating using linq, but the filter needs many tables and their registers, therefore efficiency is a concern.

此外,我正在使用linq进行评估,但过滤器需要许多表及其寄存器,因此效率是一个问题。

Thanks...

谢谢...

2 个解决方案

#1


4  

Yes, you can use Entity Framework 4 and LINQ on top, it generates the parametrized query and executes it, that's the option.

是的,您可以在顶部使用Entity Framework 4和LINQ,它会生成参数化查询并执行它,这是选项。

Another option is (and I did several times) to create a base class/interface, let's say:

另一个选择是(我做了几次)来创建一个基类/接口,让我们说:

public interface IExecutable
{
    void Execute(IConnection connection);
}
public interface IExecutable<TResult> : IExecutable
{
    TResult Result { get; }
}

public abstract ActionBase<TResult> : IExecutable<TResult>
{
    protected void AddParameter(....);

    protected IDataReader ExecuteAsReader(string query) {
        //create a DB Command, open transaction if needed, execute query, return a reader.
    }

    protected object ExecuteAsScalar(string query) {
        //....
    }

    //the concrete implementation
    protected abstract TResult ExecuteInternal();

    IExecutable.Execute(IConnection connection) {
        //keep the connection
        this.Result = ExecuteInternal();
    }

    //another common logic: 

}

Then you can create your concrete actions:

然后你可以创建你的具体行动:

public sealed class GetUsersAction : ActionBase<<IList<User>>
{
    //just a constructor, you provide it with all the information it neads
    //to be able to generate a correct SQL for this specific situation
    public GetUsersAction(int departmentId) {
        AddParameter("@depId", departmentId);
    }

    protected override IList<User> ExecuteInternal() {
        var command = GenerateYourSqlCommand();

        using(var reader = ExecuteAsReader(command)) {
            while(reader.Read) {
                //create your users from reader
            }
        }
        //return users you have created
    }
}

Very easy to create concrete actions!

很容易创造具体行动!

Then, to make it even easier, create an ExecutionManager whose concern is how to get the connection and execute the action:

然后,为了使它更容易,创建一个ExecutionManager,其关注点是如何获取连接并执行操作:

public sealed ExecutionManager() {

    TResult Execute<TResult>(IExecutable<TResult> action) {
        var connection = OhOnlyIKnowHowTOGetTheConnectionAnfHereItIs();
        action.Execute(connection);
        return action.Result;
    }
}

Now just use it:

现在只需使用它:

var getUsersAction = new GetUsersAction(salesDepartmentId);

//it is not necessary to be a singletone, up to you
var users = ExecutionManager.Instance.Execute(getUsersAction);

//OR, if you think it is not up to ExecutionManager to know about the results:
ExecutionManager.Instance.Execute(getUsersAction);
var users = getUsersAction.Result

Using this simple technique it is really easy to move all the connection/command/execution logic away from the concrete actions into the base class, and the concrete actions' concerns are just generating SQLs and converting database output into some meaningful results.

使用这种简单的技术,将所有连接/命令/执行逻辑从具体操作移到基类中非常容易,而具体操作的关注点只是生成SQL并将数据库输出转换为一些有意义的结果。

Good luck :)

祝你好运 :)

#2


1  

If you decide to go the Linq route and are looking for a good way to do filtering. LinqKit is a great library for building ad-hoc predicates. The problem with the built-in linq library is that you can only ad-hoc combine AND statements, you can not ad-hoc combine OR statements. Linqkit makes that a breeze to accomplish.

如果你决定去Linq路线并且正在寻找一种好的方法来进行过滤。 LinqKit是一个用于构建ad-hoc谓词的出色库。内置linq库的问题是你只能ad-hoc组合AND语句,你不能ad-hoc组合OR语句。 Linqkit让这一切变得轻而易举。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号