在MVC SessionState会使用WCF DataContract使用的AppFabric缓存 [英] Using WCF DataContract in MVC SessionState using AppFabric cache

查看:145
本文介绍了在MVC SessionState会使用WCF DataContract使用的AppFabric缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据访问层,服务层和presentation层。在presentation层是ASP.NET MVC2 RTM(网页),和业务层是WCF(服务)。这是所有的.NET 3.5 SP1。

I have a Data Access Layer, a Service Layer, and a Presentation Layer. The Presentation Layer is ASP.NET MVC2 RTM (web), and the Service Layer is WCF (services). It's all .NET 3.5 SP1.

的问题是,在服务,正在返回的对象都标有 [DataContract] 属性。该网站使用的AppFabric缓存(a.k.a速度)SessionStateProvider来存储会话状态。由于这个原因,我的任何存储在会话必须可序列化。

The problem is that in the services, the objects being returned are marked with the [DataContract] attribute. The web is using the AppFabric Cache (a.k.a Velocity) SessionStateProvider to store session state. Due to this, anything I store in the session must be serializable.

下面问题来了​​:在DataContracts未标有 [Serializable接口] 而据我的记忆,通过引入它已经上标有<一类code> [DataContract] 若干问题的出现,所以我不相信这是一个解决方案。

Here comes the problem: the DataContracts aren't marked with [Serializable] and as far as I can remember, by introducing it onto a class already marked with [DataContract] some issues arise, and so I don't believe this is a solution.

我最初打算使用权网层中的DataContracts,使用它们作为模型来与呈现DataContracts次(大概嵌套更高级别ViewModel类内侧)。但由于需要存储在它里面的所有对象的会话状态提供可序列化,我开始重新考虑这一策略。这将是不错的,虽然,因为它们包含使用 IDataErrorInfo的界面验证逻辑,并在MVC相同的验证逻辑,可以重新作为模型的一部分​​结合。

I was initially planning on using the DataContracts right in the web layer, using them as models to views related to rendering the DataContracts (probably nested inside a higher level ViewModel class). But due to the session state provider requiring all objects stored inside it to be serializable, I'm starting to rethink this strategy. It would be nice to have though, since they contain validation logic using the IDataErrorInfo interface, and the same validation logic could be re-used in MVC as part of model binding.

你相信什么,是让我减少工作的最佳方法需要的?

What do you believe is the best way to allow me to reduce the work needed?

我目前认为下列不同的方式:

I've currently thought of the following different ways:

一个。在WEB项目ServiceIntegration的一部分。

这将是我的控制器和我的WCF服务层之间的中间人。所述ServiceIntegration部分将向服务层使用DataContracts使用的ViewModels说话,到Web层,但必须使用一个双向变压器的DataContracts和的ViewModels之间转换

This would be a middle man between my controllers and my WCF service layer. The ServiceIntegration part would speak to the service layer using DataContracts, and to the Web layer using ViewModels, but would have to transform between the DataContracts and ViewModels using a two-way Transformer.

另外,由于IDataErrorInfo的验证不会重复使用,这将是必要的,以创建每DataContract太一个验证程序,使用该变压器,以从视图模型转换为DataContract使用IDataErrorInfo的执行验证并返回其结果。

Also, since the IDataErrorInfo Validation wouldn't be re-usable, it would be necessary to create a Validator per DataContract too, that uses the Transformer to convert from ViewModel to DataContract, perform validation using IDataErrorInfo and return its results. This would then be used inside action methods of Controllers (e.g. if (!MyValidator.IsValid(viewModel)) return View();)

不同类别的要求:xDataContract,xViewModel,xTransformer,xValidator

乙。在Web项目中创建一个SessionIntegration的一部分

这将是控制器(或任何访问会话)和会议本身之间的中间人。任何需要访问会话会去通过这个类。 DataContracts将在整个申请中使用的,除非它们被存储到会议。所述SessionIntegration部分将采取转化DataContract一些ISerializable的形式,和背面的责任。因为在DataContract使用IDataErrorInfo的界面的不需要额外的验证

This would be a middle-man between the controllers (or anything accessing the session) and the session itself. Anything requiring access to the session would go through this class. DataContracts would be used in the entire application, unless they are being stored into the session. The SessionIntegration part would take the responsibility of transforming the DataContract to some ISerializable form, and back. No additional Validator is needed because of the use of of IDataErrorInfo interface on the DataContract.

不同类别的要求:xDataContract,xTransformer,xSerializableForm

注:仍然会存在的ViewModels围绕这两种情形,但是与(B)我可以从DataContracts组成的ViewModels

Note: there would still be ViewModels around in both scenarios, however with (B) I'd be able to compose ViewModels from DataContracts.

(二)具有不需要额外的验证程序的优势。

(B) has the benefit of not needing an extra validator.

在我熄灭并实现(A)/(B)完全,我想一些反馈。此刻,我开始对(B)瘦,但是,(A)可能会更灵活。无论哪种方式,这似乎是对什么是值得太多的工作。有没有其他人遇到这个问题,你同意/不同意我,和/或你有解决问题的任何其他方式?

Before I go off and implement (A)/(B) fully, I'd like some feedback. At the moment, I'm starting to lean towards (B), however, (A) might be more flexible. Either way, it seems like way too much work for what it's worth. Has anyone else come across this problem, do you agree/disagree with me, and/or do you have any other way of solving the problem?

谢谢,

詹姆斯

推荐答案

足不出户A或B的完全成熟的路线,你能不能只是做一个普通的ISerializable的包装对象,并把那些在您的SessionState会?

Without going the full blown route of A or B, could you just make a generic ISerializable wrapper object and put those in your SessionState?

    [Serializable]
    public class Wrapper : ISerializable
    {
        public object Value { get; set; }

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (Value != null)
            {
                info.AddValue("IsNull", false);
                if (Value.GetType().GetCustomAttributes(typeof(DataContractAttribute), false).Length == 1)
                {
                    using (var ms = new MemoryStream())
                    {
                        var serializer = new DataContractSerializer(Value.GetType());
                        serializer.WriteObject(ms, Value);
                        info.AddValue("Bytes", ms.ToArray());
                        info.AddValue("IsDataContract", true);
                    }
                }
                else if (Value.GetType().IsSerializable)
                {
                    info.AddValue("Value", Value);
                    info.AddValue("IsDataContract", false);
                }
                info.AddValue("Type", Value.GetType());
            }
            else
            {
                info.AddValue("IsNull", true);
            }
        }

        public Wrapper(SerializationInfo info, StreamingContext context)
        {
            if (!info.GetBoolean("IsNull"))
            {
                var type = info.GetValue("Type", typeof(Type)) as Type;

                if (info.GetBoolean("IsDataContract"))
                {
                    using (var ms = new MemoryStream(info.GetValue("Bytes", typeof(byte[])) as byte[]))
                    {
                        var serializer = new DataContractSerializer(type);
                        Value = serializer.ReadObject(ms);
                    }
                }
                else
                {
                    Value = info.GetValue("Value", type);   
                }
            }
        }
    }

这篇关于在MVC SessionState会使用WCF DataContract使用的AppFabric缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆