I have created a public static class utils.cs I want to use it in other classes without prefixing method with utils, what\'s the syntax to do this ?
In this thread there are some questioning and argumenation about not using global methods, but I would like to add that there is another kind of scenario when static imports are desirable, i.e. when you want to bring some constants into scope. For example, if I want to construct SQL statements, I would prefer to not use duplicated string literals, as in this code example (using dynasql):
DBQuery.Select()
.Field("CustomerID")
.Count("OrderID")
.From("Orders")
.GroupBy("CustomerID")
.OrderBy("CustomerID")
.WhereField("OrderDate", Compare.GreaterThan, DBConst.DateTime(ordersAfter))
I would prefer doing this:
public class OrderTable {
public const string Orders = "Orders";
public const string CustomerID = "CustomerID";
public const string OrderID = "OrderID";
public const string OrderDate = "OrderDate";
}
...
DBQuery.Select()
.Field(OrderTable.CustomerID)
.Count(OrderTable.OrderID)
.From(OrderTable.Orders)
.GroupBy(OrderTable.CustomerID)
.OrderBy(OrderTable.CustomerID)
.WhereField(OrderTable.OrderDate, Compare.GreaterThan, DBConst.DateTime(ordersAfter))
If I would be using the suggestion by Loadmaster (Oct 16 at 16:49) then I could do this:
using O = TestingDynasql.OrderTable;
...
DBQuery.Select()
.Field(O.CustomerID)
.Count(O.OrderID)
.From(O.Orders)
.GroupBy(O.CustomerID)
.OrderBy(O.CustomerID)
.WhereField(O.OrderDate, Compare.GreaterThan, DBConst.DateTime(ordersAfter))
However, I would prefer to bring the constant into scope without any repeated but needed name and dot qualifer at all (as you can do with Java's static imports) i.e. do this:
DBQuery.Select()
.Field(CustomerID)
.Count(OrderID)
.From(Orders)
.GroupBy(CustomerID)
.OrderBy(CustomerID)
.WhereField(OrderDate, Compare.GreaterThan, DBConst.DateTime(ordersAfter))
But it is not possible to use this kind of syntax with C# ?
By the way, does anyone know of a better .NET library for creating SQL than dynasql, which does not seem to be designed for being independent of the SQL execution (as the problem described here: http://dynasql.codeplex.com/Thread/View.aspx?ThreadId=81988) ?
/ Sven