Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4381594
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:40:08+00:00 2026-05-21T12:40:08+00:00

and should I save the DTOs directly to the database? public class Product {

  • 0

and should I save the DTOs directly to the database?

public class Product
{
    public virtual int ProductId { get; set; }
    public virtual string ProductCode { get; set; }
    public virtual string ProductName { get; set; }
    public virtual Category Category { get; set; }
}

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(x => x.ProductId);
        Map(x => x.ProductCode);
        Map(x => x.ProductName);
        References(x => x.Category).Column("CategoryId");                
    }

}

public class ProductDto
{
    public virtual int ProductId { get; set; }
    public virtual string ProductCode { get; set; }
    public virtual string ProductName { get; set; }
    public virtual int CategoryId { get; set; }
}

public class ProductDtoMap : ClassMap<ProductDto>
{
    public ProductDtoMap()
    {
        Table("Product");
        Id(x => x.ProductId);
        Map(x => x.ProductCode);
        Map(x => x.ProductName);
        Map(x => x.CategoryId);
    }

}

Here’s how I create, open and save record:

public ActionResult Input()
{
    return View(new ProductDto());
}

public ActionResult Edit(int id)
{ 
    using(var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
    {
        return View("Input", s.Get<ProductDto>(id));
    }
}


// Save
[HttpPost]
public ActionResult Input(ProductDto p)
{

    if (ModelState.IsValid)
    {
        using (var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
        {
            s.Merge(p);
            s.Flush();
        }
        return RedirectToAction("Index");
    }
    else
    {
        return View(p);
    }

}

Suffice to say, I wanted convenience, I want to persist the DTOs directly to database, and retrieve them back directly too.

Now what need do I have for Product class if I just use ProductDto exclusively for CRUD? I’ll just use Product class for reporting only 🙂

Is populating DTOs directly from ORM and saving them back directly to ORM a sound practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:40:09+00:00Added an answer on May 21, 2026 at 12:40 pm

    Yes if you can do it, it’s the best way. Don’t feel guilty about it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every change of data in some row in database should save the previous row
Please guide in the following class what I should save in. Please remember I
so I have following problem. I have a session class that should save it's
I am looking for where I should save my app stuff, and after a
Yesterday I asked the question on how I should save my files. After some
I have a small issue with a method that should save the text contained
Should I use NHibernate Collection Cascading to perform collection save-updates, or should I use
Should be a quick and easy question. Does System.Drawing.Graphics.DrawImage() or Image.Save() by default make
How can we save log-file of our application actions on SD-card? We should have
I always read that I should use model = Model(a=5, b=6) model.save() But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.