Bonjour
I need some help to understand how we map OwnedEntity when they are polymorph.
I have this inventory hierarchy of records that are linked to a product class .
public abstract record Inventory
{
protected Inventory() { }
}
public record NoProductInventory : Inventory
{
public NoProductInventory()
{
}
}
public sealed record ProductLevelInventory : Inventory
{
public int Stock { get; private set; } = default!;
public int LowStock { get; private set; } = default!;
protected ProductLevelInventory() {}
public ProductLevelInventory(int stock, int lowStock) : base()
{
Stock = stock;
LowStock = lowStock;
}
}
public sealed record VariantLevelInventory : Inventory
{
public int Stock { get; private set; } = default!;
public int LowStock { get; private set; } = default!;
public int SomeOption { get; private set; }
protected VariantLevelInventory() {}
public VariantLevelInventory(int stock, int lowStock) : base()
{
Stock = stock;
LowStock = lowStock;
}
}
The Product class Definition
public class Product
{
....
public Inventory Inventory { get; private set; } = default!;
....
}
I am using fluent API in order to mapped those entities
public class ProductEntityTypeBuilder : IEntityTypeConfiguration<Product>
{
public void Configure(EntityTypeBuilder<Product> builder)
{
builder.OwnsOne(x => x.Inventory, bld =>
{
bld.ToTable("Inventories");
// bld.Property(x => x.TrackInventory).IsRequired();
bld.Property(x => x.Stock).IsRequired(false);
bld.Property(x => x.LowStock).IsRequired(false);
// bld.Property(x => x.InventoryTrackType).IsRequired(false);
});
}
}
My question is: how can I tell EF which Inventory record to use? I don't want to use casting to figure out what kind of inventory a product has.
Good Day !
Owned entities are meant to be used as value types, containing and encapsulate multiple values which belong together and don't have a meaning on their own, i.e. MonetaryAmount which consists of a Amount and a Currency property.
You can't do this right now. If you need hierarchies, you have to convert your owned types into entities and use it as regular navigation properties on the base type which are distinguished via a discriminator.
Owned types do not support inheritance Owned Types: Shortcommings
Current shortcomings
Owned entity types cannot have inheritance hierarchies
Related
First of all let's say I have two separated aggregates Basket and Order in an e-commerece website.
Basket aggregate has two entities Basket(which is the aggregate root) and BaskItem defined as following(I have removed factories and other aggregate methods for simplicity):
public class Basket : BaseEntity, IAggregateRoot
{
public int Id { get; set; }
public string BuyerId { get; private set; }
private readonly List<BasketItem> items = new List<BasketItem>();
public IReadOnlyCollection<BasketItem> Items
{
get
{
return items.AsReadOnly();
}
}
}
public class BasketItem : BaseEntity
{
public int Id { get; set; }
public decimal UnitPrice { get; private set; }
public int Quantity { get; private set; }
public string CatalogItemId { get; private set; }
}
The second aggregate which is Order has Order as aggregate root and OrderItem as entity and Address and CatalogueItemOrdered as value objects defined as following:
public class Order : BaseEntity, IAggregateRoot
{
public int Id { get; set; }
public string BuyerId { get; private set; }
public readonly List<OrderItem> orderItems = new List<OrderItem>();
public IReadOnlyCollection<OrderItem> OrderItems
{
get
{
return orderItems.AsReadOnly();
}
}
public DateTimeOffset OrderDate { get; private set; } = DateTimeOffset.Now;
public Address DeliverToAddress { get; private set; }
public string Notes { get; private set; }
}
public class OrderItem : BaseEntity
{
public int Id { get; set; }
public CatalogItemOrdered ItemOrdered { get; private set; }
public decimal Price { get; private set; }
public int Quantity { get; private set; }
}
public class CatalogItemOrdered
{
public int CatalogItemId { get; private set; }
public string CatalogItemName { get; private set; }
public string PictureUri { get; private set; }
}
public class Address
{
public string Street { get; private set; }
public string City { get; private set; }
public string State { get; private set; }
public string Country { get; private set; }
public string ZipCode { get; private set; }
}
Now If the user wants to checkout after adding several items to basket there are several actions should be applied:
Updating Basket(maybe some items' quantity has been changed)
Adding/Setting new Order
Deleting the basket(or flag as deleted in DB)
Paying via CreditCard using specific Payment gateway.
As I can see there are several transactions should be executed because depending on DDD in every transaction only one aggregate should be changed.
So could you please guide me to how can I implement that(maybe by using Eventual consistency) in a way I don't break DDD principles?
PS:
I appreciate any references or resources
The most important thing that your model is missing is behavior. Your classes are holding only data, sometimes with public setters when they shouldn't (like Basket.Id). Domain entities must define methods to operate on their data.
What you got right is that you have the aggregate root enclosing its children (e.g. Basket with a private list of Items). An aggregate is supposed to be treated like an atom, so everytime you load or persist a basket to the database, you'll be treating the Basket and Items as a single whole. This will even make things as lot easier for you.
This is a model of mine for a very similar domain:
public class Cart : AggregateRoot
{
private const int maxQuantityPerProduct = 10;
private const decimal minCartAmountForCheckout = 50m;
private readonly List<CartItem> items = new List<CartItem>();
public Cart(EntityId customerId) : base(customerId)
{
CustomerId = customerId;
IsClosed = false;
}
public EntityId CustomerId { get; }
public bool IsClosed { get; private set; }
public IReadOnlyList<CartItem> Items => items;
public decimal TotalAmount => items.Sum(item => item.TotalAmount);
public Result CanAdd(Product product, Quantity quantity)
{
var newQuantity = quantity;
var existing = items.SingleOrDefault(item => item.Product == product);
if (existing != null)
newQuantity += existing.Quantity;
if (newQuantity > maxQuantityPerProduct)
return Result.Fail("Cannot add more than 10 units of each product.");
return Result.Ok();
}
public void Add(Product product, Quantity quantity)
{
CanAdd(product, quantity)
.OnFailure(error => throw new Exception(error));
for (int i = 0; i < items.Count; i++)
{
if (items[i].Product == product)
{
items[i] = items[i].Add(quantity);
return;
}
}
items.Add(new CartItem(product, quantity));
}
public void Remove(Product product)
{
var existing = items.SingleOrDefault(item => item.Product == product);
if (existing != null)
items.Remove(existing);
}
public void Remove(Product product, Quantity quantity)
{
var existing = items.SingleOrDefault(item => item.Product == product);
for (int i = 0; i < items.Count; i++)
{
if (items[i].Product == product)
{
items[i] = items[i].Remove(quantity);
return;
}
}
if (existing != null)
existing = existing.Remove(quantity);
}
public Result CanCloseForCheckout()
{
if (IsClosed)
return Result.Fail("The cart is already closed.");
if (TotalAmount < minCartAmountForCheckout)
return Result.Fail("The total amount should be at least 50 dollars in order to proceed to checkout.");
return Result.Ok();
}
public void CloseForCheckout()
{
CanCloseForCheckout()
.OnFailure(error => throw new Exception(error));
IsClosed = true;
AddDomainEvent(new CartClosedForCheckout(this));
}
public override string ToString()
{
return $"{CustomerId}, Items {items.Count}, Total {TotalAmount}";
}
}
And the class for the Items:
public class CartItem : ValueObject<CartItem>
{
internal CartItem(Product product, Quantity quantity)
{
Product = product;
Quantity = quantity;
}
public Product Product { get; }
public Quantity Quantity { get; }
public decimal TotalAmount => Product.UnitPrice * Quantity;
public CartItem Add(Quantity quantity)
{
return new CartItem(Product, Quantity + quantity);
}
public CartItem Remove(Quantity quantity)
{
return new CartItem(Product, Quantity - quantity);
}
public override string ToString()
{
return $"{Product}, Quantity {Quantity}";
}
protected override bool EqualsCore(CartItem other)
{
return Product == other.Product && Quantity == other.Quantity;
}
protected override int GetHashCodeCore()
{
return Product.GetHashCode() ^ Quantity.GetHashCode();
}
}
Some important things to note:
Cart and CartItem are one thing. They are loaded from the database as a single unit, then persisted back as such, in one transaction;
Data and Operations (behavior) are close together. This is actually not a DDD rule or guideline, but an Object Oriented programming principle. This is what OO is all about;
Every operation someone can do with the model is expressed as a method in the aggregate root, and the aggreate root takes care of it all when it comes to dealing with its internal objects. It controls everything, every operation must go through the root;
For every operation that can potentially go wrong, there's a validation method. For example, you have the CanAdd and the Add methods. Consumers of this class should first call CanAdd and propagate possible errors up to the user. If Add is called without prior validation, than Add will check with CanAdd and throw an exception if any invariant were to be violated, and throwing an exception is the right thing to do here because getting to Add without first checking with CanAdd represents a bug in the software, an error by committed the programmers;
Cart is an entity, it has an Id, but CartItem is a ValueObject an has no Id. A customer could repeat a purchase with the same items and it would still be a different Cart, but a CartItem with the same properties (quantity, price, itemname) is always the same - it is the combination of its properties that make up its identity.
So, consider the rules of my domain:
The user can't add more than 10 units of each product to the cart;
The user can only proceed to checkout if they have at least 50 USD of products in the cart.
These are enforced by the aggregate root and there's no way of misusing the classes in any way that would allow breaking the invariants.
You can see the full model here: Shopping Cart Model
Back to your question
Updating Basket (maybe some items' quantity has been changed)
Have a method in the Basket class that will be responsible for operating changes to the basket items (adding, removing, changing quantity).
Adding/Setting new Order
It seems like an Order would reside in another Bounded Context. In that case, you would have a method like Basket.ProceedToCheckout that would mark itself as closed and would propagate a DomainEvent, which would in turn be picked up in the Order Bounded Context and an Order would be added/created.
But if you decide that the Order in your domain is part of the same BC as the Basket, you can have a DomainService that will deal with two aggregates at once: it would call Basket.ProceedToCheckout and, if no error is thrown, it would the create an Order aggregate from it. Note that this is an operation that spans two aggregates, and so it has been moved from the aggregate to the DomainService.
Note that a database transaction is not needed here in order the ensure the correctness of the state of the domain.
You can call Basket.ProceedToCheckout, which would change its internal state by setting a Closed property to true. Then the creation of the Order could go wrong and you would not need to rollback the Basket.
You could fix the error in the software, the customer could attempt to checkout once more and your logic would simply check whether the Basket is already closed and has a corresponding Order. If not, it would carry out only the necessary steps, skipping those already completed. This is what we call Idempotency.
Deleting the basket(or flag as deleted in DB)
You should really think more about that. Talk to the domain experts, because we don't delete anything the real world, and you probably shouldn't delete a basket in your domain. Because this is information that most likely has value to the business, like knowing which baskets were abandoned and then the marketing dept. could promote an action with discounts to bring back these customers so that they can buy.
I recommend you read this article: Don't Delete - Just Don't, by Udi Dahan. He dives deep in the subject.
Paying via CreditCard using specific Payment gateway
Payment Gateway is infrastructure, your Domain should not know anything about it (even interfaces should be declared in another layer). In terms of software architecture, more specifically in the Onion Architecture, I recommend you define these classes:
namespace Domain
{
public class PayOrderCommand : ICommand
{
public Guid OrderId { get; }
public PaymentInformation PaymentInformation { get; }
public PayOrderCommand(Guid orderId, PaymentInformation paymentInformation)
{
OrderId = orderId;
PaymentInformation = paymentInformation;
}
}
}
namespace Application
{
public class PayOrderCommandHandler : ICommandHandler<PayOrderCommand>
{
private readonly IPaymentGateway paymentGateway;
private readonly IOrderRepository orderRepository;
public PayOrderCommandHandler(IPaymentGateway paymentGateway, IOrderRepository orderRepository)
{
this.paymentGateway = paymentGateway;
this.orderRepository = orderRepository;
}
public Result Handle(PayOrderCommand command)
{
var order = orderRepository.Find(command.OrderId);
var items = GetPaymentItems(order);
var result = paymentGateway.Pay(command.PaymentInformation, items);
if (result.IsFailure)
return result;
order.MarkAsPaid();
orderRepository.Save(order);
return Result.Ok();
}
private List<PaymentItems> GetPaymentItems(Order order)
{
// TODO: convert order items to payment items.
}
}
public interface IPaymentGateway
{
Result Pay(PaymentInformation paymentInformation, IEnumerable<PaymentItems> paymentItems);
}
}
I hope this has given you some insight.
I'm trying to create a controller with a model that has custom types in it...
I have a main class with ID and a type 'Work', where I declared three properties, one of which relies on the other two. Then I created a dbset type. Are my mapping properties incorrect?
I am getting the following error:
There was an error running the selected code generator: "Unable to retrieve metadata for 'Stack.Models.Work'". the property sum is not a declared property on type Math. Verify that the property has not been explicitly excluded from the model by using the Ignore Method or NotMappedAttribute data annotation. Make sure it is a valid primitive property.
namespace stack.Models
{
public class Work
{
[Key]
public int ID { get; set; }
public Work ()
{
this.Maths = new Math();
}
public Math Maths { get; set; }
}
[ComplexType]
public class Math
{
public int first { get; set; }
public int second { get; set; }
public int sum
{
get
{
try
{
return first + second;
}
catch
{
return 0;
}
}
}
}
public class WorkDBContext: DbContext
{
public DbSet<Work> Working { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Work>()
.Property(c => c.Maths.first).IsRequired();
modelBuilder.Entity<Work>()
.Property(c => c.Maths.second).IsRequired();
modelBuilder.Entity<Work>()
.Property(c => c.Maths.sum).IsRequired();
base.OnModelCreating(modelBuilder);
}
}
}
Sum is not a simple type (column in the database) it is a data function(calculated from other properites), you do not have to store it in the database.
[ComplexType]
public class Math
{
public int first { get; set; }
public int second { get; set; }
[NotMapped]
public int sum
{
get
{
return first + second;
}
}
}
Remove this line:
modelBuilder.Entity<Work>()
.Property(c => c.Maths.sum).IsRequired();
Suppose, i have main class for data representation and this class have configuration field. This field must be able to answer some questions related to main class (assume, that this is one question - 'IsMainClassReadyToUse'). But inner structure of this class may be different.
Because of it, i want create abstract class Configurator and depending on situation use various Configuratos that implement its functional.
So, i have following code:
public class SimpleConfigurator : Configurator
{
public int FieldA { get; set; }
public override bool IsDataClassReadyToUse()
{
return ParentDataClass.FieldA == FieldA;
}
}
public class ComplexConfigurator : Configurator
{
public virtual List<int> FieldsB { get; set; }
public override bool IsDataClassReadyToUse()
{
return ParentDataClass.FieldsB.All(x => FieldsB.Any(y => y == x));
}
}
public abstract class Configurator
{
public int ConfiguratorId { get; set; }
public virtual DataClass ParentDataClass { get; set; }
public abstract bool IsDataClassReadyToUse();
}
public class DataClass
{
public int DataClassId { get; set; }
public virtual Configurator Configurator { get; set; }
public int FieldA { get; set; }
public virtual List<int> FieldsB { get; set; }
}
public class DataDbContext : DbContext
{
public DbSet<DataClass> DataClasses { get; set; }
}
But the problem appears when i try use DataClass instance with Configurator of type ComplexConfigurator.
Because of LazyLoading i need to load FieldsB from ComplexConfigurator, but abstract class Configurator doesn't contain such field and i can't write such code:
new DataDbContext().DataClasses
.Include(m => m.Configurator)
.Include(m => m.Configurator.FieldsB);
I tried to disable LazyLoading, adding such constructor in DataDbContext:
public DataDbContext()
{
Configuration.LazyLoadingEnabled = false;
}
But when i try get access to FieldsB it still be null.
So, how can i implement such architecture with Entity Framework?
Or maybe i should choose another architecture for such task?
I think you should try access you configurator such as
((ComplexConfigurator)yourObject.Configurator).FieldsB
But I'm afraid EF works wrong with List<int> property (when I tried do that sometimes I've got a fail) and better way is to create class Option and field List<Option> Options into your configurator instead of List with integers.
You also should check your DB scheme (there's should be a table "Configurators" with idenitifator field and all SimpleConfigurator and ComplexConfigurator's fields). May be you should add DbSet<Configurator> into your DbContext definition.
You can read this article for getting more information about inheritance and EF.
I could not find how to set up NHibernate with automapping conventions to detect my many to many relationship in the particular case where I'm using interfaces in references instead of concrete class. Notice that this work perfectly with One-To-Many and Many-To-One case.
Well for instance I have two entities:
public class Model4 : ModelBase, IModel4
{
public Model4()
{
Model5s = new List<IModel5>();
}
public virtual ValueInt D { get; set; }
public virtual IList<IModel5> Model5s { get; set; }
}
public class Model5 : ModelBase, IModel5
{
public Model5()
{
Model4s = new List<IModel4>();
}
public virtual ValueInt E { get; set; }
public virtual IList<IModel4> Model4s { get; set; }
}
where the interfaces are defined as:
public interface IModel4 : IModelBase
{
ValueInt D { get; set; }
IList<IModel5> Model5s { get; set; }
}
public interface IModel5 : IModelBase
{
ValueInt E { get; set; }
IList<IModel4> Model4s { get; set; }
}
I'm using conventions for references to translate the interface into class implementation when mapping:
public class ReferenceConvention : IReferenceConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
{
Type instanceType = instance.Class.GetUnderlyingSystemType();
if (instanceType.IsInterface)
{
// Assuming that the type starts with an I get the name of the concrete class
string className = instanceType.Name.Substring(1);
instance.CustomClass(instanceType.Assembly.GetType(
instanceType.FullName.Replace(instanceType.Name, className)));
}
instance.Cascade.All();
}
}
I would expect NHibernate to detect the model4 <-> model5 many-to-many relationship and to call my ManyToMany convention:
public class ManyToManyConvention : IHasManyToManyConvention
{
public void Apply(IManyToManyCollectionInstance instance)
{
//this code is not reached when using references through interfaces
}
}
NHibernate will never call the Apply method of ManyToManyConvention in the case I'm using references with interfaces (IList and IList).
However, if I use concrete implementation (IList and IList) the Apply code is called and the mapping is done as expected.
Do you know where I'm missing something in my conventions of where I'm wrong?
I'm trying to come up with a way to design a repository where adding and updating only accepts the exact amount of data/properties it can add/update.
I have the following design:
public interface IProduct
{
int Id { get; set; }
string Name { get; set; }
decimal Price { get; set; }
DateTime Created { get; set; }
DateTime Updated { get; set; }
}
public interface IProductRepository
{
void Add(IProduct product);
void Update(IProduct product);
IProduct Get(int id);
IEnumerable<IProduct> GetAll();
}
However, the Created and Updated properties are not really something I want to be modified outside of the database. The Id is not relevant when adding either, so I tried the following:
public interface IProductAdd
{
string Name { get; set; }
decimal Price { get; set; }
}
public interface IProductUpdate
{
int Id { get; set; }
string Name { get; set; }
decimal Price { get; set; }
}
And updated the repository accordingly:
public interface IProductRepository
{
void Add(IProductAdd product);
void Update(IProductUpdate product);
IProduct Get(int id);
IEnumerable<IProduct> GetAll();
}
Now only the relevant properties are present in each individual method.
I could then create a class that implements all product interfaces:
public class Product : IProduct, IProductAdd, IProductUpdate
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
}
So my question is: Is this the right way to do this?
My thoughts:
I could have opted to change the Add and Update methods on the Repository to accept every bit of product data as parameters, such as Update(int id, string name, decimal price), but it will get out of hand quickly when the amount of information a product holds increases.
My current solution involves repetition. If a product should hold a Description property, I would have to specify it in different 3 interfaces. I could let the interfaces implement each other to solve this...
public interface IProductAdd
{
string Name { get; set; }
decimal Price { get; set; }
string Description { get; set; }
}
public interface IProductUpdate : IProductAdd
{
int Id { get; set; }
}
public interface IProduct : IProductUpdate
{
DateTime Created { get; set; }
DateTime Updated { get; set; }
}
...but then I would get in trouble if IProductAdd were to have something that IProductUpdate shouldn't have.
Related problem: Let's say I want put products in categories, and have access to the category directly on each product.
public interface ICategory
{
int Id { get; set; }
string Name { get; set; }
string Description { get; set; }
}
public interface IProduct
{
int Id { get; set; }
string Name { get; set; }
decimal Price { get; set; }
DateTime Created { get; set; }
DateTime Updated { get; set; }
ICategory Category { get; set; }
}
When I change a product, I want to specify the id of the category (since I'm adding/updating the relationship, not the category itself):
public interface IProductAdd
{
string Name { get; set; }
decimal Price { get; set; }
int CategoryId { get; set; }
}
public interface IProductUpdate
{
int Id { get; set; }
string Name { get; set; }
decimal Price { get; set; }
int CategoryId { get; set; }
}
This results in the following implementation:
public class Product : IProduct, IProductAdd, IProductUpdate
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public ICategory Category { get; set; }
public int CategoryId { get; set; }
}
Looking at that, do I use Category.Id or CategoryId? Not ideal, IMO.
So I guess I see problems no matter how I do this. Am I too picky?
Am I looking at this wrong?
Should I just separate things completely because they ARE different things (eg. [the entity] / [add entity parameters] / [update entity parameters])?
I think you are over-complicating things while not separating your layers properly. In my opinion, the first two classes are how it should be done.
From what I understand, your whole issue is that you do not want the Created and Updated properties to be modified incorrectly. However, you are mixing up data and business concerns. Saying that a product's created date should be set upon the product being created is part of the business logic of creating a new product, and saying that a product's updateddate should be updated when x and y occur is also part of the logical process of updating a product's data. This is the same type of process as validating that the properties of the product are valid, the user is authorized, etc.., all of which are business process concerns, not data storage concerns.
Your repository should be solely part of the data layer, where it's only concern is how it retrieves the requested product from the database, how it updates a product in the database, or creates a product in the database. That's it.
You need a dedicated business layer that handles all the business logic for adding or updating a product's information. You will then call a method in this layer with the name and price of for the product you want to add, and in this method you will perform any validations you want to perform, determine if the user is authorized to be making these edits, and setting the CreatedDate or UpdatedDate if it's warranted. This method will then pass the Product entity to your repository to save it in the database.
Separating out the logic in this manner will make it MUCH easier when you want to change the logic on how things such as UpdatedDate is managed (maybe you want certain actions to change that date but not all actions). If you try and handle all of this in your repository/data layer, it will quickly become overwhelming and confusing when you get away from trivial use cases.
One other point. IProduct is a business entity, which means that you don't have to expose it to your presentation layer at all. Therefore, if you do not want to risk a developer touching certain properties, you can use what the MVC architecture usually calls ViewModels. Essentially, these are data structures that are used on the presentation layer, and the business layer can then translate these ViewModels into actual business entities.
So for example, you could have:
public class ProductViewModel
{
int Id { get; set; }
string Name { get; set; }
decimal Price { get; set; }
int CategoryId { get; set; }
}
Your presentation layer will pass a filled out ProductViewModel into your business layer's AddProduct() or UpdateProduct() methods, will then retrieve the database's IProduct entity for the one specified and use the ProductViewModel to determine how to update (or create a new) the database entity. This way, you never expose the two DateTime properties but still have full control over how and when they are set.
Forgive me if I've misunderstood you here but to me it appears that your design logic is incorrect. In essence your base entity is Product which has a number of actions Add, Update etc.
So why don't you declare base base IProduct interface which only has the minimum amount of properties required for all actions, e.g. description, category etc.
Then just get each of the actions e.g. IProductAdd inherit from this base interface. The product class itself should only inherit from the IProduct interface.
Then create new classes for each of the actions e.g. add which inherits from IProduct add & just add some methods in the product class which accept parameters of type IProductAdd etc. but which use instances of the action classes to perform work
This is how I'd go about it....I'd use reflection and attribute(s):
namespace StackOverFlowSpike.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class ReadOnlyAttribute : Attribute
{
public ReadOnlyAttribute() { }
}
}
using StackOverFlowSpike.Attributes;
namespace StackOverFlowSpike.Entities
{
public interface IEntity
{
[ReadOnly]
public int Id { get; set; }
}
}
using System;
using StackOverFlowSpike.Attributes;
namespace StackOverFlowSpike.Entities
{
public class Product : IEntity
{
[ReadOnly]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
[ReadOnly]
public DateTime Created { get; set; }
[ReadOnly]
public DateTime Updated { get; set; }
}
}
using StackOverFlowSpike.Entities;
using System.Collections.Generic;
namespace StackOverFlowSpike.Repositories
{
public interface IRepository<T> where T : IEntity
{
void Add(T item);
void Update(T item);
T Get(int id);
IEnumerable<T> GetAll();
}
}
using System;
using System.Linq;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using StackOverFlowSpike.Entities;
using StackOverFlowSpike.Attributes;
namespace StackOverFlowSpike.Repositories
{
public class ProductRepositoryMock : IRepository<Product>
{
#region Fields and constructor
private IList<Product> _productsStore;
public ProductRepositoryMock()
{
_productsStore = new List<Product>();
}
#endregion
#region private methods
private int GetNewId()
{
return _productsStore
.OrderByDescending(p => p.Id)
.Select(p => p.Id).FirstOrDefault() + 1;
}
private void PopulateProduct(Product storedProduct, Product incomingProduct)
{
foreach (var p in storedProduct.GetType().GetProperties())
{
// check if it is NOT decorated with ReadOnly attribute
if (!(p.GetCustomAttributes(typeof(ReadOnlyAttribute), false).Length > 0))
{
// i will use reflection to set the value
p.SetValue(storedProduct, p.GetValue(incomingProduct, null), null);
}
}
}
private void Synchronise(Product storedProduct, Product incomingProduct)
{
foreach (var p in storedProduct.GetType().GetProperties())
p.SetValue(incomingProduct, p.GetValue(storedProduct, null), null);
}
#endregion
public void Add(Product product)
{
Product newProduct = new Product();
newProduct.Id = GetNewId();
newProduct.Created = DateTime.Now;
newProduct.Updated = DateTime.Now;
PopulateProduct(newProduct, product);
_productsStore.Add(newProduct);
Synchronise(newProduct, product);
// system takes a quick nap so we can it really is updating created and updated date/times
Thread.Sleep(1000);
}
public void Update(Product product)
{
var storedProduct = _productsStore.Where(p => p.Id == product.Id).FirstOrDefault();
if (storedProduct != null)
{
PopulateProduct(storedProduct, product);
storedProduct.Updated = DateTime.Now;
// system takes a quick nap so we can it really is updating created and updated date/times
Synchronise(storedProduct, product);
Thread.Sleep(1000);
}
}
public Product Get(int id)
{
Product storedProduct = _productsStore.Where(p => p.Id == id).FirstOrDefault();
Product resultProduct = new Product()
{
Id = storedProduct.Id,
Name = storedProduct.Name,
Price = storedProduct.Price,
Created = storedProduct.Created,
Updated = storedProduct.Updated
};
return resultProduct;
}
public IEnumerable<Product> GetAll()
{
return _productsStore;
}
}
}
Here is a small console program to test
using System;
using System.Text;
using System.Collections.Generic;
using StackOverFlowSpike.Entities;
using StackOverFlowSpike.Repositories;
namespace StackOverFlowSpike
{
class Program
{
static void Main(string[] args)
{
Product p1 = new Product()
{
Created = Convert.ToDateTime("01/01/2012"), // ReadOnly - so should not be updated with this value
Updated = Convert.ToDateTime("01/02/2012"), // ReadOnly - so should not be updated with this value
Id = 99, // ReadOnly - should not be udpated with this value
Name = "Product 1",
Price = 12.30m
};
Product p2 = new Product()
{
Name = "Product 2",
Price = 18.50m,
};
IRepository<Product> repo = new ProductRepositoryMock();
// test the add
repo.Add(p1);
repo.Add(p2);
PrintProducts(repo.GetAll());
// p1 should not change because of change in Id
p1.Id = 5; // no update should happen
p1.Name = "Product 1 updated";
p1.Price = 10.50m;
// p2 should update name and price but not date created
p2.Name = "Product 2 updated";
p2.Price = 17m;
p2.Created = DateTime.Now;
repo.Update(p1);
repo.Update(p2);
PrintProducts(repo.GetAll());
Console.ReadKey();
}
private static void PrintProducts(IEnumerable<Product> products)
{
foreach (var p in products)
{
Console.WriteLine("Id: {0}\nName: {1}\nPrice: {2}\nCreated: {3}\nUpdated: {4}\n",
p.Id, p.Name, p.Price, p.Created, p.Updated);
}
Console.WriteLine(new StringBuilder().Append('-', 50).AppendLine().ToString());
}
}
}
Test results:
Id: 1
Name: Product 1
Price: 12.30
Created: 29/04/2011 18:41:26
Updated: 29/04/2011 18:41:26
Id: 2
Name: Product 2
Price: 18.50
Created: 29/04/2011 18:41:28
Updated: 29/04/2011 18:41:28
Id: 1
Name: Product 1
Price: 12.30
Created: 29/04/2011 18:41:26
Updated: 29/04/2011 18:41:26
Id: 2
Name: Product 2 updated Price: 17
Created: 29/04/2011 18:41:28
Updated: 29/04/2011 18:41:29