INamingStrategy being ignored by (Fluent) NHibernate? - nhibernate

I am trying to write a naming strategy for NHibernate that will prefix table names based on what assembly the poco is defined in. Right now my strategy is just trying to append any prefix at all to the tables just to prove I have things wired up right.
The problem that I am encountering is that I am able to create my INamingStrategy and attach it to the NHibernate configuration object, but it never seems to get used. Here is some example coded:
private MsSqlConfiguration GetDatabaseConfiguration()
{
var configuration = MsSqlConfiguration.MsSql2008
.ConnectionString(ConfigFileReader.GetConnectionString(ConnectionStringKey))
.ShowSql();
return configuration;
}
private FluentConfiguration GetFluentConfiguration()
{
return Fluently.Configure().Database(GetDatabaseConfiguration())
.Mappings(m =>
{
foreach (var assembly in GetAssembliesToLoadMappingsFrom())
m.FluentMappings.AddFromAssembly(assembly);
});
}
public global::NHibernate.Cfg.Configuration GetNHibernateConfiguration()
{
var nHibernateConfiguration = GetFluentConfiguration().BuildConfiguration();
var namingStrategy = GetNamingStrategy();
if (namingStrategy != null)
nHibernateConfiguration.SetNamingStrategy(namingStrategy);
return nHibernateConfiguration;
}
public void Build()
{
var schemaExport = new SchemaExport(GetNHibernateConfiguration());
schemaExport.Create(true, true);
}
By placing a breakpoint on the return statement in GetNHibernateConfiguration(), I am able to confirm that nHibernateConfiguration.NamingStrategy contains a reference to my strategy. However, placing breakpoints in every one of the INamingStrategy implementing members of that strategy shows that non of them are ever called. This is confirmed by looking at the generated schema, which has no prefixes.
Likewise, using the same approach to create a session factory, shows that CRUD operations also ignore the strategy.
I am I missing something obvious?
I am using NHibernate 2.1.1.4000

I think your strategy is too complicated. If you are using FluentNHibertate just provide the TableName convention into your initialization.
e.q.:
public class TableNameConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
}
}
and usage here:
public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{
/// <summary>
/// Get Conf Setup
/// </summary>
/// <returns>
/// Action of AutoMappingExpressions
/// </returns>
private Action<AutoMappingExpressions> GetSetup()
{
return c =>
{
c.FindIdentity = type => type.Name == "Id";
c.IsBaseType = this.IsBaseTypeConvention;
};
}
private Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<ReferenceConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
c.Add<PropertyNameConvention>();
};
}
public AutoPersistenceModel Generate()
{
var model =
new AutoPersistenceModel()
.AddEntityAssembly(Assembly.GetAssembly(typeof(User)))
.Where(
this.GetAutoMappingFilter).Conventions.Setup(this.GetConventions()).Setup(this.GetSetup()).
UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
return model;
}

Related

Required attribute on action parameter doesn't work

The code is like:
[HttpPost]
public ResultEntityVM Register([FromBody,Required] RegisterParam createAssessorParam)
{
if (ModelState.IsValid == false)
{
return null;
}
//other code
ResultEntityVM vm = new ResultEntityVM();
return vm;
}
When the parameter createAssessorParam is null, the value of ModelState.IsValid is true. Why?
If I want to auto judge the parameter is null or not, what can I do?
Don't I can only write the code:
if(RegisterParam != null)
{
//other
}
I've run into the same problem and solved it by implementing a custom action filter attribute evaluating all the validation attributes of the action method parameters.
I described the approach in this blog post, where I use ASP.NET Core 1.0, but the same approach should work with ASP.NET 4 as well.
here is #MarkVincze's answer adjusted for asp.net web
public class ValidateModelStateAttribute : System.Web.Http.Filters.ActionFilterAttribute
// there are two ActionFilterAttribute, one for MVC and another one for REST
{
/// <summary>
/// Called before the action method is invoked
/// </summary>
/// <param name="context"></param>
public override void OnActionExecuting(HttpActionContext context)
{
var parameters = context.ActionDescriptor.GetParameters();
foreach (var p in parameters)
{
if (context.ActionArguments.ContainsKey(p.ParameterName))
Validate(p, context.ActionArguments[p.ParameterName], context.ModelState);
}
if (!context.ModelState.IsValid)
{
context.Response = context.Request.CreateResponse(
HttpStatusCode.BadRequest,
context.ModelState.Select(_ => new { Parameter = _.Key, Errors = _.Value.Errors.Select(e => e.ErrorMessage ?? e.Exception.Message) }),
context.ControllerContext.Configuration.Formatters.JsonFormatter
);
}
}
private void Validate(HttpParameterDescriptor p, object argument, ModelStateDictionary modelState)
{
var attributes = p.GetCustomAttributes<ValidationAttribute>();
foreach (var attr in attributes)
{
if (!attr.IsValid(argument))
modelState.AddModelError(p.ParameterName, attr.FormatErrorMessage(p.ParameterName));
}
}
}
In WebAPI, an action parameter will never be null. It is always instantiated by the framework. So you would rather use the Required attribute on the properties of your view model if you want to ensure that they are present.

Fluent NHibernate Error: "Could Not Resolve Property"

Here's the (edited) domain class:
public class Patient : IntegerKeyEntity
{
...
public virtual string LastName
{
get
{
return Encoding.Unicode.GetString(encryptionService.Decrypt(LastNameEncrypted));
}
set
{
LastNameEncrypted = encryptionService.Encrypt(value);
}
}
/// <summary>
/// Contains the encrypted last name. Access via LastName for unencrypted version.
/// </summary>
public virtual byte[] LastNameEncrypted { get; set; }
...
}
Here's the Fluent mapping:
public class PatientMap : ClassMap<Patient>
{
public PatientMap()
{
...
Map(x => x.LastNameEncrypted, "LastName")
.Not.Nullable();
...
}
}
I don't map LastName in Patient. The table has a LastName column (a varbinary) and no LastNameEncrypted column.
Here's the query:
public virtual IQueryable<TResult> SatisfyingElementsFrom(IQueryable<T> candidates, int start, int limit, string sort, string dir)
{
if (this.MatchingCriteria != null)
{
return candidates.Where(this.MatchingCriteria).OrderBy(sort + " " + dir).Skip(start).Take(limit).ToList().ConvertAll(this.ResultMap).AsQueryable();
}
return candidates.ToList().ConvertAll(this.ResultMap).AsQueryable();
}
The return (inside the if block) is where the error is triggered. The error says "Could not resolve property LastName of Patient".
I am using NHibernate (v2.1.2.4000), Fluent NHibernate (v1.1.0.685) and NHibernate.Linq (v1.1.0.1001). I cannot update these DLLs.
Is it because I don't have a mapping for Patient.LastName? I don't have or need one. If that is the issue, how do I map/tell Fluent NHibernate to ignore that property?
PS: I am not using AutoMapping, only explicit mappings. They are loaded as follows. In my application, only cfg (an NHibernate.Cfg.Configuration object) and mappingAssemblies (which points to the one DLL with the mappings) have a value.
private static ISessionFactory CreateSessionFactoryFor(string[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, Configuration cfg, IPersistenceConfigurer persistenceConfigurer)
{
FluentConfiguration fluentConfiguration = Fluently.Configure(cfg);
if (persistenceConfigurer != null)
{
fluentConfiguration.Database(persistenceConfigurer);
}
fluentConfiguration.Mappings(m =>
{
foreach (var mappingAssembly in mappingAssemblies)
{
var assembly = Assembly.LoadFrom(MakeLoadReadyAssemblyName(mappingAssembly));
m.HbmMappings.AddFromAssembly(assembly);
m.FluentMappings.AddFromAssembly(assembly).Conventions.AddAssembly(assembly);
}
if (autoPersistenceModel != null)
{
m.AutoMappings.Add(autoPersistenceModel);
}
});
return fluentConfiguration.BuildSessionFactory();
}
This error happens when you do the query. Looking to your code I see only one thing that might cause the problem - that is MatchingCriteria:
return candidates.Where(this.MatchingCriteria)...
What type is stored in this.MatchingCriteria?
Try to replace it with inline conditions: in
..Where(<put_inline_criteria_here>)..

Ninject does not inject dependency

having problem with my Ninject construct. May be somebody can show me where I am doing it wrong..
ok.. here is Module I have:
public class WebPageModule:NinjectModule
{
public override void Load()
{
Bind<TranscriptPageMediaWidgetViewModelForWebPage>().ToSelf().InSingletonScope();
Bind<TranscriptPageTranscriptWidgetViewModelForWebPage>().ToSelf().InSingletonScope();
Bind<WebPageTranscriptProvider>().ToSelf().InSingletonScope();
Bind<ITranscriptProvider>().To<WebPageTranscriptProvider>().WhenInjectedInto<TranscriptPageTranscriptWidgetViewModelForWebPage>();
//Bind<ITranscriptProvider>().To<WebPageTranscriptProvider>();
Bind<ITranscriptRendererWidget>().To<TranscriptPageTranscriptWidgetViewModelForWebPage>();
Bind<IMediaRendererWidget>().To<TranscriptPageMediaWidgetViewModelForWebPage>();
}
}
Then in NinjectWebCommons.cs I have:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel(new WebPageModule(),new TweeterModule(), new BookmarkModule());
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
kernel.Settings.AllowNullInjection = true;//http://stackoverflow.com/questions/10517962/using-default-parameter-values-with-ninject-3-0
RegisterServices(kernel);
return kernel;
}
then I use the property injection:
https://github.com/ninject/ninject/wiki/Injection-Patterns
in my "public class TranscriptPageTranscriptWidgetViewModelForWebPage : ITranscriptRendererWidget"
here it is:
[Inject]
public ITranscriptProvider TranscriptProvider
{
get { return _transcriptProvider; }
set { _transcriptProvider = value; }
}
but, when I am going into the constructor and trying to use _transcriptProvider it is NULL:
public TranscriptPageTranscriptWidgetViewModelForWebPage(string dataEndpoint, string focusCue)
{
InitParentInterfaceProperties();
Transcript = _transcriptProvider.GetTranscript(new Uri(dataEndpoint));
FocusCue = focusCue.Replace("*", "").ToLower();
}
Any ideas what I am doing wrong? thanks!
Al
Looks like you're trying to access the property within the constructor.
.NET's object creation semantics are such that this simply cannot be made to work (which is one of lots of good reasons to try very hard to achieve things with constructor injection unless you really are dealing with an optional dependency)

Implementing a flexible searching infrastructure using nHibernate

My aim is to implement a quite generic search mechanism. Here's the general idea:
you can search based on any property of the entity you're searching for (for example- by Employee's salary, or by Department name etc.).
Each property you can search by is represented by a class, which inherits from EntityProperty:
public abstract class EntityProperty<T>
where T:Entity
{
public enum Operator
{
In,
NotIn,
}
/// <summary>
/// Name of the property
/// </summary>
public abstract string Name { get; }
//Add a search term to the given query, using the given values
public abstract IQueryable<T> AddSearchTerm(IQueryable<T> query, IEnumerable<object> values);
public abstract IQueryable<T> AddSortingTerm(IQueryable<T> query);
protected Operator _operator = Operator.In;
protected bool _sortAscending = false;
public EntityProperty(Operator op)
{
_operator = op;
}
//use this c'tor if you're using the property for sorting only
public EntityProperty(bool sortAscending)
{
_sortAscending = sortAscending;
}
}
all of the properties you're searching / sorting by are stored in a simple collection class:
public class SearchParametersCollection<T>
where T: Entity
{
public IDictionary<EntityProperty<T>,IEnumerable<object>> SearchProperties { get; private set; }
public IList<EntityProperty<T>> SortProperties { get; private set; }
public SearchParametersCollection()
{
SearchProperties = new Dictionary<EntityProperty<T>, IEnumerable<object>>();
SortProperties = new List<EntityProperty<T>>();
}
public void AddSearchProperty(EntityProperty<T> property, IEnumerable<object> values)
{
SearchProperties.Add(property, values);
}
public void AddSortProperty(EntityProperty<T> property)
{
if (SortProperties.Contains(property))
{
throw new ArgumentException(string.Format("property {0} already exists in sorting order", property.Name));
}
SortProperties.Add(property);
}
}
now, all the repository class has to do is:
protected IEnumerable<T> Search<T>(SearchParametersCollection<T> parameters)
where T : Entity
{
IQueryable<T> query = this.Session.Linq<T>();
foreach (var searchParam in parameters.SearchProperties)
{
query = searchParam.Key.AddSearchTerm(query, searchParam.Value);
}
//add order
foreach (var sortParam in parameters.SortProperties)
{
query = sortParam.AddSortingTerm(query);
}
return query.AsEnumerable();
}
for example, here's a class which implements searching a user by their full name:
public class UserFullName : EntityProperty<User>
{
public override string Name
{
get { return "Full Name"; }
}
public override IQueryable<User> AddSearchTerm(IQueryable<User> query, IEnumerable<object> values)
{
switch (_operator)
{
case Operator.In:
//btw- this doesn't work with nHibernate... :(
return query.Where(u => (values.Cast<string>().Count(v => u.FullName.Contains(v)) > 0));
case Operator.NotIn:
return query.Where(u => (values.Cast<string>().Count(v => u.FullName.Contains(v)) == 0));
default:
throw new InvalidOperationException("Unrecognized operator " + _operator.ToString());
}
}
public override IQueryable<User> AddSortingTerm(IQueryable<User> query)
{
return (_sortAscending) ? query.OrderBy(u => u.FullName) : query.OrderByDescending(u => u.FullName);
}
public UserFullName(bool sortAscending)
: base(sortAscending)
{
}
public UserFullName(Operator op)
: base(op)
{
}
}
my questions are:
1. firstly- am I even on the right track? I don't know of any well-known method for achieving what I want, but I may be wrong...
2. it seems to me that the Properties classes should be in the domain layer and not in the DAL, since I'd like the controller layers to be able to use them. However, that prevents me from using any nHibernate-specific implementation of the search (i.e any other interface but Linq). Can anybody think of a solution that would enable me to utilize the full power of nH while keeping these classes visible to upper layers? I've thought about moving them to the 'Common' project, but 'Common' has no knowledge of the Model entities, and I'd like to keep it that way.
3. as you can see by my comment for the AddSearchTerm method- I haven't really been able to implement 'in' operator using nH (I'm using nH 2.1.2 with Linq provider). any sugggestions in that respect would be appriciated. (see also my question from yesterday).
thanks!
If you need good API to query NHIbernate objects then you should use ICriteria (for NH 2.x) or QueryOver (for NH 3.x).
You over complicating DAL with these searches. Ayende has a nice post about why you should not do it
I ended up using query objects, which greatly simplified things.

Excluding some tables from Fluent Nhibernate schema Generation

I have some existing asp.net membership and roles tables in a legacy db and I am mapping them to new entities with Fluent Nhibernate.
I also generate the schema directly from Fluent Nhibernate and I then manually tweak the generated sql script to exclude the existing tables.
Is it possible to say to Fluent Nhibernate to exclude from generation certain tables?
SchemaAction.None() in your ClassMap.
Another option would be to create an attribute, say
public class DoNotAutoPersistAttribute : Attribute
{
}
Then in AutoPersistenceModelGenerator you could check for this attribute in the Where clause of AddEntityAssembly.
I've managed this with an attribute + convention:
public enum SchemaAction
{
None
}
[Serializable]
[AttributeUsage(AttributeTargets.Class)]
public class SchemaActionAttribute : Attribute
{
private readonly SchemaAction schemaAction = SchemaAction.None;
public SchemaActionAttribute()
{
}
public SchemaActionAttribute(SchemaAction schemaAction)
{
this.schemaAction = schemaAction;
}
public SchemaAction GetSchemaAction()
{
return schemaAction;
}
}
/// <summary>
/// overrides the default action for entities when creating/updating the schema
/// based on the class having a Schema attribute (<see cref="SchemaActionAttribute" />)
/// </summary>
public class SchemaActionConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
object[] attributes = instance.EntityType.GetCustomAttributes(true);
foreach (object t in attributes)
{
if (t is SchemaActionAttribute)
{
var a = (SchemaActionAttribute) t;
switch(a.GetSchemaAction())
{
case SchemaAction.None:
instance.SchemaAction.None();
return;
default: throw new ApplicationException("That schema action:" + a.GetSchemaAction().ToString() + " is not currently implemented.");
}
}
}
}
}
...
[SchemaAction(SchemaAction.None)]
public class TextItem : Entity
...