jonwagner / Insight.Database

Fast, lightweight .NET micro-ORM
Other
861 stars 145 forks source link

Method Mapping configuration #362

Closed avber closed 6 years ago

avber commented 6 years ago

Type

What kind of issue is this?

[ ] Bug report. [x ] Feature request.



Could you make it configurable?

e.g.
Beers_Insert
Beers_Get
Beers_GetAll
Beers_Update
Beers_Delete
Beers_SomeSP
etc

can be used with
//pseudocode
interface IBeerRep{
   Add(Beer b)
  Beer Load(id)
  IEnumerable<Beer> LoadAll()
  Update(Beer b)
  Delete(Beer b)
  Delete(id)

  SomeSP()
}

One option is to use attributes to generate code
[TableName("Beers")]
interface IBeerRep{
   [SpName["Beers_Insert"]]
   Add(Beer b)
  Beer Load(id)
  IEnumerable<Beer> LoadAll()
  Update(Beer b)
  Delete(Beer b)
  Delete(id)

  SomeSP()
}

This code can be auto generated using tools like T4
-------------

Method Mapping

When generating the implementation of the interface, Insight uses the DbConnection extension methods. It maps the interface methods by the return type using the following rules:
• void, method name starts with "Insert/Upsert", first param is IEnumerable => InsertList
• void, method name starts with "Insert/Upsert", first param is updatable -> Insert
• void, otherwise => Execute
• IList<T> => Query
• Results<> => QueryResults
•other type, primitive type => ExecuteScalar
•other type, otherwise => Single
jonwagner commented 6 years ago

I don't understand what you are trying to do or what limitation you are seeing. Can you give a specific example of something you cannot do with the existing library?

avber commented 6 years ago

Our naming convention for database objects is as follows:

Table name: Beers

CRUD stored procedures should be named

Beers_Insert Beers_Get Beers_GetAll Beers_Update Beers_Delete

If I understand correctly, right now the repository interface should look like

IBeersRepo { Beers_Insert Beers_Get Beers_GetAll Beers_Update Beers_Delete }

which is not very desirable.

A better interface could be

IBeersRepo { Insert Get GetAll Update Delete }

jonwagner commented 6 years ago

You can rename the stored procedures with the stored procedure attribute

IBeersRepo { [StoredProcedure("Insert")] void Insert(Beer beer); }

Jaxelr commented 6 years ago

Details on the wiki: https://github.com/jonwagner/Insight.Database/wiki/Auto-Interface-Implementation#sqlattribute

or check the Interface tests: https://github.com/jonwagner/Insight.Database/blob/7f541df8bd87d6703e1876fce06414c930a78fec/Insight.Tests/InterfaceTests.cs#L55-L56

avber commented 6 years ago

OK, thanks