简体   繁体   English

具有某种子方法的C#创建方法

[英]C# creating methods with some kind of submethods

I want to build my own mysql class. 我想建立自己的mysql类。 But I'm relative new to c#. 但是我对c#还是比较陌生。

So in my thoughts I would like to build some like: 因此,根据我的想法,我想构建一些类似的东西:

mySqlTool.select("a,b,c").from("foo").where("x=y");

I dont know how this is called and actually I dont really know if this is even possible. 我不知道如何称呼它,实际上我什至不知道这是否有可能。 My google search ended with no real result. 我的Google搜索没有任何实际结果。

So my questions are: 所以我的问题是:

Is it possible to do some like my sample above? 可以像我上面的示例一样做一些事情吗?

Creating a fluent api isn't overly complicated. 创建流利的api并不过分。 You simply return the current instance of the class from each method which allows them to be chained the way that you specify in your post: 您只需从每个方法返回该类的当前实例,就可以按照您在帖子中指定的方式将它们链接起来:

public class MySqlTool
{
    private string _fields, _table, _filters;

    public MySqlTool Select(string fields)
    {
        _fields = fields;
        return this;
    }

    public MySqlTool From(string table)
    {
        _table = table;
        return this;
    }

    public MySqlTool Where(string filters)
    {
        _filters = filters;
        return this;
    }

    public Results Execute()
    {
        // build your query from _fields, _table, _filters
        return results;
    }
}

Would allow for you to run a query like: 将允许您运行如下查询:

var tool = new MySqlTool();
var results = tool.Select("a, b, c").From("someTable").Where("a > 1").Execute();

It looks like you are trying to do something similar to Linq to SQL, but with MySQL instead. 看来您正在尝试执行与Linq to SQL类似的操作,但要使用MySQL。 There is a project called LinqConnect that does this. 有一个名为LinqConnect项目可以执行此操作。 I have no affiliation with them. 我与他们没有联系。

You can use it like this (from a LinqConnect tutorial ): 您可以这样使用它(来自LinqConnect 教程 ):

CrmDemoDataContext context = new CrmDemoDataContext();
var query = from it in context.Companies
    orderby it.CompanyID
    select it;

foreach (Company comp in query)
    Console.WriteLine("{0} | {1} | {2}", comp.CompanyID, comp.CompanyName, comp.Country);

Console.ReadLine();

I'm not a big fan of it personally, but it can be useful if you're just learning. 我个人并不喜欢它,但是如果您只是在学习,它可能会很有用。 Linq2SQL can give you that functionality somewhat out of the box. Linq2SQL可以立即为您提供该功能。

If you're looking to accomplish this yourself. 如果您希望自己完成此任务。 You'll want to turn to extension methods . 您将需要使用扩展方法 A feature within c# that will allow you to extend your classes using static methods, operating on the base. c#中的一项功能,使您可以使用在基础上运行的静态方法扩展类。

My recommendation if you're doing your own data-access layer, then you should use something like dapper (built/used by the crew at StackOverflow). 我的建议是,如果您要执行自己的数据访问层,则应使用dapper之类的东西(由StackOverflow的工作人员构建/使用)。 Which offers you a simple wrapper around the base connection to the database. 这为您提供了到数据库基本连接的简单包装。 It offers simple ORM functionality and does parameterization for you. 它提供简单的ORM功能并为您进行参数化。

I would also recommend strongly that you encapsulate your intensive queries within Stored Procedures. 我也强烈建议您将密集查询封装在存储过程中。

What you're looking to do is design a fluent interface , and it's somewhat of an advanced concept for someone who is just learning C#. 您想要做的是设计一个流畅的界面 ,对于刚学习C#的人来说,这是一个高级概念。 I would suggest you stick to the basics until you have a little more experience. 我建议您坚持基础,直到您有更多的经验。

More importantly, there are already existing data adapters built into the .NET FCL and also third-party adapters like this one that are more suitable and already do what you're trying to do using "LINQ to (database)". 更重要的是,.NET FCL中已经内置了数据适配器,并且像这样的第三方适配器更合适,并且已经可以使用“ LINQ to(数据库)”来完成您要执行的操作。 I wouldn't reinvent the wheel if I were you. 如果我是你,我不会重蹈覆辙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM