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

  • Home
  • SEARCH
  • 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 7704951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:47:33+00:00 2026-05-31T23:47:33+00:00

I have my Fluent NHibernate setup code in a DataAccess project which is accessed

  • 0

I have my Fluent NHibernate setup code in a DataAccess project which is accessed from my Web application.

As part of the NHibernate configuration I defined a Model to be used when mapping to the database.

public sealed class MyTypeMapping: ClassMap<MyType>

This model MyType is identical to a model that I require in my web layer to be passed to a view. I’d also like to add some additional meta data to this model such as Required and DisplayName.

I’m sure this is a common scenaro where models in the data access layer are the same as models required in the web layer.

How is this commonly handled? Is it an accepted practice to make a view dependent on a model in the data access layer or should I simply create a ViewModel to abstract the details away from the view?

I’m quite new to ORM and it feels odd to have my models outside of my Models folder in the MVC project and duplicating the model doesn’t feel right either.

How do you handle this?

  • 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-31T23:47:33+00:00Added an answer on May 31, 2026 at 11:47 pm

    Is it an accepted practice to make a view dependent on a model in the data access layer

    For some people maybe, for me no.

    or should I simply create a ViewModel to abstract the details away from the view?

    That’s what I would do.

    Of course if you don’t want to follow my suggestion of using view models you have a couple of possibilities. So let’s suppose that you have the following domain model:

    public class MyDomainModel
    {
        public string Foo { get; set; }
    }
    

    but you wanted to associate some metadata to it.

    The first possibility that you have and which is built-in is to use the [MetadataType] attribute:

    [MetadataType(typeof(MyDomainModelMetadata))]
    public class MyDomainModel
    {
        public string Foo { get; set; }
    }
    

    and have a separate class for the metadata:

    public class MyDomainModelMetadata
    {
        [Display(Name = "foo bar")]
        public object Foo { get; set; }
    }
    

    But still your DAL must know about metadata and presentation logic which is not quite good. The MyDomainModelMetadata class must be defined in your DAL because attributes represent metadata that is baked into the assembly at compile time.

    Which brings us to the second possibility consisting of writing a custom model metadata provider:

    public class MyMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
        {
            // TODO: you could keep a static hashtable that will map between your
            // domain model type and the associated metadata type in your UI layer
            // but for the purpose of this demonstration I have hardcoded them to simplify
            if (containerType == typeof(MyDomainModel))
            {
                return GetMetadataForProperty(
                    modelAccessor, 
                    typeof(MyDomainModelMetadata), 
                    propertyName
                );
            }
            return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        }
    }
    

    which will be registered in your Application_Start and which will replace the default metadata provider:

    ModelMetadataProviders.Current = new MyMetadataProvider();
    

    Now you can get rid of the MetadataType attribute on your domain model and have the MyDomainModelMetadata class in your UI layer (the MVC application itself).

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

Sidebar

Related Questions

I have a WPF Application using Fluent NHibernate 1.0 RTM and System.Data.SQLite 1.0.65 that
I am changing my application to use Fluent NHibernate. I have created my Fluent
I have a fluent nhibernate configuration in my app. Running latest of both. I
I have to write a query in Fluent NHibernate for Select * from Users
I have dictionary which I'm mapping using Fluent NHibernate. The dictionary has a complex
I'm using NHibernate with Fluent NHibernate. I have code where I start a transaction,
I'm using Fluent NHibernate in an Asp.net MVC application. I have it set up
I am trying to use Fluent NHibernate. I have setup two table Products and
I'm using Fluent NHibernate and the setup I have is as follows: An Address
What references are necessary to have fluent nhibernate work with mysql. I have downloaded

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.