Save data via web service using NHibernate?

We currently have an application that retrieves data from a server through a web service and populates a DataSet. Then, API users manipulate it through objects, which, in turn, change the data set. The changes are then serialized, compressed, and sent back to the server for updating.

However, I started using NHibernate in projects, and I really like the disconnected nature of POCO objects. The problem that we have now is that our objects are tied to an internal dataset, that they cannot be used in many situations, and as a result, we create duplicate POCO objects to go back and forth.

Batch.GetBatch() -> calls to web server and populates an internal dataset
Batch.SaveBatch() -> send changes to web server from dataset 

Is there a way to get a similar model that we use, accessed through a web service, but use NHibernate?

Change 1

I have a partial solution that works and is saved through a web service but has two problems.

  • I need to serialize and send my entire collection, not just the changed items.
  • If I try to refill the collection when returning my objects, then any links that I had will be lost.

Here is my sample solution.

Client side

public IList<Job> GetAll()
{
    return coreWebService
      .GetJobs()
      .BinaryDeserialize<IList<Job>>();
}

public IList<Job> Save(IList<Job> Jobs)
{
    return coreWebService
             .Save(Jobs.BinarySerialize())
             .BinaryDeserialize<IList<Job>>();
}

Server side

[WebMethod]
public byte[] GetJobs()
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        return (from j in session.Linq<Job>()
                select j).ToList().BinarySerialize();
    }
}

[WebMethod]
public byte[] Save(byte[] JobBytes)
{
    var Jobs = JobBytes.BinaryDeserialize<IList<Job>>();

    using (ISession session = NHibernateHelper.OpenSession())
    using (ITransaction transaction = session.BeginTransaction())
    {
        foreach (var job in Jobs)
        {
            session.SaveOrUpdate(job);
        }
        transaction.Commit();
    }

    return Jobs.BinarySerialize();
}

As you can see, every time I send the entire collection to the server, and then return the entire collection. But I get a replaced collection instead of a merged / updated collection. Not to mention the fact that it seems extremely inefficient to send all the data back and forth, when only part of it can be changed.

2

. , , .

DataSet, . , , , / . .

+3
2

, , , :

, DTO.

, , , , , . .

, , .

, , REST/ - . , automapper, , .

, , , , , , , .

0

, :

http://thatextramile.be/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/

ORM- ?

- . ( ) .

NHibernate. ORM-. POCO. "" , .

DTO. POCO. .

, "". ORM- ( NHibernate)... Domain-DTO-Objects. , .

Domain-DTO. . "Goldie Locks" "just right". , , -DTO .

Domain-DTO- ( ), . Domain-DTO ORM. NHibernate ORM-.

"", (NHiberntae) ".Merge()".

        // ormItem is any NHibernate poco
        using (ISession session = ISessionCreator.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.BeginTransaction();
                ParkingAreaNHEntity mergedItem = session.Merge(ormItem);
                transaction.Commit();
            }
        }

.Merge - . Entity Framework . Boo.

? . , ? .

. DTO (Poco's), "" ORM, ORM, .

datalayer ADO.NET, EF, NHibernate - . "", , ORM, .

. , , ORM- .

, " ". . /orm .

( ) EF NHibernate, .

-DTO 95% ORM? . 5%, .

DataSets, TSQL, . , , , CRUD, .

TSQL . 1999 . , .

.

PS .Merge( EF), : (boo microsoft)

http://www.entityframeworktutorial.net/EntityFramework4.3/update-many-to-many-entity-using-dbcontext.aspx

0

Source: https://habr.com/ru/post/1721131/


All Articles