C# Cassandra UDT Mappings Configuration Better Approach - c#

Is there any better way to Write UDTMaps is the same way as Tables using Cassandra Database with C# Driver
Suppose there is a Table called Users(Id, name text, Address frozen )in Cassandra Database.
now to map this to C#
for Tables I can write as below
public class AppMappings : Mappings
{
public AppMappings()
{
For<User>()
.TableName("Users")
.Column(u => u.Id, cm => cm.WithName("userId"))
.Column(u => u.Name, cm => cm.WithName("username"))
.Column(u => u.AddressDetails , cm => cm.WithName("address"))
.PartitionKey(u=> u.Id);
}
}
public class User
{
public Guid Id {get;set;}
public string Name {get;set;}
public Address AddressDetails {get;set;}
}
public class Address {
public string State {get;set;}
public string City {get;set;}
}
Now I can Apply any Configurations with Single Class AppMappings , it can have many mappings with tables. With this one line I can initialize all mappings
MappingConfiguration.Global.Define<AppMappings>();
but inroder to apply UDT I need to add the below line manually.
session.UserDefinedTypes.Define(UdtMap.For<Address>("address_udt"));
Assume I have 10/20 UDTs then I need to add this line for every UDT. instead is there any way to add UDT Mappings in one place like Mappings?
or better approach to add UDT Mappings ?

Unfortunately the driver doesn't have an utility like that one for UDT mappings but you can create something similar like this:
public abstract class CustomUdtMappings
{
private readonly IDictionary<Type, UdtMap> _definitions = new Dictionary<Type, UdtMap>();
public UdtMap[] Definitions => _definitions.Values.ToArray();
public UdtMap<TPoco> For<TPoco>(string udtName = null, string keyspace = null) where TPoco : new()
{
if (_definitions.TryGetValue(typeof(TPoco), out var map) == false)
{
map = UdtMap.For<TPoco>(udtName, keyspace);
_definitions.Add(typeof(TPoco), map);
}
return (UdtMap<TPoco>) map;
}
}
Then create your mappings class that contains the udt mappings:
public class MyAppUdtMappings : CustomUdtMappings
{
public MyAppUdtMappings()
{
For<Udt1>("udt1")
.Map(udt => udt.Id, "iid")
.Map(udt => udt.Column1, "aasd");
For<Udt2>("udt2")
.Map(udt => udt.Id, "iiid")
.Map(udt => udt.Column1, "aaasd");
}
}
And you can use it this way:
await session.UserDefinedTypes.DefineAsync(new MyAppUdtMappings().Definitions).ConfigureAwait(false);
I think it makes a lot of sense to add something like this to the driver so I created https://datastax-oss.atlassian.net/browse/CSHARP-897 to track this.

Related

Using private fields with the Cassandra C# driver

I'm trying to migrate one of my modules from Postgres (with EF) to Cassandra.
Here is my best try for Cassandra mappings:
internal sealed class UserMappings : Mappings
{
public UserMappings()
{
For<User>().TableName("users")
.PartitionKey(x => x.Id)
.ClusteringKey(x => x.Id)
.Column(x => x.Id, x => x.WithDbType<Guid>().WithName("id"))
// I want to add mappings for password Hash here
}
}
The first problem is that I use VO for completive safety but want to store primitives in database.
Example VO for entity id:
public record UserId
{
public Guid Value { get; }
public UserId(Guid value)
{
if (value == Guid.Empty) throw new Exception("Invalid UserId");
Value = value;
}
public static implicit operator Guid(UserId id) => id.Value;
public static implicit operator UserId(Guid id) => new(id);
}
Secondly, my entity has private fields and I don't know how to map them to the database.
internal class User
{
private User()
{
}
public User(/*...*/)
{
//...
}
private string _passwordHash;
public UserId Id { get; }
//...
}
Also is public parameterless constructor required?
It sounds like you want to have some business logic in the classes that you are using to map to your database. I would recommend creating new classes that have only public properties and no logic whatsoever (i.e. POCOs) and then mapping these objects into your domain objects either "manually" or with a library like AutoMapper. This has the benefit of keeping your domain objects separate from the database schema.
The DataStax C# driver mapper will not be able to map private fields or properties that don't have a setter. It is able to map properties with a private setter though so you might want to leverage that instead.
Also keep in mind that you will need to provide a custom TypeConverter to the Mapper or Table objects if you use custom types in your mapping. You might get away with not implementing a TypeConveter if you have implicit operators to convert these types (like your UserId class) but I'm not 100% sure.
On the constructor issue, I think having a private empty constructor is enough.

Use enum as FK in EF6

We have a enum Supplier
But now we need to also have some Domain data on that relation
So in 99.9% in the domain code we doe operations on the enum like product.Supplier == Suppliers.FedEx
But now we also have added product.SupplierInfo.CanAdjustPickupTime where SupplierInfo is a Entity and not just a simple enum type.
I have tried these configs
Property(p => p.Supplier)
.IsRequired()
.HasColumnName("SupplierId");
HasRequired(p => p.SupplierInfo)
.WithMany()
.HasForeignKey(p => p.Supplier); //I have also tried casting to int doing .HasForeignKey(p => (int)p.Supplier)
This will fail with
The ResultType of the specified expression is not compatible with the
required type. The expression ResultType is
'MyApp.Model.Suppliers' but the required type is
'Edm.Int32'. Parameter name: keyValues[0]
Also tried
Property(l => l.Supplier)
.IsRequired()
.HasColumnName("SupplierId");
HasRequired(p => p.SupplierInfo)
.WithMany()
.Map(m => m.MapKey("SupplierId"));
This will offcourse give the good old
One or more validation errors were detected during model generation:
SupplierId: Name: Each property name in a type must be unique.
Property name 'SupplierId' is already defined.
I could offcourse define SupplierId as a Property use that with HasForeignKey But then I need to change to .SuppliedId == (int)Suppliers.FedEx etc. Not really a solution.
I could also add a property enum that uses the SupplierId property as backing field, but this will not work with Expressions since it needs to use real mapped DB properties
Any ideas?
I have classes:
public class Agreement
{
public int Id { get; set; }
public AgreementStateTypeEnum AgreementStateId { get; set; }
}
public class AgreementState
{
public int Id { get; set; }
public string Title { get; set; }
}
context:
public class AgreementContext :DbContext
{
public AgreementContext() : base("SqlConnection") { }
public DbSet<Agreement> Agreements { get; set; }
}
In method OnModelCreating I wrote nothing.
My enum:
public enum AgreementStateTypeEnum : int
{
InReviewing = 1,
Confirmed = 2,
Rejected = 3
}
In database: in table Agreements I have foreign key AgreementStateId - it is link to table AgreementStates.
Everything is working. For example:
var temp = context.Agreements.First(x => x.AgreementStateId == AgreementStateTypeEnum.Confirmed);
I use enum how foreign key.
Finally I found the problem. (I'm using EF6, NET 4.5)
So, if you create a type Enum in your code, you couldn't create a relationship with other property virtual.
//This is wrong, when do you create a foreignkey using a type enum
//Do You should remove that's code on in your class Map.
HasRequired(p => p.SupplierInfo)
.WithMany()
.HasForeignKey(p => p.Supplier); //I have also tried casting to int doing
.HasForeignKey(p => (int)p.Supplier)
If did you created a type enum it means that you don't need for a table return data throught for a join in EF.
So, the correct code it is:
public class MyClass{
public enum myEnumType {
FedEx,
Olther
}
public int id {get;set;}
public myEnumType Supplier {get;set;}
}
//My class Map (using Fluent...)
public class MyClassMap {
HasKey(t => t.Id);
Property(t => t.Id).HasColumnName("Id");
//The type [supplier] should be [int] in database.
Property(t => t.Supplier).HasColumnName("supplier");
//That's all, you don't need write relationship, int this case
//Because, when the data returns, the EF will to do the conversion for you.
}
I hope that's useful
The best way I have found to deal with this scenario is to map Supplier as a regular domain object and create a separate class of known supplier IDs.
public class KnownSupplierIds
{
public const int FedEx = 1;
public const int UPS = 2;
// etc.
}
if (product.Supplier.SupplierId == KnownSupplierIds.Fedex) { ... };
When your code needs to check the supplier, it can compare the IDs; when you need additional info from the domain model you just load the Supplier. The reason I prefer using a class of constants instead of an enum is that the pattern works for string comparisons also and there's no need to cast.

Orchard 1.8 - Creating a part corresponding to a 1-n relationship

I need to create a part to attach to an existing content type. The model associated to this part has no variables except a list of items, corresponding to a 1-N relationship with the data contained in a table I created. If I use the Record approach, creating a Model containing only the list, it will generate a "fake" table with no other data except its Id. This is the current code:
**Model**
public class MyPartRecord : ContentPartRecord
{ public virtual IList<OtherRecord> MyList { get; set; } }
public class MyPart : ContentPart<MyPartRecord>
{
public IList<OtherRecord> MyList {
get { return Record.MyList; }
}
public class OtherRecord
{
public virtual string Var1 { get; set; }
public virtual int Var2 { get; set; }
}
**Migration**
SchemaBuilder.CreateTable("MyPartRecord", table => table
.ContentPartRecord());
SchemaBuilder.CreateTable("OtherRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Var1")
.Column<int>("Var2")
.Column<int>("MyPartRecord_Id")
);
I'm not convinced by this approach, so I'd like to know if I can create the 1-N relationship using only the Infoset approach and eliminating the need to create the "fake" table in the migration, or if there is some smarter way to do it using the Record approach.
UPDATE
I tried to use the infoset approach but I can't make it work. I changed my files like this:
**Model**
public class MyPart : ContentPart
{
public IList<OtherRecord> MyList {
get { return this.Retrieve(x => x.MyList, new List<OtherRecord>()); }
}
**Migration**
SchemaBuilder.CreateTable("OtherRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Var1")
.Column<int>("Var2")
.Column<int>("MyPart_Id")
);
But when the driver calls the get method I always receive an empty list, so I don't think that the relations are built properly.

AutoMapper TwoWay Mapping with same Property Name

Given these two objects
public class UserModel
{
public string Name {get;set;}
public IList<RoleModel> Roles {get;set;}
}
public class UserViewModel
{
public string Name {get;set;}
public IList<RoleViewModel> Roles {get;set;} // notice the ViewModel
}
Is this the most optimal way to do the mapping, or is AutoMapper capable of mapping Roles to Roles on its own?
App Config
Mapper.CreateMap<UserModel, UserViewModel>()
.ForMember(dest => dest.Roles, opt => opt.MapFrom(src => src.Roles));
Mapper.CreateMap<UserViewModel, UserModel>()
.ForMember(dest => dest.Roles, opt => opt.MapFrom(src => src.Roles));
Implementation
_userRepository.Create(Mapper.Map<UserModel>(someUserViewModelWithRolesAttached);
Is this the most optimal way to do the mapping, or is AutoMapper capable of mapping Roles to Roles on its own?
If the property names are identical, you should not have to manually provide a mapping:
Mapper.CreateMap<UserModel, UserViewModel>();
Mapper.CreateMap<UserViewModel, UserModel>();
Just make sure the inner types are mapped as well (RoleViewModel ↔ RoleModel)
What this means, however, is that if you change a source or destination property name, AutoMapper mappings can fail silently and cause hard to track down problems (e.g., if you changed UserModel.Roles to UserModel.RolesCollection without changing UserViewModels.Roles).
AutoMapper provides a Mapper.AssertConfigurationIsValid() method that will check all of your mappings for errors and catch misconfigured mappings. It's useful to have a unit test that runs with the build that validates your mappings for this kind of problem.
You don't need to map the properties. Just make sure that the property names match and there is a mapping defined between them.
Mapper.CreateMap<UserModel, UserViewModel>();
Mapper.CreateMap<UserViewModel, UserModel>();
Mapper.CreateMap<RoleModel, RoleViewModel>();
Mapper.CreateMap<RoleViewModel, RoleModel>();
Or with the cooler way I just found out:
Mapper.CreateMap<UserModel, UserViewModel>().ReverseMap();
Mapper.CreateMap<RoleModel, RoleViewModel>().ReverseMap();
All the other answers, are much better (which I gave an upvote to each).
But what I wanted to post here is a quick playground that you could copy and past right into LinqPad in C# program mode and play your idea's without messing with your actual code.
Another awesome thing about moving all your conversions into a TyperConverter class is that your conversions are now Unit Testable. :)
Here you will notice that the model and viewmodel are almost identical except for one property. But through this process the right property is converted to the correct property in the destination object.
Copy this code into LinqPad and you can run it with the play button after switching to C# Program mode.
void Main()
{
AutoMapper.Mapper.CreateMap<UserModel, UserViewModel>().ConvertUsing(new UserModelToUserViewModelConverter());
AutoMapper.Mapper.AssertConfigurationIsValid();
var userModel = new UserModel
{
DifferentPropertyName = "Batman",
Name = "RockStar",
Roles = new[] {new RoleModel(), new RoleModel() }
};
var userViewModel = AutoMapper.Mapper.Map<UserViewModel>(userModel);
Console.WriteLine(userViewModel.ToString());
}
// Define other methods and classes here
public class UserModel
{
public string Name {get;set;}
public IEnumerable<RoleModel> Roles { get; set; }
public string DifferentPropertyName { get; set; }
}
public class UserViewModel
{
public string Name {get;set;}
public IEnumerable<RoleModel> Roles { get; set; } // notice the ViewModel
public string Thingy { get; set; }
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine(string.Format("Name: {0}", Name));
sb.AppendLine(string.Format("Thingy: {0}", Thingy));
sb.AppendLine(string.Format("Contains #{0} of roles", Roles.Count()));
return sb.ToString();
}
}
public class UserModelToUserViewModelConverter : TypeConverter<UserModel, UserViewModel>
{
protected override UserViewModel ConvertCore(UserModel source)
{
if(source == null)
{
return null;
}
//You can add logic here to deal with nulls, empty strings, empty objects etc
var userViewModel = new UserViewModel
{
Name = source.Name,
Roles = source.Roles,
Thingy = source.DifferentPropertyName
};
return userViewModel;
}
}
public class RoleModel
{
//no content for ease, plus this has it's own mapper in real life
}
Result from the Console.WriteLine(userViewModel.ToString());:
Name: RockStar
Thingy: Batman
Contains #2 of roles
Inside the Startup.cs in the Configure() method:
Mapper.Initialize(config => {
config.CreateMap<UserModel, UserViewModel>().ReverseMap();
// other maps you want to do.
});

Automapper nested mapping

I've read the nested mapping wiki page but it appears to not like multiple levels of nesting. I've got the following maps created and classes defined.
AutoMapper.Mapper.CreateMap<Address, AddressDTO>();
AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>();
public class MatchCompanyRequest
{
Address Address {get;set;}
}
public class MatchCompanyRequestDTO
{
public CompanyInformationDTO {get;set;}
}
public class CompanyInformationDTO {get;set;}
{
public string CompanyName {get;set;}
public AddressDTO Address {get;set;}
}
But the following code...
// works
matchCompanyRequestDTO.companyInformationDTO.Address =
AutoMapper.Mapper.Map<Address, AddressDTO>(matchCompanyRequest.Address);
// fails
matchCompanyRequestDTO =
AutoMapper.Mapper
.Map<MatchCompanyRequest, MatchCompanyRequestDTO>(matchCompanyRequest);
Does this deep nesting work and I have it configured improperly? Or is this kind of nesting not yet supported?
-- Edit
For anyone interested, I am not in control of the DTOs.
It lacks the mapping from Address to CompanyInformationDTO, as those objects are on the same nest-level.
The map is created for MatchCompanyRequest -> MatchCompanyRequestDTO, but it is unable to figure out whether it can map Address to CompanyInformationDTO.
So your MatchCompanyRequestDTO could in fact have same declaration as your CompanyInformationDTO:
public class MatchCompanyRequestDTO
{
public string CompanyName {get;set;}
public AddressDTO Address {get;set;}
}
This of course only affects you if you want to use automatic mapping. You still can configure your maps manually, but it seems like the DTOs should be fixed instead, let's try anyway:
public class CustomResolver : ValueResolver<Address, CompanyInformationDTO>
{
protected override CompanyInformationDTO ResolveCore(Address source)
{
return new CompanyInformationDTO() { Address = Mapper.Map<Address, AddressDTO>(source) };
}
}
// ...
AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>()
.ForMember(dest => dest.companyInformationDTO, opt => opt.ResolveUsing<CustomResolver>().FromMember(src => src.Address)); // here we are telling to use our custom resolver that converts Address into CompanyInformationDTO
The important thing is you define how deeper is your navigation, to previne the stackoverflow problems. Imagine this possibility:
You have 2 entities Users and Notifications in NxN model (And
you have DTOs object to represent that), when you user auto mapper
without set MaxDepth in you mapper expression, "Houston we have a
problem" :).
The code below show a workaround to resolve this for all Mappers. If you want can be defined to each mapper. Like this Question
Solution 1 (Global Definition)
public class AutoMapperConfig
{
public static void RegisterMappings()
{
Mapper.Initialize(mapperConfiguration =>
{
mapperConfiguration.AddProfile<DomainModelToYourDTOsMappingProfile>();
mapperConfiguration.AddProfile<YourDTOsToDomainModelMappingProfile>();
mapperConfiguration.AllowNullCollections = true;
mapperConfiguration.ForAllMaps(
(mapType, mapperExpression) => {
mapperExpression.MaxDepth(1);
});
}
}
Solution 2 (For each Mapper)
public class AutoMapperConfig
{
public static void RegisterMappings()
{
Mapper.CreateMap<User, DTOsModel>()
.MaxDepth(1);
}
}
Consider the following instead:
public class MatchCompanyRequest
{
Address Address {get;set;}
}
public class MatchCompanyRequestDTO
{
public string Name {get;set;}
public AddressDTO Address {get;set;}
}
public class AddressDTO
{
....
}
Your DTO objects need to have the same structure as your domain objects for the default mapping conventions to work in AutoMapper.
Look at this: https://github.com/AutoMapper/AutoMapper/wiki/Projection It will explain the Projection for you, you could customize it to work the way you have it.

Categories

Resources