c# - How to create domain entities for a NoSQL application -


i want create application makes use of nosql database in such way plays nicely domain entities.

right when create "domain entity" mongodb have define this:

public class user {     [bsonid]     public int id {get;set;}      [bsonelement("username")]     public string username {get;set;}      [bsonelement("email")]     public string email {get;set;} } 

but means entity isn't persistence ignorant. how can achieve when using nosql database mongodb?

one way define poco class in domain

namespace myapp.domain {     public class user     {         public int id { get; set; }         public string username { get; set; }         public string email { get; set; }     } }    

and define same class each persistence type, example mongodb

namespace myapp.mongodb {     public class user     {         [bsonid]         public int id { get; set; }          [bsonelement("username")]         public string username { get; set; }          [bsonelement("email")]         public string email { get; set; }     } } 

you business logic interact domain classes through repository interfaces, , when persist data, need copy instance over.

you don't have write copying data code manually because automapper can that. alternatively, can use simplified code copy data between domain classes below:

/// <summary>         /// copy public fields instance of source type tv instance of destination type t.         ///  source property copied if there property in destination type same name.         /// instance, tv.name copied t.name         /// </summary>         /// <typeparam name="t">the destination type</typeparam>         /// <typeparam name="tv">the source type</typeparam>         /// <param name="input">the source data</param>         /// <param name="existinginstance">the instance want copy values to, if null new instance created</param>         /// <returns>an instance of type t</returns>         public static t copyfields< tv,t>(tv input, t existinginstance=null)where  t:class tv:class          {             var sourcepublicfields = typeof (tv).getproperties();              var instance =existinginstance ?? activator.createinstance<t>();              var destinationpublicfields = typeof(t).getproperties();              debug.writeline("copying data source type {0} destination type {1}", typeof(tv), typeof(t));              foreach (var field in sourcepublicfields)             {                 var destinationfield = destinationpublicfields.firstordefault(it => it.name == field.name);                 if (destinationfield == null || destinationfield.propertytype != field.propertytype)                 {                     debug.writeline("no destination field matched source field. source field  name {0}, source field type {1} ", field.name, field.propertytype);                     continue;                 }                  var sourcevalue = field.getvalue(input);                 //set value                 destinationfield.setvalue(instance,sourcevalue);              }               return instance;         } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -