Where do I create the mapping files in a SharpArchitechture project - asp.net-mvc

I am following the tutorial on sharparchitecture.net. I have created a few entities using T4, but I can't figure out where the mapping to the database is done?
I would like to create some relations in the mapping files, but should I add a new mapping file or are there already created one with T4?

SharpArchitecture uses FluentNHibernate to automagically create NHibernate mappings based on your Entities as defined in project PROJECT.Core. It will define relationships based on the domain model. You will find that you frequently need to customize your mappings.
The project PROJECT.Data is where you will perform overrides to the default mapping. In this project you will find a folder called NHibernateMaps with several classes to setup the default mapping strategy. Step 1 would be to evaluate the default mapping strategy to see if you need to make any systemwide changes to the default strategies. Second, you may want to override a mapping for an specific entity. To do this create a new class called [EntityClass]Map that looks like the following:
public class EntityMap : IAutoMappingOverride<Entity> {
public void Override(AutoMapping<Entity> mapping) {
//use the mapping. to override default mappings. Here is just an example
mapping.References(x => x.EntityCategory).Fetch.Join();
mapping.References(x => x.EntitySubItem).NotFound.Ignore();
}
}
Your application ties this all up in the InitializeNHibernateSession method within the global.asax.cs.

Related

Automatically updating database with multiple DbContexts

I'm using EF6 in ASP.NET 5 project. Where I'm using ASP.NET Identity as the authentication mechanism. As anyone would do, I have modeled my domain objects in a separate dll project and for data access logic I have a separate project. This data project contains EF migrations, DbContext for domain models, repositories and Unit of work.
And I have a service layer which is contacted by ASP.NET controllers and this service layer will communicate with data layer and do required operations.
In the main ASP.NET web project I have the default DbContext which is related to Identity and it's migrations.
Having two DbContexts somehow prevents me from updating database automatically. If I had only one DbContext after I create migrations for model changes, it will automatically run on the first time I try to access the website. This doesn't happen anymore, always I have to run the "update-database" command manually.
One solution I have right now is to add a reference to "Microsoft.AspNet.Identity.EntityFramework" in my data project and use Identity context contain my domain tables. But adding an ASP.NET reference in my data project is something I don't want to do unless I have no other options. Because data layer is not even communicating directly with web layer.
Even though this video by Scott Allen discuss about this issue, a solution is not proposed.
You can kick in migrations explicitly by using database initializer and using MigrateDatabaseToLatestVersion. This initializer allows you explicitly state the context and configuration you want to use. Something like:
SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
You can also call all the logic manually, effectively doing the same as Update-Database cmdlet, when you want during app start. Look at DbMigrator class.
You can run migration inside each DbContext constructor separately.
public class DataContext: DbContext
{
static DaraContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
}
}
public class ApplicationDbContext : IndetityDbContext
{
static ApplicationDbContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
}
}

Partial class on a model when Entity Framework is in separate library

I am not sure if this is the correct way to go, if so, please advise otherwise.
This is an ASP.Net MVC 4 site using EF 5.x
Suppose you have your Entity Framework in a class library on it's own.
Code Generation Item has now generated all of your Models (the xxx.tt section of your EF mode)
This project is then added/referenced in the development of a site.
You can now access the data via the EF.
Now - in the site project I want to create a partial class of one of my EF models, for example "Users", with an additional property that isn't in the DB.
In the past on a web forms project when the EF was part of the project and not a reference I would simply create the partial class and all would be good; my "Users" would now have a bunch of other stuff in it that wasn't database related but needed on the "User".
I can't seem to get this to work in this MVC project where the EF is in a separate project.
I have tried doing this for example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyTestEntity.Entity;
namespace MyTestMVCSite.Models
{
public partial class Email
{
public string OtherEmail {
get { return "me#myEmail.com"; }
}
}
}
I have also tried inheriting the EF models class, like this:
public partial class Email : MyTestEntity.Entity.Email
{
public string OtherEmail {
get { return "me#myEmail.com"; }
}
}
Nothing I seem to be doing gives me access to "OtherEmail"
What I actually want to be able to do is create a partial class for some of my models and then have this partial class implement an interface so i can inject an instance of this interface into another object rather than overloading.
Am i talking crazy nonsense?
You cannot have two partial classes referring to the same class in two different assemblies (projects). Once the assembly is compiled, the meta-data is baked in, and your classes are no longer partial. Partial classes allows you to split the definition of the same class into two files.
Is it possible to have two partial classes in different assemblies represent the same class?
If you wish to augment your models with additional properties used for display purposes, then you should consider using view models, and a mechanism for mapping data to and from your models to view models.
You can then perform validation independently from the model based on the current view. View models will also protect you from accidentally exposing properties on your model that you do not wish users to alter through post data, even if you haven't explicitly specified them in your view.

Where should Grails Command objects be placed in the project structure?

I have a class called LoginCommand in domain/my/package/name
class LoginCommand {
String emailAddress
String password
}
My question is why is a table be auto generated in my database for a ***Command object in grails? Are these command objects supposed to be placed ouside of /domain to avoid auto generation of a table by hibernate/orm.
They should not go in grails-app/domain; they are not domain classes. Place them in src/groovy. Alternatively, a common convention is to put the command class in the same file as the controller that uses it.
Take a look at the Convention Over Configuration section in the grails manual to get an idea of what goes where.

Is it good to store CustomMembershipUser and Agregate classes in domain library

I need just a few confirmation that I do some stuff in the right way or I make horrible mistake :)
1) I put my data access layer (MyProject.Domain) in sepparate assembly. There I have entity object "User" that has properties in 1:1 relation with "User" table in my database. I also extend this user from "MembershipUser" because I use custom schema for membership. Is this good location to store MembershipUser entity?
2) I have "Image" table in database and "Image" entity in my domain library. Image in database has "AuthorId" column which is FK to "User" table. Also image contain list of "comments". So I structure Image domain object like this:
public class Domain
{
public int ImageId{get;set;}
public string Name{get;set;}
public Author Author{get;set;}
public IEnumerable<Comment> Comments{get;set;}
}
Is this good way or maybe I should assemble all data in a ViewModel class?
You should really make a distinction between domain models and view models. Domain models are those classes that represent your domain business entities. It could be EF autogenerated classes or whatever. So even being aggregate classes they are still domain models as they aggregate domain entities and could be stored alongside with other domain models.
View models are classes that are specifically defined for a given view. The view models are always defined inside the ASP.NET MVC project because they are tightly coupled to specific views which are themselves defined in the ASP.NET MVC project. Domain models could be defined in separate assembly. They are intended to be resused in other applications as well. Think of your domain models as the core of your business. If tomorrow ASP.NET MVC is no longer modern, and something else comes out you should still be able to reuse your domain models. The view models are only a specific representation of your domain models for some given and specific view.

Problem with WCF Data service exposing custom partial methods of EF4 model

I am exploring the idea of implementing a web service api using WCF Data Services and EF4. Realizing that some operations require complex business logic, I decided to create a partial class the same name as the main EF data context partial class and implement additional methods there to handle the more complex business logic. When the EF context object is used directly, the additional method shows up (via intellisense) and works properly. When the EF classes are exposed through a WCF Data Service and a Service Reference is created and consumed in another project, the new method does not show up in intellisense or in the generated Service.cs file (of course, I updated the reference and even deleted it and re-added it). The native data methods (i.e. context.AddObject() and context.AddToPeople()) work properly, but the new method isn't even available.
My EF classes look something like this:
namespace PeopleModel
{
//EF generated class
public partial class PeopleEntities : ObjectContext
{
//Constructors here
//Partial Methods here
//etc....
}
//Entity classes here
//My added partial class
public partial class PeopleEntities
{
public void AddPerson(Person person)
{
base.AddObject("People", person);
}
}
}
There's nothing special about the .svc file. The Reference.cs file containing the auto generated proxy classes do not have the new "AddPerson()" method.
My questions are:
1. Any idea why the web service doesn't see the added partial class, but when directly using the EF objects the method is there and works properly?
2. Is using a partial class with additional methods a good solution to the problem of handling complex business rules with an EF generated model?
I like the idea of letting the oData framework provide a querying mechanism on the exposed data objects and the fact that you can have a restful web service with some of the benefits of SOAP.
Service operations are only recognized if they are present on the class which derives from DataService. The WCF Data Service will not look into the context class for these. Also note that methods are not exposed by default, you need to attribute them with either WebGet or WebInvoke and allow access to them in your InitializeService implementation.
http://msdn.microsoft.com/en-us/library/cc668788.aspx

Resources